Spaces:
Running
Running
| 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.") |