AlignAI/Open-Deep-Research
10
1import streamlit as st2import asyncio3from research import deep_research4from PIL import Image5 6# Page configuration7st.set_page_config(8 page_title="Open DeepResearch",9 page_icon="๐",10 layout="wide",11 initial_sidebar_state="expanded"12)13 14# Load images15logo = Image.open('logo.png')16banner = Image.open('banner.png')17 18# Display banner at top19# st.image(banner, use_container_width=True)20 21# Custom CSS22st.markdown("""23 <style>24 .stImage > img {25 border-radius: 10px;26 margin-bottom: 2rem;27 }28 .api-container {29 background-color: #f8f9fa;30 padding: 1.5rem;31 border-radius: 10px;32 margin-bottom: 2rem;33 border: 1px solid #e0e0e0;34 }35 .api-header {36 color: #1E88E5;37 font-size: 1.2rem;38 margin-bottom: 1rem;39 }40 </style>41 """, unsafe_allow_html=True)42 43# Initialize session state44if 'api_keys_configured' not in st.session_state:45 st.session_state.api_keys_configured = False46 47# Sidebar configuration48with st.sidebar:49 st.image(logo, width=200, use_container_width=True)50 st.markdown("### โ๏ธ API Configuration")51 52 with st.expander("Configure API Keys", expanded=not st.session_state.api_keys_configured):53 api_form = st.form("api_keys_form")54 with api_form:55 openrouter_key = api_form.text_input(56 "OpenRouter API Key",57 type="password",58 value=st.session_state.get('openrouter_key', ''),59 help="Required for language model access"60 )61 62 serpapi_key = api_form.text_input(63 "SerpAPI Key",64 type="password",65 value=st.session_state.get('serpapi_key', ''),66 help="Required for web search functionality"67 )68 69 jina_key = api_form.text_input(70 "Jina API Key",71 type="password",72 value=st.session_state.get('jina_key', ''),73 help="Required for content extraction"74 )75 76 if api_form.form_submit_button("Save API Keys"):77 if not all([openrouter_key, serpapi_key, jina_key]):78 st.error("โ All API keys are required!")79 else:80 st.session_state.openrouter_key = openrouter_key81 st.session_state.serpapi_key = serpapi_key82 st.session_state.jina_key = jina_key83 st.session_state.api_keys_configured = True84 st.success("โ
API keys saved successfully!")85 st.rerun()86 87 if st.session_state.api_keys_configured:88 st.success("โ
API Keys configured")89 90 st.markdown("### ๐ Get API Keys")91 st.markdown("""92 - [OpenRouter API Key](https://openrouter.ai/keys)93 - [SerpAPI Key](https://serpapi.com/manage-api-key)94 - [Jina API Key](https://jina.ai/api-key)95 """)96 97def run_research(user_query, iteration_limit, search_limit):98 deep_research.OPENROUTER_API_KEY = st.session_state.openrouter_key99 deep_research.SERPAPI_API_KEY = st.session_state.serpapi_key100 deep_research.JINA_API_KEY = st.session_state.jina_key101 return asyncio.run(deep_research.research_flow(user_query, iteration_limit, search_limit))102 103# Main content104st.title("๐ Open DeepResearch")105st.markdown("""106 <div style='background-color: #5dade2; padding: 1rem; border-radius: 10px; margin-bottom: 2rem;'>107 <h4 style='color: #1565C0; margin-bottom: 0.5rem;'>Welcome to the Open DeepResearch!</h4>108 <p style='color: #424242;'>109 This application helps you conduct comprehensive research on any topic by:110 <br>111 โข Generating relevant search queries<br>112 โข Analyzing multiple sources<br>113 โข Synthesizing information into a detailed report114 </p>115 </div>116""", unsafe_allow_html=True)117 118with st.container():119 col1, col2 = st.columns([2, 1])120 121 with col1:122 with st.form("research_form", clear_on_submit=False):123 st.markdown("### Research Parameters")124 125 user_query = st.text_area(126 "Research Query",127 placeholder="Enter your research topic or question here...",128 help="Be as specific as possible for better results",129 height=100,130 disabled=not st.session_state.api_keys_configured131 )132 133 col_a, col_b = st.columns(2)134 with col_a:135 iter_limit_input = st.number_input(136 "Maximum Iterations",137 min_value=1,138 max_value=20,139 value=10,140 help="Higher values mean more thorough research but longer processing time",141 disabled=not st.session_state.api_keys_configured142 )143 144 with col_b:145 search_limit_input = st.number_input(146 "Results Per Search",147 min_value=1,148 max_value=20,149 value=5,150 help="Number of results per search. Lower values reduce token usage",151 disabled=not st.session_state.api_keys_configured152 )153 154 submitted = st.form_submit_button(155 "๐ Start Research",156 disabled=not st.session_state.api_keys_configured157 )158 159 with col2:160 st.markdown("### Tips for Better Results")161 st.info("""162 โข Be specific in your query163 โข Use clear, focused questions164 โข Consider including relevant keywords165 โข Specify time periods if applicable166 โข Lower search results reduce token usage167 """)168 169 if not st.session_state.api_keys_configured:170 st.warning("โ ๏ธ Please configure your API keys in the sidebar to enable research.")171 172if submitted and st.session_state.api_keys_configured:173 if not user_query.strip():174 st.error("โ ๏ธ Please enter a research query before proceeding.")175 else:176 try:177 with st.spinner("๐ Conducting research... This may take a few minutes..."):178 final_report = run_research(user_query, int(iter_limit_input), int(search_limit_input))179 180 st.markdown("""181 <div class='report-container'>182 <h3 style='color: #1E88E5; margin-bottom: 1rem;'>๐ Research Report</h3>183 </div>184 """, unsafe_allow_html=True)185 186 tab1, tab2 = st.tabs(["๐ Formatted Report", "๐ Raw Text"])187 188 with tab1:189 st.markdown(final_report)190 191 with tab2:192 st.text_area(193 label="",194 value=final_report,195 height=500,196 help="You can copy the raw text from here"197 )198 199 st.download_button(200 label="๐ฅ Download Report",201 data=final_report,202 file_name="research_report.txt",203 mime="text/plain"204 )205 206 except Exception as e:207 st.error(f"โ An error occurred during research: {str(e)}")208 st.markdown("""209 <div style='background-color: #ffebee; padding: 1rem; border-radius: 10px;'>210 <p style='color: #c62828;'>Please try again with a different query or contact support if the issue persists.</p>211 </div>212 """, unsafe_allow_html=True)213 214st.markdown("""215 <div style='text-align: center; color: #666; padding: 2rem;'>216 <p>Built by GitsSaikat โค๏ธ</p>217 </div>218""", unsafe_allow_html=True)