Spaces:
Running
Running
import gradio as gr | |
from research_agent import research | |
import os | |
from dotenv import load_dotenv | |
import re | |
# Load environment variables | |
load_dotenv() | |
def format_as_markdown(raw: str) -> str: | |
# 1. Remove <think>...</think> and everything inside | |
raw = re.sub(r"<think>.*?</think>", "", raw, flags=re.DOTALL) | |
# 2. Replace section headers with markdown equivalents | |
replacements = { | |
"[EXECUTIVE_SUMMARY]": "## Executive Summary", | |
"[MAIN_FINDINGS]": "## Main Findings", | |
"[ANALYSIS]": "## Analysis", | |
"[CONCLUSION]": "## Conclusion", | |
"[SOURCES]": "## Sources", | |
} | |
for tag, header in replacements.items(): | |
raw = raw.replace(tag, f"\n\n{header}\n\n") | |
# 3. Optional: clean up extra whitespace | |
raw = re.sub(r"\n{3,}", "\n\n", raw).strip() | |
return raw | |
def process_query(query: str) -> str: | |
"""Process the user query and return research results.""" | |
if not query.strip(): | |
return "Please enter a valid query." | |
try: | |
result = research(query) | |
# print("returning result", result) | |
result = format_as_markdown(result) | |
return result | |
except Exception as e: | |
return f"Error occurred: {str(e)}" | |
# Create Gradio interface | |
demo = gr.Interface( | |
fn=process_query, | |
inputs=gr.Textbox( | |
lines=3, | |
placeholder="Enter your research query here...", | |
label="Research Query" | |
), | |
outputs=gr.Markdown( | |
label="Research Results" | |
), | |
title="Deep Research Assistant", | |
description="Enter any query and get a comprehensive research report based on the latest information.", | |
examples=[ | |
["What are the latest developments in quantum computing?"], | |
["Explain the current state of climate change and its impacts"], | |
["What are the emerging trends in artificial intelligence?"] | |
], | |
theme=gr.themes.Soft() | |
).launch(mcp_server=True) | |