Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,7 @@ import torch.nn as nn
|
|
| 7 |
import torch.nn.functional as F
|
| 8 |
import torch.optim as optim
|
| 9 |
import gradio as gr
|
| 10 |
-
from datetime import datetime
|
| 11 |
|
| 12 |
# Removed: import kagglehub, pandas, tqdm
|
| 13 |
|
|
@@ -192,6 +192,7 @@ def save_checkpoint(model, optimizer, voc, path):
|
|
| 192 |
|
| 193 |
|
| 194 |
def perform_web_search(query):
|
|
|
|
| 195 |
print(f"**Web Search Triggered for:** '{query}'")
|
| 196 |
query_lower = query.lower()
|
| 197 |
if "capital of singapore" in query_lower:
|
|
@@ -298,8 +299,10 @@ def generate_response(prompt, search_context="", conversation_context=""):
|
|
| 298 |
text = re.sub(r"\s+([,.!?;:])", r"\1", text)
|
| 299 |
text = text.strip()
|
| 300 |
|
|
|
|
|
|
|
| 301 |
if not text or check_repetition(text):
|
| 302 |
-
return "I
|
| 303 |
return text
|
| 304 |
|
| 305 |
|
|
@@ -312,6 +315,7 @@ def process_feedback(history, rating):
|
|
| 312 |
last_user_message, last_ai_response = history[-1][0], history[-1][1]
|
| 313 |
|
| 314 |
file_context_text = ""
|
|
|
|
| 315 |
file_match = re.search(r"\(Attached: (.*?)\)", last_user_message)
|
| 316 |
if file_match:
|
| 317 |
file_name = file_match.group(1)
|
|
@@ -321,12 +325,9 @@ def process_feedback(history, rating):
|
|
| 321 |
clean_user_message = re.sub(r'\(Attached:.*?\)', '', clean_user_message).strip()
|
| 322 |
clean_user_message = re.sub(r'\[IMAGE_PREVIEW_URL:.*?]', '', clean_user_message).strip()
|
| 323 |
|
|
|
|
| 324 |
search_context_text = ""
|
| 325 |
-
|
| 326 |
-
clean_user_message.lower().startswith(q) for q in ["what is", "who is", "how to"]
|
| 327 |
-
):
|
| 328 |
-
search_context_text = perform_web_search(clean_user_message)
|
| 329 |
-
|
| 330 |
print(f"\n--- FEEDBACK RECEIVED ---")
|
| 331 |
print(f"Rating: {rating}")
|
| 332 |
print(f"User Prompt: '{last_user_message}'")
|
|
@@ -388,23 +389,25 @@ def on_submit(user_message, history, uploaded_file, web_search_enabled):
|
|
| 388 |
|
| 389 |
|
| 390 |
conversation_context = ""
|
| 391 |
-
# In the UI flow, the user message is already in history, so we use history[:-1] as context
|
| 392 |
-
# If this function is called directly (e.g., from the API wrapper), history might not be updated yet.
|
| 393 |
-
# We rely on the calling function (like `chat_submit_api` or the `then` block) to ensure history is ready.
|
| 394 |
context_history = history[:-1] if not is_instruction else history
|
| 395 |
if context_history and isinstance(context_history[-1], list) and len(context_history[-1]) == 2 and isinstance(context_history[-1][1], str):
|
| 396 |
last_user, last_ai = context_history[-1][0], context_history[-1][1]
|
| 397 |
conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"
|
| 398 |
|
| 399 |
search_context_text = ""
|
| 400 |
-
|
|
|
|
| 401 |
clean_user_message.lower().startswith(q) for q in ["what is", "who is", "how to"]
|
| 402 |
-
)
|
|
|
|
| 403 |
if search_needed:
|
| 404 |
search_context_text = perform_web_search(clean_user_message)
|
| 405 |
|
| 406 |
display_search_result = f"π **Search Result Used:** {search_context_text}" if search_context_text else "π **No Search Performed or Result Found.**"
|
| 407 |
|
|
|
|
|
|
|
|
|
|
| 408 |
|
| 409 |
final_model_prompt = f"{file_context} {model_prompt}" if file_context else model_prompt
|
| 410 |
reply = generate_response(
|
|
@@ -523,13 +526,9 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 523 |
clear.click(lambda: [], None, chat, queue=False)
|
| 524 |
|
| 525 |
def feedback_good_wrapper(history_state):
|
| 526 |
-
# NOTE: This wrapper in app.py seems to have been intended for a UI button
|
| 527 |
-
# but the logic for online_update is now handled in process_feedback/feedback_good.
|
| 528 |
-
# It's kept for completeness but the UI-based feedback is not fully defined here.
|
| 529 |
return process_feedback(history_state, "Good Response")
|
| 530 |
|
| 531 |
def feedback_bad_wrapper(history_state):
|
| 532 |
-
# NOTE: Same as above.
|
| 533 |
return process_feedback(history_state, "Bad Response")
|
| 534 |
|
| 535 |
# --- Public API Endpoints (from app.py and app_combined.py) ---
|
|
@@ -602,6 +601,4 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 602 |
|
| 603 |
if __name__ == "__main__":
|
| 604 |
# Using the demo.queue().launch() from app.py
|
| 605 |
-
demo.queue(api_open=True).launch(ssr_mode=False)
|
| 606 |
-
# The server_name and server_port from app_combined.py's launch call are not needed
|
| 607 |
-
# for the default Gradio launch, but the combined app will run on a similar port.
|
|
|
|
| 7 |
import torch.nn.functional as F
|
| 8 |
import torch.optim as optim
|
| 9 |
import gradio as gr
|
| 10 |
+
from datetime import datetime
|
| 11 |
|
| 12 |
# Removed: import kagglehub, pandas, tqdm
|
| 13 |
|
|
|
|
| 192 |
|
| 193 |
|
| 194 |
def perform_web_search(query):
|
| 195 |
+
"""MOCK WEB SEARCH: Only returns results for hard-coded queries."""
|
| 196 |
print(f"**Web Search Triggered for:** '{query}'")
|
| 197 |
query_lower = query.lower()
|
| 198 |
if "capital of singapore" in query_lower:
|
|
|
|
| 299 |
text = re.sub(r"\s+([,.!?;:])", r"\1", text)
|
| 300 |
text = text.strip()
|
| 301 |
|
| 302 |
+
# CRITICAL FIX 1: If the generation fails or is repetitive, return a default silly message
|
| 303 |
+
# instead of the "I couldn't generate a stable response" placeholder.
|
| 304 |
if not text or check_repetition(text):
|
| 305 |
+
return "I got lost in the data tubes! Perhaps ask me something a little sillier? π€ͺ"
|
| 306 |
return text
|
| 307 |
|
| 308 |
|
|
|
|
| 315 |
last_user_message, last_ai_response = history[-1][0], history[-1][1]
|
| 316 |
|
| 317 |
file_context_text = ""
|
| 318 |
+
# Note: The JS client injects file names using special markers, which are needed to determine context.
|
| 319 |
file_match = re.search(r"\(Attached: (.*?)\)", last_user_message)
|
| 320 |
if file_match:
|
| 321 |
file_name = file_match.group(1)
|
|
|
|
| 325 |
clean_user_message = re.sub(r'\(Attached:.*?\)', '', clean_user_message).strip()
|
| 326 |
clean_user_message = re.sub(r'\[IMAGE_PREVIEW_URL:.*?]', '', clean_user_message).strip()
|
| 327 |
|
| 328 |
+
# We assume search_context_text is empty here because the API for feedback doesn't pass search_enabled
|
| 329 |
search_context_text = ""
|
| 330 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
print(f"\n--- FEEDBACK RECEIVED ---")
|
| 332 |
print(f"Rating: {rating}")
|
| 333 |
print(f"User Prompt: '{last_user_message}'")
|
|
|
|
| 389 |
|
| 390 |
|
| 391 |
conversation_context = ""
|
|
|
|
|
|
|
|
|
|
| 392 |
context_history = history[:-1] if not is_instruction else history
|
| 393 |
if context_history and isinstance(context_history[-1], list) and len(context_history[-1]) == 2 and isinstance(context_history[-1][1], str):
|
| 394 |
last_user, last_ai = context_history[-1][0], context_history[-1][1]
|
| 395 |
conversation_context = f"User: {to_ascii(last_user)} Response: {to_ascii(last_ai)}"
|
| 396 |
|
| 397 |
search_context_text = ""
|
| 398 |
+
# CRITICAL FIX 2: Only check for search need IF the web search is explicitly enabled (from the checkbox).
|
| 399 |
+
search_needed = bool(web_search_enabled) and ("?" in clean_user_message or any(
|
| 400 |
clean_user_message.lower().startswith(q) for q in ["what is", "who is", "how to"]
|
| 401 |
+
))
|
| 402 |
+
|
| 403 |
if search_needed:
|
| 404 |
search_context_text = perform_web_search(clean_user_message)
|
| 405 |
|
| 406 |
display_search_result = f"π **Search Result Used:** {search_context_text}" if search_context_text else "π **No Search Performed or Result Found.**"
|
| 407 |
|
| 408 |
+
# If search was NOT enabled, overwrite the message to be definitively "No Search Performed."
|
| 409 |
+
if not web_search_enabled:
|
| 410 |
+
display_search_result = "π **Web Search Disabled by Menu.**"
|
| 411 |
|
| 412 |
final_model_prompt = f"{file_context} {model_prompt}" if file_context else model_prompt
|
| 413 |
reply = generate_response(
|
|
|
|
| 526 |
clear.click(lambda: [], None, chat, queue=False)
|
| 527 |
|
| 528 |
def feedback_good_wrapper(history_state):
|
|
|
|
|
|
|
|
|
|
| 529 |
return process_feedback(history_state, "Good Response")
|
| 530 |
|
| 531 |
def feedback_bad_wrapper(history_state):
|
|
|
|
| 532 |
return process_feedback(history_state, "Bad Response")
|
| 533 |
|
| 534 |
# --- Public API Endpoints (from app.py and app_combined.py) ---
|
|
|
|
| 601 |
|
| 602 |
if __name__ == "__main__":
|
| 603 |
# Using the demo.queue().launch() from app.py
|
| 604 |
+
demo.queue(api_open=True).launch(ssr_mode=False)
|
|
|
|
|
|