Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from dotenv import load_dotenv | |
| from research_manager_JD import ResearchManager | |
| from planner_agent_JD import get_clarification_questions, refine_query_with_user_input | |
| load_dotenv(override=True) | |
| manager = ResearchManager() | |
| with gr.Blocks(theme=gr.themes.Default(primary_hue="sky")) as ui: | |
| gr.Markdown("# π Deep Research Assistant") | |
| chatbot = gr.Chatbot(label="Deep Research Chat") | |
| user_input = gr.Textbox(placeholder="Ask a research question...", show_label=False, lines=1) | |
| # Add this Markdown to display the full email content | |
| email_content_display = gr.Markdown(label="π§ Sent Email Content") | |
| query_state = gr.State() # Stores the original query (if clarification needed) | |
| clarification_mode = gr.State(value=False) # Whether awaiting clarification | |
| message_history = gr.State([]) # Keeps chat history | |
| # Main chat handler | |
| async def chat_handler(message, query, awaiting_clarification, history): | |
| history = history or [] | |
| email_content = "" # will hold full email content if returned | |
| # Initial user message β run clarification check | |
| if not query: | |
| clarification = await get_clarification_questions(message) | |
| if clarification.lower().strip() == "no clarification needed.": | |
| response = "" | |
| async for chunk in manager.run(message): | |
| # Detect email content if chunk contains full email | |
| if chunk.startswith("π§ Email sent successfully"): | |
| # Sometimes you might get just a notice, skip that | |
| continue | |
| elif chunk.startswith("<html>") or chunk.startswith("Dear") or "<html" in chunk.lower(): | |
| # If chunk looks like HTML email or formal email text, capture it | |
| email_content = chunk | |
| else: | |
| response = chunk | |
| history.append((message, response)) | |
| return history, None, False, history, gr.update(value=""), email_content | |
| else: | |
| history.append((message, clarification)) | |
| return history, message, True, history, gr.update(value=""), "" | |
| # If in clarification mode, treat input as clarification answer | |
| if awaiting_clarification: | |
| refined = await refine_query_with_user_input(query, message) | |
| response = "" | |
| async for chunk in manager.run(refined): | |
| if chunk.startswith("π§ Email sent successfully"): | |
| continue | |
| elif chunk.startswith("<html>") or chunk.startswith("Dear") or "<html" in chunk.lower(): | |
| email_content = chunk | |
| else: | |
| response = chunk | |
| history.append((message, response)) | |
| return history, None, False, history, gr.update(value=""), email_content | |
| # Fallback | |
| history.append((message, "Hmm, something went wrong.")) | |
| return history, None, False, history, gr.update(value=""), "" | |
| user_input.submit( | |
| fn=chat_handler, | |
| inputs=[user_input, query_state, clarification_mode, message_history], | |
| outputs=[chatbot, query_state, clarification_mode, message_history, user_input, email_content_display] | |
| ) | |
| ui.launch(inbrowser=True) | |