shubhamgs commited on
Commit
21eca01
·
verified ·
1 Parent(s): 4fceb0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -83
app.py CHANGED
@@ -1,83 +1,53 @@
1
- import streamlit as st
2
- from deep_research_system import run_deep_research_system
3
- import traceback
4
- import streamlit.components.v1 as components
5
-
6
- if __name__ == "__main__":
7
- # Define API base URL (Update for Hugging Face deployment)
8
- API_URL = "http://localhost:8000/"
9
-
10
- # Set page configuration
11
- st.set_page_config(page_title="Deep Research AI", layout="centered")
12
-
13
- # Title and instructions
14
- st.title("Deep Research AI Agentic System")
15
- st.write("Enter a question below to get the latest insights from web research. Click 'Reset' to start over.")
16
-
17
- # Initialize session state
18
- if "show_reset_button" not in st.session_state:
19
- st.session_state.show_reset_button = False
20
- if "question" not in st.session_state:
21
- st.session_state.question = ""
22
- if "reset_triggered" not in st.session_state:
23
- st.session_state.reset_triggered = False
24
-
25
- # JavaScript to clear the input field
26
- clear_input_js = """
27
- <script>
28
- const input = document.querySelector('input[aria-label="Your Question"]');
29
- if (input) {
30
- input.value = '';
31
- }
32
- </script>
33
- """
34
-
35
- # Use a form to manage the question input and submission
36
- with st.form(key="question_form"):
37
- st.session_state.question = st.text_input(
38
- "Your Question",
39
- placeholder="e.g., What are the latest advancements in quantum computing?",
40
- value=st.session_state.question,
41
- key="question_input"
42
- )
43
- submit_button = st.form_submit_button("Get Answer")
44
-
45
- # Process the form submission
46
- if submit_button:
47
- if st.session_state.question:
48
- st.write(f"Research Agent: Searching for '{st.session_state.question}'...")
49
- try:
50
- with st.spinner("Gathering research data..."):
51
- answer = run_deep_research_system(st.session_state.question)
52
- st.write("Research Agent: Found 5 relevant sources.")
53
- st.write("Answer Drafter Agent: Drafted the final answer.")
54
- st.write("**Final Answer:**")
55
- st.write(answer)
56
- st.session_state.show_reset_button = True
57
- except Exception as e:
58
- st.error(f"An error occurred: {str(e)}\n{traceback.format_exc()}")
59
- st.session_state.show_reset_button = True
60
- else:
61
- st.warning("Please enter a question!")
62
- # Reset the trigger flag after submission
63
- st.session_state.reset_triggered = False
64
-
65
- # Function to clear the input state
66
- def clear_input():
67
- st.session_state.show_reset_button = False
68
- st.session_state.question = ""
69
- st.session_state.pop("question_input", None)
70
- st.session_state.pop("question_form", None)
71
- st.session_state.reset_triggered = True
72
-
73
- # Show Reset button only if show_reset_button is True
74
- if st.session_state.show_reset_button:
75
- if st.button("Reset"):
76
- # Clear the input and reset state
77
- clear_input()
78
- # Refresh the webpage
79
- st.rerun()
80
-
81
- # Execute JavaScript to clear the input field if reset was triggered
82
- if st.session_state.reset_triggered:
83
- components.html(clear_input_js, height=0)
 
1
+ import uuid
2
+ import traceback
3
+ import streamlit as st
4
+ from deep_research_system import run_deep_research_system
5
+
6
+ # Set page configuration
7
+ st.set_page_config(page_title="Deep Research AI", layout="centered")
8
+
9
+ # Session state setup
10
+ if "question" not in st.session_state:
11
+ st.session_state.question = ""
12
+ if "show_result" not in st.session_state:
13
+ st.session_state.show_result = False
14
+ if "input_key" not in st.session_state:
15
+ st.session_state.input_key = str(uuid.uuid4()) # Unique key for the input field
16
+
17
+ # Title
18
+ st.title("Deep Research AI Agentic System")
19
+ st.write("Enter a question below to get the latest insights from web research. Click 'Reset' to start over.")
20
+
21
+ # Text input (outside form for easier control)
22
+ st.session_state.question = st.text_input(
23
+ "Your Question",
24
+ value=st.session_state.question,
25
+ placeholder="e.g., What are the latest advancements in quantum computing?",
26
+ key=st.session_state.input_key
27
+ )
28
+
29
+ # Submit button
30
+ if st.button("Get Answer"):
31
+ if st.session_state.question.strip() != "":
32
+ st.session_state.show_result = True
33
+ st.write(f"Research Agent: Searching for '{st.session_state.question}'...")
34
+ try:
35
+ with st.spinner("Gathering research data..."):
36
+ answer = run_deep_research_system(st.session_state.question)
37
+ st.success("Research complete!.Answer Drafter Agent: Drafted the final answer.")
38
+ st.markdown("**Final Answer:**")
39
+ st.write(answer)
40
+ except Exception as e:
41
+ st.error(f"An error occurred: {str(e)}\n{traceback.format_exc()}")
42
+ else:
43
+ st.warning("Please enter a question!")
44
+
45
+ # Show result
46
+ if st.session_state.show_result:
47
+ # Reset button
48
+ if st.button("Reset"):
49
+ # Clear session and reset input widget key to force input clear
50
+ st.session_state.question = ""
51
+ st.session_state.show_result = False
52
+ st.session_state.input_key = str(uuid.uuid4()) # New key forces fresh input widget
53
+ st.rerun()