Spaces:
Runtime error
Runtime error
Update app.py
Browse files203 unmodified lines20420520620720820918 unmodified lines22822923023123223335 unmodified lines26927027127227327489 unmodified lines36436536636736836937046 unmodified lines4174184194204214224233 unmodified lines42742842943043143243343443543643743843944051 unmodified lines4924934944954964974984995005015025035045055065073 unmodified lines51151251351451551651741 unmodified lines55956056156256356456543 unmodified lines609610611612203 unmodified lines# --------------------------------def perform_web_search(query):"""MOCK WEB SEARCH: Only returns results for hard-coded queries."""print(f"**Web Search Triggered for:** '{query}'")18 unmodified linesprint("Skipping online update: Target response is corrupted or highly repetitive.")returnprefix_search = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""prefix_file = f"Based on the attached file: {to_ascii(file_context)}. " if file_context else ""context_prefix = f"{conversation_context} " if conversation_context else ""35 unmodified linesdef generate_response(prompt, search_context="", conversation_context=""):"""Generates a response using the GPT model with Top-K sampling."""prefix_text = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""context_prefix = f"{conversation_context} " if conversation_context else ""89 unmodified linesclean_user_message,last_ai_response,max_length,search_context=search_context_text,file_context=file_context_text,conversation_context=conversation_context)46 unmodified lines# -----------------------------------------------------search_context_text = ""# CRITICAL FIX 2: Only check for search need IF the web search is explicitly enabled (from the checkbox).search_needed = bool(web_search_enabled) and ("?" in clean_user_message or any(clean_user_message.lower().startswith(q) for q in ["what is", "who is", "how to"]))3 unmodified linesdisplay_search_result = f"🔍 **Search Result Used:** {search_context_text}" if search_context_text else "🔍 **No Search Performed or Result Found.**"# If search was NOT enabled, overwrite the message to be definitively "No Search Performed."if not web_search_enabled:display_search_result = "🔍 **Web Search Disabled by Menu.**"final_model_prompt = f"{file_context} {model_prompt}" if file_context else model_promptreply = generate_response(final_model_prompt,search_context=search_context_text,conversation_context=conversation_context)51 unmodified lines# --- Gradio UI Definition ---with gr.Blocks(title="OkemoLLM") as demo:gr.Markdown("# OkemoLLM (Online Learning & Web Search Enabled with 1-Turn Context) 🌐")search_status_display = gr.Markdown("🔍 Search status will appear here.")chat = gr.Chatbot(height=500)with gr.Row():with gr.Column(scale=4):msg = gr.Textbox(placeholder="Type your message…", label="Message", elem_id="okemo-input-textbox")uploaded_file_state = gr.File(label="Uploaded File", visible=False, elem_id="uploaded_file_state")web_search_enabled_state = gr.Checkbox(label="Enable Web Search", value=True)clear = gr.Button("Clear")send_trigger = gr.Button(visible=False, elem_id="okemo-send-button-trigger")3 unmodified linesupdated_history = history + [[message, None]]return updated_history, "..."# UI Chat Flow: Add message to history -> Then process and generate responsemsg.submit(add_user_message_to_history,[msg, chat, uploaded_file_state, web_search_enabled_state],41 unmodified linesgr.Textbox(label="Message"),gr.State(value=[]),gr.File(label="Uploaded File"),gr.Checkbox(label="Web Search Enabled"),],outputs=[gr.State(value=[]),43 unmodified linesif __name__ == "__main__":# Using the demo.queue().launch() from app.pydemo.queue(api_open=True).launch(ssr_mode=False)203 unmodified lines20420520620720820921018 unmodified lines22923023123223323423535 unmodified lines27127227327427527627789 unmodified lines36736836937037137237346 unmodified lines4204214224234244254263 unmodified lines43043143243343443543643743843944044144244351 unmodified lines4954964974984995005015025035045055065075085095105115125135143 unmodified lines51851952052152252352441 unmodified lines56656756856957057157257343 unmodified lines617618619620203 unmodified lines# --------------------------------# REINTRODUCING: perform_web_search functiondef perform_web_search(query):"""MOCK WEB SEARCH: Only returns results for hard-coded queries."""print(f"**Web Search Triggered for:** '{query}'")18 unmodified linesprint("Skipping online update: Target response is corrupted or highly repetitive.")return# RE-ADDED search prefix logicprefix_search = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""prefix_file = f"Based on the attached file: {to_ascii(file_context)}. " if file_context else ""context_prefix = f"{conversation_context} " if conversation_context else ""35 unmodified linesdef generate_response(prompt, search_context="", conversation_context=""):"""Generates a response using the GPT model with Top-K sampling."""# RE-ADDED search prefix logicprefix_text = f"According to a search result: {to_ascii(search_context)}. " if search_context else ""context_prefix = f"{conversation_context} " if conversation_context else ""89 unmodified linesclean_user_message,last_ai_response,max_length,search_context=search_context_text,file_context=file_context_text,conversation_context=conversation_context)46 unmodified lines# -----------------------------------------------------search_context_text = ""# RE-ADDED: Check for search need IF the web search is explicitly enabled (from the checkbox).search_needed = bool(web_search_enabled) and ("?" in clean_user_message or any(clean_user_message.lower().startswith(q) for q in ["what is", "who is", "how to"]))3 unmodified linesdisplay_search_result = f"🔍 **Search Result Used:** {search_context_text}" if search_context_text else "🔍 **No Search Performed or Result Found.**"# RE-ADDED: If search was NOT enabled, overwrite the message to be definitively "No Search Performed."if not web_search_enabled:display_search_result = "🔍 **Web Search Disabled by Menu.**"final_model_prompt = f"{file_context} {model_prompt}" if file_context else model_promptreply = generate_response(final_model_prompt,search_context=search_context_text,conversation_context=conversation_context)51 unmodified lines# --- Gradio UI Definition ---with gr.Blocks(title="OkemoLLM") as demo:# RESTORED TITLEgr.Markdown("# OkemoLLM (Online Learning & Web Search Enabled with 1-Turn Context) 🌐")# RESTORED STATUSsearch_status_display = gr.Markdown("🔍 Search status will appear here.")chat = gr.Chatbot(height=500)with gr.Row():with gr.Column(scale=4):msg = gr.Textbox(placeholder="Type your message…", label="Message", elem_id="okemo-input-textbox")uploaded_file_state = gr.File(label="Uploaded File", visible=False, elem_id="uploaded_file_state")# RESTORED CHECKBOXweb_search_enabled_state = gr.Checkbox(label="Enable Web Search", value=True)clear = gr.Button("Clear")send_trigger = gr.Button(visible=False, elem_id="okemo-send-button-trigger")3 unmodified linesupdated_history = history + [[message, None]]return updated_history, "..."# UI Chat Flow: All elements restoredmsg.submit(add_user_message_to_history,[msg, chat, uploaded_file_state, web_search_enabled_state],41 unmodified linesgr.Textbox(label="Message"),gr.State(value=[]),gr.File(label="Uploaded File"),# RESTORED CHECKBOXgr.Checkbox(label="Web Search Enabled", value=True),],outputs=[gr.State(value=[]),43 unmodified linesif __name__ == "__main__":# Using the demo.queue().launch() from app.pydemo.queue(api_open=True).launch(ssr_mode=False)
app.py+17-9