File size: 3,233 Bytes
448e2cc
4bbac91
fb56984
4bbac91
448e2cc
4bbac91
 
 
 
42aa7ed
4bbac91
 
fb56984
 
 
4bbac91
 
 
 
 
 
 
 
 
 
695c334
 
4bbac91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb56984
 
4bbac91
 
 
fb56984
 
 
 
4bbac91
 
 
fb56984
4bbac91
 
 
 
fb56984
 
 
 
4bbac91
 
 
 
 
 
fb56984
4bbac91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fb56984
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import streamlit as st
import requests
import os
import time

# =============================
# CONFIG
# =============================

BACKEND_URL = "https://mohith96-researchagentbackend.hf.space/research"
REQUEST_TIMEOUT = 300  # seconds

# Hugging Face token for private backend access
HF_BACKEND_TOKEN = os.getenv("HF_BACKEND_TOKEN")

# =============================
# PAGE SETUP
# =============================

st.set_page_config(
    page_title="Research Agent",
    page_icon="πŸ“š",
    layout="wide"
)

st.title("πŸ“š Research Agent")
st.caption("A general purpose research agent that will break down the input query into sub queries and perform iterative search in order to generate an ideal output")

# =============================
# STATE
# =============================

if "result" not in st.session_state:
    st.session_state.result = None

if "title" not in st.session_state:
    st.session_state.title = None

# =============================
# INPUT
# =============================

query = st.text_area(
    label="Research Query",
    placeholder="e.g. Compatibility issues between LangChain, Python 3.12, and Groq LLMs",
    height=120
)

col1, col2 = st.columns([1, 5])

with col1:
    run_clicked = st.button("πŸš€ Run Research", use_container_width=True)

# =============================
# ACTION
# =============================

if run_clicked:
    if not query.strip():
        st.warning("You need to enter a research query. This is not optional.")
    elif not HF_BACKEND_TOKEN:
        st.error("Backend token missing. Set HF_BACKEND_TOKEN in Space secrets.")
    else:
        with st.spinner("Running research pipeline..."):
            try:
                headers = {
                    "Authorization": f"Bearer {HF_BACKEND_TOKEN}"
                }

                response = requests.post(
                    BACKEND_URL,
                    json={"query": query},
                    headers=headers,
                    timeout=REQUEST_TIMEOUT
                )

                if response.status_code != 200:
                    st.error(
                        f"Backend error ({response.status_code}). "
                        f"Response: {response.text}"
                    )
                else:
                    payload = response.json()
                    st.session_state.title = payload.get("title", "Untitled")
                    st.session_state.result = payload.get("report", "")

            except requests.exceptions.Timeout:
                st.error("Backend timed out. Either it's slow or asleep.")
            except Exception as e:
                st.error(f"Unexpected failure: {str(e)}")

# =============================
# OUTPUT
# =============================

if st.session_state.result:
    st.divider()

    st.subheader(st.session_state.title)
    st.markdown(st.session_state.result)

    st.download_button(
        label="⬇️ Download Markdown",
        data=st.session_state.result,
        file_name=f"{st.session_state.title}.md",
        mime="text/markdown"
    )

# =============================
# FOOTER
# =============================

st.divider()
st.caption("Frontend-only Space. Backend is private and authenticated. As it should be.")