File size: 1,931 Bytes
d07654f
68b80a4
860c2fc
 
68b80a4
 
 
d07654f
68b80a4
 
 
860c2fc
68b80a4
 
 
 
 
 
 
 
 
 
860c2fc
68b80a4
 
860c2fc
68b80a4
d07654f
68b80a4
 
 
 
 
860c2fc
68b80a4
 
 
 
 
d07654f
68b80a4
 
d07654f
68b80a4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)