Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -204,6 +204,7 @@ def save_checkpoint(model, optimizer, voc, path):
|
|
| 204 |
# --------------------------------
|
| 205 |
|
| 206 |
|
|
|
|
| 207 |
def perform_web_search(query):
|
| 208 |
"""MOCK WEB SEARCH: Only returns results for hard-coded queries."""
|
| 209 |
print(f"**Web Search Triggered for:** '{query}'")
|
|
@@ -228,6 +229,7 @@ def online_update(model, input_sentence, target_response, max_seq_len, search_co
|
|
| 228 |
print("Skipping online update: Target response is corrupted or highly repetitive.")
|
| 229 |
return
|
| 230 |
|
|
|
|
| 231 |
prefix_search = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""
|
| 232 |
prefix_file = f"Based on the attached file: {to_ascii(file_context)}. " if file_context else ""
|
| 233 |
context_prefix = f"{conversation_context} " if conversation_context else ""
|
|
@@ -269,6 +271,7 @@ def online_update(model, input_sentence, target_response, max_seq_len, search_co
|
|
| 269 |
|
| 270 |
def generate_response(prompt, search_context="", conversation_context=""):
|
| 271 |
"""Generates a response using the GPT model with Top-K sampling."""
|
|
|
|
| 272 |
prefix_text = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""
|
| 273 |
context_prefix = f"{conversation_context} " if conversation_context else ""
|
| 274 |
|
|
@@ -364,7 +367,7 @@ def process_feedback(history, rating):
|
|
| 364 |
clean_user_message,
|
| 365 |
last_ai_response,
|
| 366 |
max_length,
|
| 367 |
-
search_context=search_context_text,
|
| 368 |
file_context=file_context_text,
|
| 369 |
conversation_context=conversation_context
|
| 370 |
)
|
|
@@ -417,7 +420,7 @@ def on_submit(user_message, history, uploaded_file, web_search_enabled):
|
|
| 417 |
# -----------------------------------------------------
|
| 418 |
|
| 419 |
search_context_text = ""
|
| 420 |
-
#
|
| 421 |
search_needed = bool(web_search_enabled) and ("?" in clean_user_message or any(
|
| 422 |
clean_user_message.lower().startswith(q) for q in ["what is", "who is", "how to"]
|
| 423 |
))
|
|
@@ -427,14 +430,14 @@ def on_submit(user_message, history, uploaded_file, web_search_enabled):
|
|
| 427 |
|
| 428 |
display_search_result = f"๐ **Search Result Used:** {search_context_text}" if search_context_text else "๐ **No Search Performed or Result Found.**"
|
| 429 |
|
| 430 |
-
# If search was NOT enabled, overwrite the message to be definitively "No Search Performed."
|
| 431 |
if not web_search_enabled:
|
| 432 |
display_search_result = "๐ **Web Search Disabled by Menu.**"
|
| 433 |
|
| 434 |
final_model_prompt = f"{file_context} {model_prompt}" if file_context else model_prompt
|
| 435 |
reply = generate_response(
|
| 436 |
final_model_prompt,
|
| 437 |
-
search_context=search_context_text,
|
| 438 |
conversation_context=conversation_context
|
| 439 |
)
|
| 440 |
|
|
@@ -492,16 +495,20 @@ def feedback_bad_with_prompt(history_state, suggested_prompt):
|
|
| 492 |
# --- Gradio UI Definition ---
|
| 493 |
|
| 494 |
with gr.Blocks(title="OkemoLLM") as demo:
|
| 495 |
-
|
| 496 |
-
|
|
|
|
|
|
|
| 497 |
chat = gr.Chatbot(height=500)
|
| 498 |
|
| 499 |
with gr.Row():
|
| 500 |
with gr.Column(scale=4):
|
| 501 |
msg = gr.Textbox(placeholder="Type your messageโฆ", label="Message", elem_id="okemo-input-textbox")
|
| 502 |
uploaded_file_state = gr.File(label="Uploaded File", visible=False, elem_id="uploaded_file_state")
|
|
|
|
| 503 |
web_search_enabled_state = gr.Checkbox(label="Enable Web Search", value=True)
|
| 504 |
|
|
|
|
| 505 |
clear = gr.Button("Clear")
|
| 506 |
send_trigger = gr.Button(visible=False, elem_id="okemo-send-button-trigger")
|
| 507 |
|
|
@@ -511,7 +518,7 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 511 |
updated_history = history + [[message, None]]
|
| 512 |
return updated_history, "..."
|
| 513 |
|
| 514 |
-
# UI Chat Flow:
|
| 515 |
msg.submit(
|
| 516 |
add_user_message_to_history,
|
| 517 |
[msg, chat, uploaded_file_state, web_search_enabled_state],
|
|
@@ -559,7 +566,8 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 559 |
gr.Textbox(label="Message"),
|
| 560 |
gr.State(value=[]),
|
| 561 |
gr.File(label="Uploaded File"),
|
| 562 |
-
|
|
|
|
| 563 |
],
|
| 564 |
outputs=[
|
| 565 |
gr.State(value=[]),
|
|
@@ -609,4 +617,4 @@ with gr.Blocks(title="OkemoLLM") as demo:
|
|
| 609 |
|
| 610 |
if __name__ == "__main__":
|
| 611 |
# Using the demo.queue().launch() from app.py
|
| 612 |
-
demo.queue(api_open=True).launch(ssr_mode=False)
|
|
|
|
| 204 |
# --------------------------------
|
| 205 |
|
| 206 |
|
| 207 |
+
# REINTRODUCING: perform_web_search function
|
| 208 |
def perform_web_search(query):
|
| 209 |
"""MOCK WEB SEARCH: Only returns results for hard-coded queries."""
|
| 210 |
print(f"**Web Search Triggered for:** '{query}'")
|
|
|
|
| 229 |
print("Skipping online update: Target response is corrupted or highly repetitive.")
|
| 230 |
return
|
| 231 |
|
| 232 |
+
# RE-ADDED search prefix logic
|
| 233 |
prefix_search = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""
|
| 234 |
prefix_file = f"Based on the attached file: {to_ascii(file_context)}. " if file_context else ""
|
| 235 |
context_prefix = f"{conversation_context} " if conversation_context else ""
|
|
|
|
| 271 |
|
| 272 |
def generate_response(prompt, search_context="", conversation_context=""):
|
| 273 |
"""Generates a response using the GPT model with Top-K sampling."""
|
| 274 |
+
# RE-ADDED search prefix logic
|
| 275 |
prefix_text = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""
|
| 276 |
context_prefix = f"{conversation_context} " if conversation_context else ""
|
| 277 |
|
|
|
|
| 367 |
clean_user_message,
|
| 368 |
last_ai_response,
|
| 369 |
max_length,
|
| 370 |
+
search_context=search_context_text,
|
| 371 |
file_context=file_context_text,
|
| 372 |
conversation_context=conversation_context
|
| 373 |
)
|
|
|
|
| 420 |
# -----------------------------------------------------
|
| 421 |
|
| 422 |
search_context_text = ""
|
| 423 |
+
# RE-ADDED: Check for search need IF the web search is explicitly enabled (from the checkbox).
|
| 424 |
search_needed = bool(web_search_enabled) and ("?" in clean_user_message or any(
|
| 425 |
clean_user_message.lower().startswith(q) for q in ["what is", "who is", "how to"]
|
| 426 |
))
|
|
|
|
| 430 |
|
| 431 |
display_search_result = f"๐ **Search Result Used:** {search_context_text}" if search_context_text else "๐ **No Search Performed or Result Found.**"
|
| 432 |
|
| 433 |
+
# RE-ADDED: If search was NOT enabled, overwrite the message to be definitively "No Search Performed."
|
| 434 |
if not web_search_enabled:
|
| 435 |
display_search_result = "๐ **Web Search Disabled by Menu.**"
|
| 436 |
|
| 437 |
final_model_prompt = f"{file_context} {model_prompt}" if file_context else model_prompt
|
| 438 |
reply = generate_response(
|
| 439 |
final_model_prompt,
|
| 440 |
+
search_context=search_context_text,
|
| 441 |
conversation_context=conversation_context
|
| 442 |
)
|
| 443 |
|
|
|
|
| 495 |
# --- Gradio UI Definition ---
|
| 496 |
|
| 497 |
with gr.Blocks(title="OkemoLLM") as demo:
|
| 498 |
+
# RESTORED TITLE
|
| 499 |
+
gr.Markdown("# OkemoLLM (Online Learning & Web Search Enabled with 1-Turn Context) ๐")
|
| 500 |
+
# RESTORED STATUS
|
| 501 |
+
search_status_display = gr.Markdown("๐ Search status will appear here.")
|
| 502 |
chat = gr.Chatbot(height=500)
|
| 503 |
|
| 504 |
with gr.Row():
|
| 505 |
with gr.Column(scale=4):
|
| 506 |
msg = gr.Textbox(placeholder="Type your messageโฆ", label="Message", elem_id="okemo-input-textbox")
|
| 507 |
uploaded_file_state = gr.File(label="Uploaded File", visible=False, elem_id="uploaded_file_state")
|
| 508 |
+
# RESTORED CHECKBOX
|
| 509 |
web_search_enabled_state = gr.Checkbox(label="Enable Web Search", value=True)
|
| 510 |
|
| 511 |
+
|
| 512 |
clear = gr.Button("Clear")
|
| 513 |
send_trigger = gr.Button(visible=False, elem_id="okemo-send-button-trigger")
|
| 514 |
|
|
|
|
| 518 |
updated_history = history + [[message, None]]
|
| 519 |
return updated_history, "..."
|
| 520 |
|
| 521 |
+
# UI Chat Flow: All elements restored
|
| 522 |
msg.submit(
|
| 523 |
add_user_message_to_history,
|
| 524 |
[msg, chat, uploaded_file_state, web_search_enabled_state],
|
|
|
|
| 566 |
gr.Textbox(label="Message"),
|
| 567 |
gr.State(value=[]),
|
| 568 |
gr.File(label="Uploaded File"),
|
| 569 |
+
# RESTORED CHECKBOX
|
| 570 |
+
gr.Checkbox(label="Web Search Enabled", value=True),
|
| 571 |
],
|
| 572 |
outputs=[
|
| 573 |
gr.State(value=[]),
|
|
|
|
| 617 |
|
| 618 |
if __name__ == "__main__":
|
| 619 |
# Using the demo.queue().launch() from app.py
|
| 620 |
+
demo.queue(api_open=True).launch(ssr_mode=False)
|