Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,7 +13,7 @@ from pathlib import Path
|
|
| 13 |
import time
|
| 14 |
|
| 15 |
# Set up logging
|
| 16 |
-
logging.basicConfig(level=logging.INFO)
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
# API Key setup
|
|
@@ -32,12 +32,12 @@ bhashini_user_id = os.getenv("USER_ID", "").strip()
|
|
| 32 |
# π― MODEL LIST: Ordered by parameter size (largest β smallest)
|
| 33 |
# Fallback chain: if rate limit (429) or model error, try next model
|
| 34 |
MODEL_CHAIN = [
|
| 35 |
-
{"id": "llama-3.3-70b-versatile", "name": "Llama 3.3 70B", "priority": 1},
|
| 36 |
-
{"id": "qwen/qwen3-32b", "name": "Qwen3 32B", "priority": 2},
|
| 37 |
-
{"id": "meta-llama/llama-4-scout-17b-16e-instruct", "name": "Llama-4 Scout 17B", "priority": 3},
|
| 38 |
-
{"id": "groq/compound", "name": "Groq Compound", "priority": 4},
|
| 39 |
-
{"id": "gemma2-9b-it", "name": "Gemma2 9B", "priority": 5},
|
| 40 |
-
{"id": "llama-3.1-8b-instant", "name": "Llama 3.1 8B Instant", "priority": 6},
|
| 41 |
]
|
| 42 |
|
| 43 |
def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
|
|
@@ -46,7 +46,6 @@ def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") ->
|
|
| 46 |
return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
|
| 47 |
|
| 48 |
print(f'Translating: "{text[:50]}..." from {from_code} to {to_code}')
|
| 49 |
-
gr.Warning(f'Translating to {to_code}...')
|
| 50 |
|
| 51 |
url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
|
| 52 |
headers = {
|
|
@@ -113,11 +112,11 @@ def create_agent_with_model(model_id: str) -> Agent:
|
|
| 113 |
],
|
| 114 |
model=Groq(id=model_id, api_key=api_key),
|
| 115 |
markdown=True,
|
| 116 |
-
timeout=60
|
| 117 |
)
|
| 118 |
|
| 119 |
|
| 120 |
-
def generate_with_fallback(prompt: str
|
| 121 |
"""
|
| 122 |
Try models in descending size order.
|
| 123 |
Returns: (response_text, model_used)
|
|
@@ -143,23 +142,22 @@ def generate_with_fallback(prompt: str, max_retries: int = 2) -> tuple[str, str]
|
|
| 143 |
error_msg = str(e).lower()
|
| 144 |
last_error = e
|
| 145 |
|
| 146 |
-
# Check for
|
| 147 |
should_fallback = any([
|
| 148 |
-
"429" in error_msg,
|
| 149 |
"rate limit" in error_msg,
|
| 150 |
"too many requests" in error_msg,
|
| 151 |
"model_decommissioned" in error_msg,
|
| 152 |
"model_not_found" in error_msg,
|
| 153 |
"invalid_request_error" in error_msg and "model" in error_msg,
|
| 154 |
-
"timeout" in error_msg and priority < len(MODEL_CHAIN)
|
| 155 |
])
|
| 156 |
|
| 157 |
if should_fallback:
|
| 158 |
logger.warning(f"β οΈ {model_name} failed ({type(e).__name__}), falling back to next model...")
|
| 159 |
-
time.sleep(0.5 * priority)
|
| 160 |
continue
|
| 161 |
else:
|
| 162 |
-
# Non-recoverable error - don't fallback
|
| 163 |
logger.error(f"β {model_name} failed with non-fallback error: {e}")
|
| 164 |
break
|
| 165 |
|
|
@@ -167,7 +165,6 @@ def generate_with_fallback(prompt: str, max_retries: int = 2) -> tuple[str, str]
|
|
| 167 |
error_details = f"{type(last_error).__name__}: {str(last_error)}" if last_error else "Unknown error"
|
| 168 |
logger.error(f"π₯ All models failed. Last error: {error_details}")
|
| 169 |
|
| 170 |
-
# Return graceful fallback response
|
| 171 |
fallback_response = (
|
| 172 |
"β οΈ I'm experiencing high demand right now. Please try again in a moment.\n\n"
|
| 173 |
f"*Technical note: Unable to connect to available models ({error_details})*"
|
|
@@ -192,10 +189,8 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
|
| 192 |
documents = [doc["text"] for doc in documents]
|
| 193 |
|
| 194 |
# Re-rank documents using cross-encoder
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
else 'cross-encoder/ms-marco-MiniLM-L-6-v2'
|
| 198 |
-
)
|
| 199 |
query_doc_pair = [[query, doc] for doc in documents]
|
| 200 |
cross_scores = cross_encoder_model.predict(query_doc_pair)
|
| 201 |
sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
|
|
@@ -215,7 +210,7 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
|
| 215 |
# Create full prompt
|
| 216 |
full_prompt = f"{history_context}{context}Question: {query}\n\nPlease answer the question using the context provided above. If the context doesn't contain relevant information, use your general knowledge about 10th-grade science topics."
|
| 217 |
|
| 218 |
-
#
|
| 219 |
response_text, model_used = generate_with_fallback(full_prompt)
|
| 220 |
|
| 221 |
# Add model attribution to response
|
|
@@ -232,23 +227,6 @@ def retrieve_and_generate_response(query, cross_encoder_choice, history=None):
|
|
| 232 |
return f"Error: {str(e)}", [], "error"
|
| 233 |
|
| 234 |
|
| 235 |
-
def simple_chat_function(message, history, cross_encoder_choice):
|
| 236 |
-
"""Chat function with semantic search, multi-model fallback, and template rendering"""
|
| 237 |
-
if not message.strip():
|
| 238 |
-
return "", history, "", "N/A"
|
| 239 |
-
|
| 240 |
-
# Generate response and get documents + model info
|
| 241 |
-
response, documents, model_used = retrieve_and_generate_response(message, cross_encoder_choice, history)
|
| 242 |
-
|
| 243 |
-
# Add to history
|
| 244 |
-
history.append([message, response])
|
| 245 |
-
|
| 246 |
-
# Render template with documents and query
|
| 247 |
-
prompt_html = template_html.render(documents=documents, query=message, model=model_used)
|
| 248 |
-
|
| 249 |
-
return "", history, prompt_html, model_used
|
| 250 |
-
|
| 251 |
-
|
| 252 |
def translate_text(selected_language, history):
|
| 253 |
"""Translate the last response in history to the selected language."""
|
| 254 |
iso_language_codes = {
|
|
@@ -285,6 +263,7 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft', css="""
|
|
| 285 |
border-radius: 12px; font-size: 0.8em; font-weight: 500;
|
| 286 |
display: inline-block; margin: 4px 0;
|
| 287 |
}
|
|
|
|
| 288 |
""") as demo:
|
| 289 |
|
| 290 |
# Header section
|
|
@@ -297,10 +276,10 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft', css="""
|
|
| 297 |
try:
|
| 298 |
gr.Image(value='logo.png', height=180, width=180, show_label=False)
|
| 299 |
except:
|
| 300 |
-
gr.HTML("<div style='height: 180px; width: 180px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold;'>π€</div>")
|
| 301 |
|
| 302 |
-
# Model status indicator
|
| 303 |
-
model_status = gr.Markdown(
|
| 304 |
|
| 305 |
# Chatbot
|
| 306 |
chatbot = gr.Chatbot(
|
|
@@ -350,28 +329,30 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft', css="""
|
|
| 350 |
|
| 351 |
prompt_html = gr.HTML(label="π Source Documents")
|
| 352 |
|
| 353 |
-
# Event
|
| 354 |
def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
|
| 355 |
-
|
| 356 |
-
return "", history, "", "", "N/A"
|
| 357 |
|
| 358 |
-
#
|
| 359 |
-
|
|
|
|
| 360 |
|
| 361 |
-
# Generate response
|
| 362 |
-
response, documents, model_used = retrieve_and_generate_response(
|
|
|
|
|
|
|
| 363 |
|
| 364 |
-
#
|
| 365 |
status_emoji = "β
" if model_used != "fallback" else "β οΈ"
|
| 366 |
-
|
| 367 |
|
| 368 |
-
# Add to history
|
| 369 |
history.append([message, response])
|
| 370 |
|
| 371 |
# Translate response
|
| 372 |
translated_text = translate_text(selected_language, history)
|
| 373 |
|
| 374 |
-
# Render template
|
| 375 |
prompt_html_content = template_html.render(
|
| 376 |
documents=documents,
|
| 377 |
query=message,
|
|
@@ -379,28 +360,37 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft', css="""
|
|
| 379 |
timestamp=time.strftime("%H:%M:%S")
|
| 380 |
)
|
| 381 |
|
| 382 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 383 |
|
| 384 |
-
# Bind events
|
| 385 |
msg.submit(
|
| 386 |
update_chat_and_translate,
|
| 387 |
-
[msg, chatbot, cross_encoder, language_dropdown],
|
| 388 |
-
[msg, chatbot, translated_textbox, prompt_html, model_status]
|
| 389 |
)
|
|
|
|
| 390 |
submit_btn.click(
|
| 391 |
update_chat_and_translate,
|
| 392 |
-
[msg, chatbot, cross_encoder, language_dropdown],
|
| 393 |
-
[msg, chatbot, translated_textbox, prompt_html, model_status]
|
| 394 |
)
|
| 395 |
|
| 396 |
-
# Clear button
|
| 397 |
clear = gr.Button("ποΈ Clear Conversation", variant="secondary")
|
| 398 |
clear.click(
|
| 399 |
lambda: ([], "", "", "", "π *Ready to answer with best available model*"),
|
| 400 |
outputs=[chatbot, msg, translated_textbox, prompt_html, model_status]
|
| 401 |
)
|
| 402 |
|
| 403 |
-
#
|
| 404 |
gr.Examples(
|
| 405 |
examples=[
|
| 406 |
"What is the difference between metals and non-metals?",
|
|
@@ -414,8 +404,8 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft', css="""
|
|
| 414 |
label="π‘ Try these science questions:"
|
| 415 |
)
|
| 416 |
|
| 417 |
-
# Footer with
|
| 418 |
-
gr.Markdown(
|
| 419 |
<details>
|
| 420 |
<summary>π§ Technical: Model Fallback Chain (Largest β Smallest)</summary>
|
| 421 |
|
|
@@ -434,7 +424,8 @@ with gr.Blocks(title="Science Chatbot", theme='gradio/soft', css="""
|
|
| 434 |
|
| 435 |
if __name__ == "__main__":
|
| 436 |
logger.info("π Starting Science Chatbot with multi-model fallback...")
|
| 437 |
-
|
|
|
|
| 438 |
# from phi.agent import Agent
|
| 439 |
# from phi.model.groq import Groq
|
| 440 |
# import os
|
|
|
|
| 13 |
import time
|
| 14 |
|
| 15 |
# Set up logging
|
| 16 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 17 |
logger = logging.getLogger(__name__)
|
| 18 |
|
| 19 |
# API Key setup
|
|
|
|
| 32 |
# π― MODEL LIST: Ordered by parameter size (largest β smallest)
|
| 33 |
# Fallback chain: if rate limit (429) or model error, try next model
|
| 34 |
MODEL_CHAIN = [
|
| 35 |
+
{"id": "llama-3.3-70b-versatile", "name": "Llama 3.3 70B", "priority": 1},
|
| 36 |
+
{"id": "qwen/qwen3-32b", "name": "Qwen3 32B", "priority": 2},
|
| 37 |
+
{"id": "meta-llama/llama-4-scout-17b-16e-instruct", "name": "Llama-4 Scout 17B", "priority": 3},
|
| 38 |
+
{"id": "groq/compound", "name": "Groq Compound", "priority": 4},
|
| 39 |
+
{"id": "gemma2-9b-it", "name": "Gemma2 9B", "priority": 5},
|
| 40 |
+
{"id": "llama-3.1-8b-instant", "name": "Llama 3.1 8B Instant", "priority": 6},
|
| 41 |
]
|
| 42 |
|
| 43 |
def bhashini_translate(text: str, from_code: str = "en", to_code: str = "hi") -> dict:
|
|
|
|
| 46 |
return {"status_code": 400, "message": "Input text is empty", "translated_content": None}
|
| 47 |
|
| 48 |
print(f'Translating: "{text[:50]}..." from {from_code} to {to_code}')
|
|
|
|
| 49 |
|
| 50 |
url = 'https://meity-auth.ulcacontrib.org/ulca/apis/v0/model/getModelsPipeline'
|
| 51 |
headers = {
|
|
|
|
| 112 |
],
|
| 113 |
model=Groq(id=model_id, api_key=api_key),
|
| 114 |
markdown=True,
|
| 115 |
+
timeout=60
|
| 116 |
)
|
| 117 |
|
| 118 |
|
| 119 |
+
def generate_with_fallback(prompt: str) -> tuple[str, str]:
|
| 120 |
"""
|
| 121 |
Try models in descending size order.
|
| 122 |
Returns: (response_text, model_used)
|
|
|
|
| 142 |
error_msg = str(e).lower()
|
| 143 |
last_error = e
|
| 144 |
|
| 145 |
+
# Check for errors that warrant fallback to next model
|
| 146 |
should_fallback = any([
|
| 147 |
+
"429" in error_msg,
|
| 148 |
"rate limit" in error_msg,
|
| 149 |
"too many requests" in error_msg,
|
| 150 |
"model_decommissioned" in error_msg,
|
| 151 |
"model_not_found" in error_msg,
|
| 152 |
"invalid_request_error" in error_msg and "model" in error_msg,
|
| 153 |
+
"timeout" in error_msg and priority < len(MODEL_CHAIN)
|
| 154 |
])
|
| 155 |
|
| 156 |
if should_fallback:
|
| 157 |
logger.warning(f"β οΈ {model_name} failed ({type(e).__name__}), falling back to next model...")
|
| 158 |
+
time.sleep(0.5 * priority)
|
| 159 |
continue
|
| 160 |
else:
|
|
|
|
| 161 |
logger.error(f"β {model_name} failed with non-fallback error: {e}")
|
| 162 |
break
|
| 163 |
|
|
|
|
| 165 |
error_details = f"{type(last_error).__name__}: {str(last_error)}" if last_error else "Unknown error"
|
| 166 |
logger.error(f"π₯ All models failed. Last error: {error_details}")
|
| 167 |
|
|
|
|
| 168 |
fallback_response = (
|
| 169 |
"β οΈ I'm experiencing high demand right now. Please try again in a moment.\n\n"
|
| 170 |
f"*Technical note: Unable to connect to available models ({error_details})*"
|
|
|
|
| 189 |
documents = [doc["text"] for doc in documents]
|
| 190 |
|
| 191 |
# Re-rank documents using cross-encoder
|
| 192 |
+
model_name = 'BAAI/bge-reranker-base' if cross_encoder_choice == '(ACCURATE) BGE reranker' else 'cross-encoder/ms-marco-MiniLM-L-6-v2'
|
| 193 |
+
cross_encoder_model = CrossEncoder(model_name)
|
|
|
|
|
|
|
| 194 |
query_doc_pair = [[query, doc] for doc in documents]
|
| 195 |
cross_scores = cross_encoder_model.predict(query_doc_pair)
|
| 196 |
sim_scores_argsort = list(reversed(np.argsort(cross_scores)))
|
|
|
|
| 210 |
# Create full prompt
|
| 211 |
full_prompt = f"{history_context}{context}Question: {query}\n\nPlease answer the question using the context provided above. If the context doesn't contain relevant information, use your general knowledge about 10th-grade science topics."
|
| 212 |
|
| 213 |
+
# Generate response with model fallback chain
|
| 214 |
response_text, model_used = generate_with_fallback(full_prompt)
|
| 215 |
|
| 216 |
# Add model attribution to response
|
|
|
|
| 227 |
return f"Error: {str(e)}", [], "error"
|
| 228 |
|
| 229 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
def translate_text(selected_language, history):
|
| 231 |
"""Translate the last response in history to the selected language."""
|
| 232 |
iso_language_codes = {
|
|
|
|
| 263 |
border-radius: 12px; font-size: 0.8em; font-weight: 500;
|
| 264 |
display: inline-block; margin: 4px 0;
|
| 265 |
}
|
| 266 |
+
.gradio-container { max-width: 1200px !important; margin: auto; }
|
| 267 |
""") as demo:
|
| 268 |
|
| 269 |
# Header section
|
|
|
|
| 276 |
try:
|
| 277 |
gr.Image(value='logo.png', height=180, width=180, show_label=False)
|
| 278 |
except:
|
| 279 |
+
gr.HTML("<div style='height: 180px; width: 180px; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: bold; font-size: 3em;'>π€</div>")
|
| 280 |
|
| 281 |
+
# Model status indicator (Markdown component)
|
| 282 |
+
model_status = gr.Markdown(value="π *Ready to answer with best available model*")
|
| 283 |
|
| 284 |
# Chatbot
|
| 285 |
chatbot = gr.Chatbot(
|
|
|
|
| 329 |
|
| 330 |
prompt_html = gr.HTML(label="π Source Documents")
|
| 331 |
|
| 332 |
+
# Event handler function - NO .update() calls, return values instead
|
| 333 |
def update_chat_and_translate(message, history, cross_encoder_choice, selected_language):
|
| 334 |
+
"""Handler that returns all outputs in correct order"""
|
|
|
|
| 335 |
|
| 336 |
+
# Handle empty message
|
| 337 |
+
if not message.strip():
|
| 338 |
+
return "", history, "", "", "π *Ready to answer with best available model*"
|
| 339 |
|
| 340 |
+
# Generate response with fallback chain
|
| 341 |
+
response, documents, model_used = retrieve_and_generate_response(
|
| 342 |
+
message, cross_encoder_choice, history
|
| 343 |
+
)
|
| 344 |
|
| 345 |
+
# Prepare markdown status as STRING (return value, not .update())
|
| 346 |
status_emoji = "β
" if model_used != "fallback" else "β οΈ"
|
| 347 |
+
model_status_text = f"{status_emoji} *Responded using: **{model_used}***"
|
| 348 |
|
| 349 |
+
# Add to chat history
|
| 350 |
history.append([message, response])
|
| 351 |
|
| 352 |
# Translate response
|
| 353 |
translated_text = translate_text(selected_language, history)
|
| 354 |
|
| 355 |
+
# Render HTML template
|
| 356 |
prompt_html_content = template_html.render(
|
| 357 |
documents=documents,
|
| 358 |
query=message,
|
|
|
|
| 360 |
timestamp=time.strftime("%H:%M:%S")
|
| 361 |
)
|
| 362 |
|
| 363 |
+
# RETURN ALL VALUES IN EXACT ORDER MATCHING outputs=[...]
|
| 364 |
+
# outputs=[msg, chatbot, translated_textbox, prompt_html, model_status]
|
| 365 |
+
return (
|
| 366 |
+
"", # 1. msg: clear input textbox
|
| 367 |
+
history, # 2. chatbot: updated conversation history
|
| 368 |
+
translated_text, # 3. translated_textbox: translation result
|
| 369 |
+
prompt_html_content, # 4. prompt_html: rendered source documents
|
| 370 |
+
model_status_text # 5. model_status: markdown status (STRING)
|
| 371 |
+
)
|
| 372 |
|
| 373 |
+
# Bind events - outputs order must match return tuple order
|
| 374 |
msg.submit(
|
| 375 |
update_chat_and_translate,
|
| 376 |
+
inputs=[msg, chatbot, cross_encoder, language_dropdown],
|
| 377 |
+
outputs=[msg, chatbot, translated_textbox, prompt_html, model_status]
|
| 378 |
)
|
| 379 |
+
|
| 380 |
submit_btn.click(
|
| 381 |
update_chat_and_translate,
|
| 382 |
+
inputs=[msg, chatbot, cross_encoder, language_dropdown],
|
| 383 |
+
outputs=[msg, chatbot, translated_textbox, prompt_html, model_status]
|
| 384 |
)
|
| 385 |
|
| 386 |
+
# Clear button - return tuple matching its outputs order: [chatbot, msg, translated_textbox, prompt_html, model_status]
|
| 387 |
clear = gr.Button("ποΈ Clear Conversation", variant="secondary")
|
| 388 |
clear.click(
|
| 389 |
lambda: ([], "", "", "", "π *Ready to answer with best available model*"),
|
| 390 |
outputs=[chatbot, msg, translated_textbox, prompt_html, model_status]
|
| 391 |
)
|
| 392 |
|
| 393 |
+
# Example questions
|
| 394 |
gr.Examples(
|
| 395 |
examples=[
|
| 396 |
"What is the difference between metals and non-metals?",
|
|
|
|
| 404 |
label="π‘ Try these science questions:"
|
| 405 |
)
|
| 406 |
|
| 407 |
+
# Footer with technical details (collapsible)
|
| 408 |
+
gr.Markdown("""
|
| 409 |
<details>
|
| 410 |
<summary>π§ Technical: Model Fallback Chain (Largest β Smallest)</summary>
|
| 411 |
|
|
|
|
| 424 |
|
| 425 |
if __name__ == "__main__":
|
| 426 |
logger.info("π Starting Science Chatbot with multi-model fallback...")
|
| 427 |
+
logger.info(f"Model chain: {[m['name'] for m in MODEL_CHAIN]}")
|
| 428 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, show_error=True)
|
| 429 |
# from phi.agent import Agent
|
| 430 |
# from phi.model.groq import Groq
|
| 431 |
# import os
|