MusaR commited on
Commit
c7f8b81
·
verified ·
1 Parent(s): d0a08f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -38
app.py CHANGED
@@ -9,13 +9,12 @@ from research_agent.agent import get_clarifying_questions, research_and_plan, wr
9
 
10
  # --- CSS for a professional chatbot look ---
11
  CSS = """
12
- body, .gradio-container { font-family: 'Inter', sans-serif; }
13
  .gradio-container { max-width: 800px !important; margin: auto !important; padding-top: 20px !important;}
14
  h1 { text-align: center; font-weight: 700; font-size: 2.5em; color: #1E293B; }
15
  .sub-header { text-align: center; color: #475569; margin-bottom: 20px; font-size: 1.1em; }
16
  .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; }
17
- #chatbot { box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px -1px rgba(0, 0, 0, 0.1) !important; }
18
- .message-bubble-container > .message-bubble { box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.05) !important; }
19
  .message-bubble.user { background: #2563EB !important; color: white; }
20
  .message-bubble.bot { background: #FFFFFF !important; color: #334155; border: 1px solid #E2E8F0; }
21
  footer { display: none !important; }
@@ -54,30 +53,24 @@ with gr.Blocks(css=CSS, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="
54
  initialization_status = gr.Markdown(visible=False)
55
 
56
  chatbot = gr.Chatbot(
57
- [],
58
  elem_id="chatbot",
59
- label="Research Agent",
60
  bubble_full_width=False,
61
  height=500,
62
  visible=False,
63
- render=False # We will render it manually later
64
  )
65
- # Use the modern 'messages' type for the chatbot state
66
- chat_history_state = gr.State([])
67
 
68
- chat_input = gr.Textbox(placeholder="Enter your research topic...", interactive=False, visible=False, render=False)
69
-
70
- # Agent state can be: "INITIAL", "CLARIFYING", "GENERATING"
71
  agent_state = gr.State("INITIAL")
72
  initial_topic_state = gr.State("")
73
 
74
  def handle_initialization(google_key, tavily_key):
75
  init_status = initialize_models(google_key, tavily_key)
76
- initial_message = {"role": "assistant", "content": "Agent initialized. Please enter your research topic to begin."}
77
  return {
78
  initialization_status: gr.update(value=f"**Status:** {init_status}", visible=True),
79
- chatbot: gr.update(visible=True, value=[initial_message]),
80
- chat_history_state: [initial_message],
81
  chat_input: gr.update(interactive=True, visible=True),
82
  settings_accordion: gr.update(open=False)
83
  }
@@ -86,25 +79,19 @@ with gr.Blocks(css=CSS, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="
86
  history.append({"role": "user", "content": user_input})
87
 
88
  if current_agent_state == "INITIAL":
89
- # 1. User provides the initial topic
90
- new_agent_state = "CLARIFYING"
91
- new_topic_state = user_input
92
  history.append({"role": "assistant", "content": "Thinking..."})
93
- yield history, history, new_agent_state, new_topic_state, gr.update(interactive=False)
94
 
95
  questions = get_clarifying_questions(planner_model, user_input)
96
  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}
97
- yield history, history, new_agent_state, new_topic_state, gr.update(interactive=True)
98
 
99
  elif current_agent_state == "CLARIFYING":
100
- # 2. User provides answers to clarifying questions
101
- new_agent_state = "GENERATING"
102
- new_topic_state = topic_state
103
  history.append({"role": "assistant", "content": "Generating full report..."})
104
- yield history, history, new_agent_state, new_topic_state, gr.update(interactive=False)
105
 
106
  try:
107
- plan = research_and_plan(config, planner_model, tavily_client, new_topic_state, user_input)
108
  report_generator = write_report_stream(config, writer_model, tavily_client, embedding_model, reranker, plan)
109
 
110
  status_updates = ""
@@ -113,34 +100,26 @@ with gr.Blocks(css=CSS, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="
113
  final_report_md = update if "Report Generation Complete" not in update else final_report_md
114
  status_updates += update
115
  history[-1] = {"role": "assistant", "content": status_updates}
116
- yield history, history, new_agent_state, new_topic_state, gr.update(interactive=False)
117
 
118
  history.append({"role": "assistant", "content": final_report_md})
119
- new_agent_state = "INITIAL"
120
- new_topic_state = ""
121
- yield history, history, new_agent_state, new_topic_state, gr.update(interactive=True)
122
 
123
  except Exception as e:
124
  error_message = f"An error occurred: {str(e)}"
125
  history.append({"role": "assistant", "content": error_message})
126
- new_agent_state = "INITIAL"
127
- new_topic_state = ""
128
- yield history, history, new_agent_state, new_topic_state, gr.update(interactive=True)
129
-
130
- # Manually render components to control order
131
- chatbot.render()
132
- chat_input.render()
133
 
134
  init_button.click(
135
  fn=handle_initialization,
136
  inputs=[google_api_key_input, tavily_api_key_input],
137
- outputs=[initialization_status, chatbot, chat_history_state, chat_input, settings_accordion]
138
  )
139
 
140
  chat_input.submit(
141
  fn=chat_step,
142
- inputs=[chat_input, chat_history_state, agent_state, initial_topic_state],
143
- outputs=[chatbot, chat_history_state, agent_state, initial_topic_state, chat_input]
144
  ).then(
145
  lambda: "", None, chat_input, queue=False
146
  )
 
9
 
10
  # --- CSS for a professional chatbot look ---
11
  CSS = """
12
+ body, .gradio-container { font-family: 'Inter', sans-serif; background-color: #F1F5F9; }
13
  .gradio-container { max-width: 800px !important; margin: auto !important; padding-top: 20px !important;}
14
  h1 { text-align: center; font-weight: 700; font-size: 2.5em; color: #1E293B; }
15
  .sub-header { text-align: center; color: #475569; margin-bottom: 20px; font-size: 1.1em; }
16
  .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; }
17
+ #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; }
 
18
  .message-bubble.user { background: #2563EB !important; color: white; }
19
  .message-bubble.bot { background: #FFFFFF !important; color: #334155; border: 1px solid #E2E8F0; }
20
  footer { display: none !important; }
 
53
  initialization_status = gr.Markdown(visible=False)
54
 
55
  chatbot = gr.Chatbot(
 
56
  elem_id="chatbot",
 
57
  bubble_full_width=False,
58
  height=500,
59
  visible=False,
60
+ type="messages" # Use the modern 'messages' format
61
  )
62
+ chat_input = gr.Textbox(placeholder="Enter your research topic...", interactive=False, visible=False)
 
63
 
64
+ # State variables
 
 
65
  agent_state = gr.State("INITIAL")
66
  initial_topic_state = gr.State("")
67
 
68
  def handle_initialization(google_key, tavily_key):
69
  init_status = initialize_models(google_key, tavily_key)
70
+ initial_messages = [{"role": "assistant", "content": "Agent initialized. Please enter your research topic to begin."}]
71
  return {
72
  initialization_status: gr.update(value=f"**Status:** {init_status}", visible=True),
73
+ chatbot: gr.update(visible=True, value=initial_messages),
 
74
  chat_input: gr.update(interactive=True, visible=True),
75
  settings_accordion: gr.update(open=False)
76
  }
 
79
  history.append({"role": "user", "content": user_input})
80
 
81
  if current_agent_state == "INITIAL":
 
 
 
82
  history.append({"role": "assistant", "content": "Thinking..."})
83
+ yield history, "CLARIFYING", user_input, gr.update(interactive=False)
84
 
85
  questions = get_clarifying_questions(planner_model, user_input)
86
  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}
87
+ yield history, "CLARIFYING", user_input, gr.update(interactive=True)
88
 
89
  elif current_agent_state == "CLARIFYING":
 
 
 
90
  history.append({"role": "assistant", "content": "Generating full report..."})
91
+ yield history, "GENERATING", topic_state, gr.update(interactive=False)
92
 
93
  try:
94
+ plan = research_and_plan(config, planner_model, tavily_client, topic_state, user_input)
95
  report_generator = write_report_stream(config, writer_model, tavily_client, embedding_model, reranker, plan)
96
 
97
  status_updates = ""
 
100
  final_report_md = update if "Report Generation Complete" not in update else final_report_md
101
  status_updates += update
102
  history[-1] = {"role": "assistant", "content": status_updates}
103
+ yield history, "GENERATING", topic_state, gr.update(interactive=False)
104
 
105
  history.append({"role": "assistant", "content": final_report_md})
106
+ yield history, "INITIAL", "", gr.update(interactive=True)
 
 
107
 
108
  except Exception as e:
109
  error_message = f"An error occurred: {str(e)}"
110
  history.append({"role": "assistant", "content": error_message})
111
+ yield history, "INITIAL", "", gr.update(interactive=True)
 
 
 
 
 
 
112
 
113
  init_button.click(
114
  fn=handle_initialization,
115
  inputs=[google_api_key_input, tavily_api_key_input],
116
+ outputs=[initialization_status, chatbot, chat_input, settings_accordion]
117
  )
118
 
119
  chat_input.submit(
120
  fn=chat_step,
121
+ inputs=[chat_input, chatbot, agent_state, initial_topic_state],
122
+ outputs=[chatbot, agent_state, initial_topic_state, chat_input]
123
  ).then(
124
  lambda: "", None, chat_input, queue=False
125
  )