-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (52 loc) · 1.82 KB
/
app.py
File metadata and controls
73 lines (52 loc) · 1.82 KB
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
import time
import streamlit as st
from server import process_query
def reset_conversation():
"""
Resets the conversation state, clearing query history and input fields.
"""
st.session_state.query_history = []
st.session_state.current_query = ""
st.session_state.clear_input = False
def initialize_session_state():
"""
Initializes session state variables if not already set.
"""
if "query_history" not in st.session_state:
st.session_state.query_history = []
if "current_query" not in st.session_state:
st.session_state.current_query = ""
if "clear_input" not in st.session_state:
st.session_state.clear_input = False
def handle_query_submission(query: str):
"""
Handles query submission and displays the result chunk by chunk.
Args:
query (str): The user input query.
"""
if query.strip():
st.session_state.query_history.append(query)
st.session_state.current_query = query
result_placeholder = st.empty()
with st.spinner("Processing your query..."):
result_text = ""
for chunk in process_query(query):
result_text += chunk + " "
result_placeholder.write(result_text.strip())
time.sleep(0.05)
st.session_state.clear_input = True
else:
st.warning("Please enter a query before submitting.")
def main():
st.title("Search GPT")
initialize_session_state()
query = st.text_input("Enter your query:", value="", key="initial_input")
if st.button("Submit"):
handle_query_submission(query)
if st.button("Start New Query"):
reset_conversation()
st.experimental_rerun()
if st.session_state.clear_input:
st.session_state.clear_input = False
if __name__ == "__main__":
main()