Spaces:
Sleeping
Sleeping
Create streamlit_app.py
Browse files- streamlit_app.py +94 -0
streamlit_app.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# streamlit_app.py
|
2 |
+
import streamlit as st
|
3 |
+
import sys
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Add the directory containing app.py to the Python path
|
7 |
+
# This assumes app.py is in the same directory as streamlit_app.py
|
8 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
9 |
+
|
10 |
+
# Import your respond function and any necessary global variables from app.py
|
11 |
+
# Make sure app.py loads the model, tokenizer, etc. when imported
|
12 |
+
try:
|
13 |
+
from app import respond, model_id # Import your main function and model_id
|
14 |
+
# You might also need to import other things if respond relies on globals directly
|
15 |
+
# from app import model, tokenizer, embedder, nlp, data, descriptions, embeddings, ...
|
16 |
+
print("Successfully imported respond function from app.py")
|
17 |
+
except ImportError as e:
|
18 |
+
st.error(f"Error importing core logic from app.py: {e}")
|
19 |
+
st.stop() # Stop the app if the core logic can't be loaded
|
20 |
+
|
21 |
+
# Set Streamlit page config
|
22 |
+
st.set_page_config(page_title="Business Q&A Assistant")
|
23 |
+
|
24 |
+
st.title(f"Business Q&A Assistant with {model_id}")
|
25 |
+
st.write("Ask questions about the business (details from Google Sheet) or general knowledge (via search).")
|
26 |
+
|
27 |
+
# Initialize chat history in Streamlit's session state
|
28 |
+
# Session state persists across reruns for a single user session
|
29 |
+
if "messages" not in st.session_state:
|
30 |
+
st.session_state.messages = []
|
31 |
+
|
32 |
+
# Display chat messages from history on app rerun
|
33 |
+
for message in st.session_state.messages:
|
34 |
+
with st.chat_message(message["role"]):
|
35 |
+
st.markdown(message["content"])
|
36 |
+
|
37 |
+
# Accept user input
|
38 |
+
if prompt := st.chat_input("Your Question"):
|
39 |
+
# Add user message to chat history
|
40 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
41 |
+
# Display user message in chat message container
|
42 |
+
with st.chat_message("user"):
|
43 |
+
st.markdown(prompt)
|
44 |
+
|
45 |
+
# Get the current chat history in the format your respond function expects
|
46 |
+
# Gradio's history is [(user, bot), (user, bot), ...]
|
47 |
+
# Streamlit's session state is a list of dicts [{"role": "user", "content": "..."}]
|
48 |
+
# We need to convert Streamlit's history format to Gradio's format for your respond function
|
49 |
+
gradio_chat_history = []
|
50 |
+
# Start from the second message if the first was from the system/initial state
|
51 |
+
# Or just iterate through pairs, skipping the latest user prompt for history pass
|
52 |
+
# The respond function expects history *before* the current turn
|
53 |
+
history_for_respond = []
|
54 |
+
# Iterate through messages, excluding the very last user prompt which is the current input
|
55 |
+
for i in range(len(st.session_state.messages) - 1):
|
56 |
+
if st.session_state.messages[i]["role"] == "user" and st.session_state.messages[i+1]["role"] == "assistant":
|
57 |
+
history_for_respond.append((st.session_state.messages[i]["content"], st.session_state.messages[i+1]["content"]))
|
58 |
+
# Handle cases where the last turn was user, but no assistant response yet (e.g., previous session)
|
59 |
+
elif st.session_state.messages[i]["role"] == "user" and (i+1 == len(st.session_state.messages) or st.session_state.messages[i+1]["role"] == "user"):
|
60 |
+
# If the last message was user and there's no following assistant message,
|
61 |
+
# this user message is part of the current turn's input, not history.
|
62 |
+
# This part of the loop structure might need adjustment based on how history is built
|
63 |
+
pass # Skip the current user prompt
|
64 |
+
|
65 |
+
|
66 |
+
# Call your respond function
|
67 |
+
with st.spinner("Thinking..."):
|
68 |
+
# Your respond function returns ("", updated_chat_history)
|
69 |
+
# We only need the updated chat history part
|
70 |
+
# We also need to pass the current user prompt as the first argument
|
71 |
+
_, updated_gradio_history = respond(prompt, history_for_respond) # Call your core logic
|
72 |
+
|
73 |
+
# The respond function modifies and returns the history in Gradio format.
|
74 |
+
# The last entry in updated_gradio_history should be the current turn's (user_prompt, bot_response).
|
75 |
+
# We need to extract the bot_response part and add it to Streamlit's session state.
|
76 |
+
|
77 |
+
if updated_gradio_history and updated_gradio_history[-1][0] == prompt:
|
78 |
+
bot_response = updated_gradio_history[-1][1]
|
79 |
+
else:
|
80 |
+
# Fallback if the history structure is unexpected
|
81 |
+
bot_response = "Sorry, I couldn't get a response from the model."
|
82 |
+
print("Warning: respond function returned history in an unexpected format.")
|
83 |
+
|
84 |
+
|
85 |
+
# Add assistant response to chat history
|
86 |
+
st.session_state.messages.append({"role": "assistant", "content": bot_response})
|
87 |
+
# Display assistant response in chat message container
|
88 |
+
with st.chat_message("assistant"):
|
89 |
+
st.markdown(bot_response)
|
90 |
+
|
91 |
+
# You might want a clear history button similar to Gradio's
|
92 |
+
if st.button("Clear Chat History"):
|
93 |
+
st.session_state.messages = []
|
94 |
+
st.experimental_rerun() # Rerun the app to clear the display
|