daniel-simeone commited on
Commit ·
080bb85
1
Parent(s): 3680137
change to conversational model
Browse files
app.py
CHANGED
|
@@ -1,6 +1,15 @@
|
|
| 1 |
"""
|
| 2 |
Gradio app for Hugging Face chatbot with RAG capabilities.
|
| 3 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from gradio.themes.base import Base
|
| 6 |
from gradio.themes.utils import colors, fonts, sizes
|
|
@@ -127,6 +136,21 @@ class RAGChatbot:
|
|
| 127 |
|
| 128 |
self.chat_history = []
|
| 129 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
def generate_response(self, query: str, use_rag: bool = True, num_results: int = 5) -> str:
|
| 131 |
"""
|
| 132 |
Generate a response to the user query using RAG and Inference API.
|
|
@@ -166,16 +190,12 @@ Question: {query}
|
|
| 166 |
|
| 167 |
Answer:"""
|
| 168 |
|
| 169 |
-
# Generate response using
|
| 170 |
try:
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
top_p=0.9,
|
| 176 |
-
return_full_text=False
|
| 177 |
-
)
|
| 178 |
-
return response.strip()
|
| 179 |
except Exception as api_error:
|
| 180 |
print(f"Error calling Inference API: {api_error}")
|
| 181 |
# Fallback: return formatted chunks with note
|
|
@@ -201,15 +221,10 @@ Answer:"""
|
|
| 201 |
Question: {query}
|
| 202 |
|
| 203 |
Answer:"""
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
temperature=0.7,
|
| 209 |
-
top_p=0.9,
|
| 210 |
-
return_full_text=False
|
| 211 |
-
)
|
| 212 |
-
return response.strip()
|
| 213 |
except Exception as e:
|
| 214 |
print(f"Error generating response: {e}")
|
| 215 |
return f"I encountered an error while generating a response: {str(e)}. Please check your HF_TOKEN configuration."
|
|
|
|
| 1 |
"""
|
| 2 |
Gradio app for Hugging Face chatbot with RAG capabilities.
|
| 3 |
"""
|
| 4 |
+
import warnings
|
| 5 |
+
|
| 6 |
+
# Suppress deprecation from dependencies (e.g. accelerate) until they use torch.distributed.ReduceOp
|
| 7 |
+
warnings.filterwarnings(
|
| 8 |
+
"ignore",
|
| 9 |
+
message=".*torch.distributed.reduce_op.*ReduceOp.*",
|
| 10 |
+
category=FutureWarning,
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
import gradio as gr
|
| 14 |
from gradio.themes.base import Base
|
| 15 |
from gradio.themes.utils import colors, fonts, sizes
|
|
|
|
| 136 |
|
| 137 |
self.chat_history = []
|
| 138 |
|
| 139 |
+
def _generate_with_chat(self, user_content: str, max_new_tokens: int = 512) -> str:
|
| 140 |
+
"""Call the Inference API using chat/comversational endpoint (required for Mistral instruct)."""
|
| 141 |
+
response = self.inference_client.chat_completion(
|
| 142 |
+
model=self.model_name,
|
| 143 |
+
messages=[{"role": "user", "content": user_content}],
|
| 144 |
+
max_tokens=max_new_tokens,
|
| 145 |
+
temperature=0.7,
|
| 146 |
+
)
|
| 147 |
+
# ChatCompletion has choices[0].message.content
|
| 148 |
+
if response and response.choices and len(response.choices) > 0:
|
| 149 |
+
msg = response.choices[0].message
|
| 150 |
+
if hasattr(msg, "content") and msg.content:
|
| 151 |
+
return msg.content.strip()
|
| 152 |
+
return ""
|
| 153 |
+
|
| 154 |
def generate_response(self, query: str, use_rag: bool = True, num_results: int = 5) -> str:
|
| 155 |
"""
|
| 156 |
Generate a response to the user query using RAG and Inference API.
|
|
|
|
| 190 |
|
| 191 |
Answer:"""
|
| 192 |
|
| 193 |
+
# Generate response using chat/comversational API (Mistral instruct uses this)
|
| 194 |
try:
|
| 195 |
+
response_text = self._generate_with_chat(prompt, max_new_tokens=512)
|
| 196 |
+
if response_text:
|
| 197 |
+
return response_text
|
| 198 |
+
raise ValueError("Empty response from model")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
except Exception as api_error:
|
| 200 |
print(f"Error calling Inference API: {api_error}")
|
| 201 |
# Fallback: return formatted chunks with note
|
|
|
|
| 221 |
Question: {query}
|
| 222 |
|
| 223 |
Answer:"""
|
| 224 |
+
response_text = self._generate_with_chat(prompt, max_new_tokens=256)
|
| 225 |
+
if response_text:
|
| 226 |
+
return response_text
|
| 227 |
+
return "I couldn't generate a response. Please try again."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
except Exception as e:
|
| 229 |
print(f"Error generating response: {e}")
|
| 230 |
return f"I encountered an error while generating a response: {str(e)}. Please check your HF_TOKEN configuration."
|