Spaces:
Runtime error
Runtime error
Update app.py
Browse files8 unmodified lines910111213141516171819202132 unmodified lines5455565758596061626364656667686970717273747576777879808182832 unmodified lines86878889909192939495969798991001011021031041051061071081091102 unmodified lines1131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451468 unmodified lines# --- CSS for a professional chatbot look ---CSS = """body, .gradio-container { font-family: 'Inter', sans-serif; }.gradio-container { max-width: 800px !important; margin: auto !important; padding-top: 20px !important;}h1 { text-align: center; font-weight: 700; font-size: 2.5em; color: #1E293B; }.sub-header { text-align: center; color: #475569; margin-bottom: 20px; font-size: 1.1em; }.accordion { border: none !important; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1) !important; }#chatbot { box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1) !important; }.message-bubble-container > .message-bubble { box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.05) !important; }.message-bubble.user { background: #2563EB !important; color: white; }.message-bubble.bot { background: #FFFFFF !important; color: #334155; border: 1px solid #E2E8F0; }footer { display: none !important; }32 unmodified linesinitialization_status = gr.Markdown(visible=False)chatbot = gr.Chatbot([],elem_id="chatbot",label="Research Agent",bubble_full_width=False,height=500,visible=False,render=False # We will render it manually later)# Use the modern 'messages' type for the chatbot statechat_history_state = gr.State([])chat_input = gr.Textbox(placeholder="Enter your research topic...", interactive=False, visible=False, render=False)# Agent state can be: "INITIAL", "CLARIFYING", "GENERATING"agent_state = gr.State("INITIAL")initial_topic_state = gr.State("")def handle_initialization(google_key, tavily_key):init_status = initialize_models(google_key, tavily_key)initial_message = {"role": "assistant", "content": "Agent initialized. Please enter your research topic to begin."}return {initialization_status: gr.update(value=f"**Status:** {init_status}", visible=True),chatbot: gr.update(visible=True, value=[initial_message]),chat_history_state: [initial_message],chat_input: gr.update(interactive=True, visible=True),settings_accordion: gr.update(open=False)}2 unmodified lineshistory.append({"role": "user", "content": user_input})if current_agent_state == "INITIAL":# 1. User provides the initial topicnew_agent_state = "CLARIFYING"new_topic_state = user_inputhistory.append({"role": "assistant", "content": "Thinking..."})yield history, history, new_agent_state, new_topic_state, gr.update(interactive=False)questions = get_clarifying_questions(planner_model, user_input)history[-1] = {"role": "assistant", "content": "I can do that. To give you the best report, could you answer these questions for me?\n\n" + questions}yield history, history, new_agent_state, new_topic_state, gr.update(interactive=True)elif current_agent_state == "CLARIFYING":# 2. User provides answers to clarifying questionsnew_agent_state = "GENERATING"new_topic_state = topic_statehistory.append({"role": "assistant", "content": "Generating full report..."})yield history, history, new_agent_state, new_topic_state, gr.update(interactive=False)try:plan = research_and_plan(config, planner_model, tavily_client, new_topic_state, user_input)report_generator = write_report_stream(config, writer_model, tavily_client, embedding_model, reranker, plan)status_updates = ""2 unmodified linesfinal_report_md = update if "Report Generation Complete" not in update else final_report_mdstatus_updates += updatehistory[-1] = {"role": "assistant", "content": status_updates}yield history, history, new_agent_state, new_topic_state, gr.update(interactive=False)history.append({"role": "assistant", "content": final_report_md})new_agent_state = "INITIAL"new_topic_state = ""yield history, history, new_agent_state, new_topic_state, gr.update(interactive=True)except Exception as e:error_message = f"An error occurred: {str(e)}"history.append({"role": "assistant", "content": error_message})new_agent_state = "INITIAL"new_topic_state = ""yield history, history, new_agent_state, new_topic_state, gr.update(interactive=True)# Manually render components to control orderchatbot.render()chat_input.render()init_button.click(fn=handle_initialization,inputs=[google_api_key_input, tavily_api_key_input],outputs=[initialization_status, chatbot, chat_history_state, chat_input, settings_accordion])chat_input.submit(fn=chat_step,inputs=[chat_input, chat_history_state, agent_state, initial_topic_state],outputs=[chatbot, chat_history_state, agent_state, initial_topic_state, chat_input]).then(lambda: "", None, chat_input, queue=False)8 unmodified lines9101112131415161718192032 unmodified lines5354555657585960616263646566676869707172737475762 unmodified lines798081828384858687888990919293949596972 unmodified lines1001011021031041051061071081091101111121131141151161171181191201211221231241258 unmodified lines# --- CSS for a professional chatbot look ---CSS = """body, .gradio-container { font-family: 'Inter', sans-serif; background-color: #F1F5F9; }.gradio-container { max-width: 800px !important; margin: auto !important; padding-top: 20px !important;}h1 { text-align: center; font-weight: 700; font-size: 2.5em; color: #1E293B; }.sub-header { text-align: center; color: #475569; margin-bottom: 20px; font-size: 1.1em; }.accordion { border: none !important; box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1) !important; }#chatbot { box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1) !important; min-height: 450px; }.message-bubble.user { background: #2563EB !important; color: white; }.message-bubble.bot { background: #FFFFFF !important; color: #334155; border: 1px solid #E2E8F0; }footer { display: none !important; }32 unmodified linesinitialization_status = gr.Markdown(visible=False)chatbot = gr.Chatbot(elem_id="chatbot",bubble_full_width=False,height=500,visible=False,type="messages" # Use the modern 'messages' format)chat_input = gr.Textbox(placeholder="Enter your research topic...", interactive=False, visible=False)# State variablesagent_state = gr.State("INITIAL")initial_topic_state = gr.State("")def handle_initialization(google_key, tavily_key):init_status = initialize_models(google_key, tavily_key)initial_messages = [{"role": "assistant", "content": "Agent initialized. Please enter your research topic to begin."}]return {initialization_status: gr.update(value=f"**Status:** {init_status}", visible=True),chatbot: gr.update(visible=True, value=initial_messages),chat_input: gr.update(interactive=True, visible=True),settings_accordion: gr.update(open=False)}2 unmodified lineshistory.append({"role": "user", "content": user_input})if current_agent_state == "INITIAL":history.append({"role": "assistant", "content": "Thinking..."})yield history, "CLARIFYING", user_input, gr.update(interactive=False)questions = get_clarifying_questions(planner_model, user_input)history[-1] = {"role": "assistant", "content": "I can do that. To give you the best report, could you answer these questions for me?\n\n" + questions}yield history, "CLARIFYING", user_input, gr.update(interactive=True)elif current_agent_state == "CLARIFYING":history.append({"role": "assistant", "content": "Generating full report..."})yield history, "GENERATING", topic_state, gr.update(interactive=False)try:plan = research_and_plan(config, planner_model, tavily_client, topic_state, user_input)report_generator = write_report_stream(config, writer_model, tavily_client, embedding_model, reranker, plan)status_updates = ""2 unmodified linesfinal_report_md = update if "Report Generation Complete" not in update else final_report_mdstatus_updates += updatehistory[-1] = {"role": "assistant", "content": status_updates}yield history, "GENERATING", topic_state, gr.update(interactive=False)history.append({"role": "assistant", "content": final_report_md})yield history, "INITIAL", "", gr.update(interactive=True)except Exception as e:error_message = f"An error occurred: {str(e)}"history.append({"role": "assistant", "content": error_message})yield history, "INITIAL", "", gr.update(interactive=True)init_button.click(fn=handle_initialization,inputs=[google_api_key_input, tavily_api_key_input],outputs=[initialization_status, chatbot, chat_input, settings_accordion])chat_input.submit(fn=chat_step,inputs=[chat_input, chatbot, agent_state, initial_topic_state],outputs=[chatbot, agent_state, initial_topic_state, chat_input]).then(lambda: "", None, chat_input, queue=False)
app.py+17-38