import gradio as gr import os from dotenv import load_dotenv import uuid load_dotenv(override=True) from research_assistant import graph as compiled_graph def start_research(topic:str, max_analysts:int=3, key:str=""): if key != os.getenv("RESEARCH_KEY"): return ( "❌ Invalid key. Please provide a valid key to use this service.", gr.update(visible=True, interactive=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), gr.update(value="", interactive=True) # clear key ) global thread_id thread_id = str(uuid.uuid4()) clean_state = { "topic": topic, "max_analysts": max_analysts, "human_analyst_feedback": "", "analysts": [], "sections": [], "introduction": "", "content": "", "conclusion": "", "final_report": "" } try: thread = {"configurable": {"thread_id": thread_id}} result=compiled_graph.invoke(clean_state, thread) return display_analysts_and_request_feedback(result) except Exception as e: return reset_to_start(result, f"❌ Error starting research: {str(e)}") def display_analysts_and_request_feedback(result): analysts= result.get('analysts',[]) if analysts: analysts_display="\n".join([ f"**{i+1}. {analyst.name}**\n" f" - Role: {analyst.role}\n" f" - Affiliation: {analyst.affiliation}\n" f" - Description: {analyst.description}\n" for i, analyst in enumerate(analysts) ]) feedback_prompt = ( f"## Analysts Generated for: '{result['topic']}'\n\n" f"{analysts_display}\n\n" f"**Please provide your feedback:**\n" f"- Type 'approve' to continue with these analysts\n" f"- Or provide specific feedback to regenerate analysts\n" f"- Example: 'Add a cybersecurity expert, remove the marketing analyst'" ) return ( feedback_prompt, gr.update(visible=True, value="", interactive=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=True), # reset_btn gr.update(value="", interactive=True) # clear key ) else: return reset_to_start(result, "❌ No Analysts generated. Please try again with a different topic.") def continue_with_feedback(feedback, button_clicked): if button_clicked == "research": feedback= "approve" try: thread = {"configurable": {"thread_id": thread_id}} compiled_graph.update_state( thread, {"human_analyst_feedback": feedback}, as_node="human_feedback" ) result=compiled_graph.invoke(None, thread) state= compiled_graph.get_state(thread) if state.next and state.next[0] == "human_feedback": return display_analysts_and_request_feedback(result) else: print("Going to display final report") return display_final_report(result) except Exception as e: return reset_to_start(result, f"❌ Error processing feedback: {str(e)}") def display_final_report(result): """Muestra el reporte final y resetea la interfaz""" print("Displaying final report...") try: final_report = result.get("final_report", "") print(f"Final report content: {final_report}") if final_report: return ( f"## 📄 Final Research Report\n\n{final_report}", gr.update(visible=False), # feedback_input gr.update(visible=False), # continue_btn gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), # reset_btn gr.update(value="", interactive=True) # clear key_input ) else: return reset_to_start(result, "❌ Error: No final report was generated.") except Exception as e: return reset_to_start(result, f"❌ Error displaying final report: {str(e)}") def reset_to_start(result, message=""): """Resetea la interfaz al estado inicial""" return ( message, gr.update(visible=False, value="", interactive=True), # feedback_input gr.update(visible=False), # continue_btn gr.update(visible=False), gr.update(visible=False), gr.update(value="", interactive=True), # clear key_input gr.update(visible=False) # start_btn ) def reset_interface(): return ( "", # output1 gr.update(visible=False, value="", interactive=True), # feedback_input gr.update(visible=False), # continue_btn gr.update(visible=False), # continue_research_btn gr.update(visible=True), # start_btn gr.update(visible=True, value=""), # topic gr.update(visible=True) # max_analysts ) ############################################## Gradio UI ######################################################################### with gr.Blocks(title="Research Assistant", theme=gr.themes.Soft()) as app: gr.Markdown("