Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -144,16 +144,15 @@ def chatbot_interface(message, history, use_web_search, model, temperature, num_
|
|
144 |
history = history + [(message, "")]
|
145 |
|
146 |
try:
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
yield history
|
151 |
-
else:
|
152 |
-
for partial_response in get_response_from_pdf(message, model, num_calls=num_calls, temperature=temperature):
|
153 |
-
history[-1] = (message, partial_response)
|
154 |
-
yield history
|
155 |
except gr.CancelledError:
|
156 |
yield history
|
|
|
|
|
|
|
|
|
157 |
|
158 |
def retry_last_response(history, use_web_search, model, temperature, num_calls):
|
159 |
if not history:
|
@@ -165,12 +164,28 @@ def retry_last_response(history, use_web_search, model, temperature, num_calls):
|
|
165 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
166 |
|
167 |
def respond(message, history, model, temperature, num_calls, use_web_search):
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
174 |
|
175 |
def get_response_with_search(query, model, num_calls=3, temperature=0.2):
|
176 |
search_results = duckduckgo_search(query)
|
|
|
144 |
history = history + [(message, "")]
|
145 |
|
146 |
try:
|
147 |
+
for response in respond(message, history, model, temperature, num_calls, use_web_search):
|
148 |
+
history[-1] = (message, response)
|
149 |
+
yield history
|
|
|
|
|
|
|
|
|
|
|
150 |
except gr.CancelledError:
|
151 |
yield history
|
152 |
+
except Exception as e:
|
153 |
+
logging.error(f"Unexpected error in chatbot_interface: {str(e)}")
|
154 |
+
history[-1] = (message, f"An unexpected error occurred: {str(e)}")
|
155 |
+
yield history
|
156 |
|
157 |
def retry_last_response(history, use_web_search, model, temperature, num_calls):
|
158 |
if not history:
|
|
|
164 |
return chatbot_interface(last_user_msg, history, use_web_search, model, temperature, num_calls)
|
165 |
|
166 |
def respond(message, history, model, temperature, num_calls, use_web_search):
|
167 |
+
logging.info(f"User Query: {message}")
|
168 |
+
logging.info(f"Model Used: {model}")
|
169 |
+
logging.info(f"Search Type: {'Web Search' if use_web_search else 'PDF Search'}")
|
170 |
+
|
171 |
+
try:
|
172 |
+
if use_web_search:
|
173 |
+
for main_content, sources in get_response_with_search(message, model, num_calls=num_calls, temperature=temperature):
|
174 |
+
response = f"{main_content}\n\n{sources}"
|
175 |
+
logging.info(f"Generated Response (first line): {response.split('\n')[0]}")
|
176 |
+
yield response
|
177 |
+
else:
|
178 |
+
for partial_response in get_response_from_pdf(message, model, num_calls=num_calls, temperature=temperature):
|
179 |
+
logging.info(f"Generated Response (first line): {partial_response.split('\n')[0]}")
|
180 |
+
yield partial_response
|
181 |
+
except Exception as e:
|
182 |
+
logging.error(f"Error with {model}: {str(e)}")
|
183 |
+
if "microsoft/Phi-3-mini-4k-instruct" in model:
|
184 |
+
logging.info("Falling back to Mistral model due to Phi-3 error")
|
185 |
+
fallback_model = "mistralai/Mistral-7B-Instruct-v0.3"
|
186 |
+
yield from respond(message, history, fallback_model, temperature, num_calls, use_web_search)
|
187 |
+
else:
|
188 |
+
yield f"An error occurred with the {model} model: {str(e)}. Please try again or select a different model."
|
189 |
|
190 |
def get_response_with_search(query, model, num_calls=3, temperature=0.2):
|
191 |
search_results = duckduckgo_search(query)
|