Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +76 -76
streamlit_app.py
CHANGED
|
@@ -1,76 +1,76 @@
|
|
| 1 |
-
|
| 2 |
-
import streamlit as st
|
| 3 |
-
import requests
|
| 4 |
-
import re # For space cleanup
|
| 5 |
-
|
| 6 |
-
st.set_page_config(page_title="AI Chatbot", layout="centered")
|
| 7 |
-
st.title("🤖 AI Chatbot")
|
| 8 |
-
|
| 9 |
-
if "messages" not in st.session_state:
|
| 10 |
-
st.session_state.messages = []
|
| 11 |
-
|
| 12 |
-
# Function to query AI API and stream response
|
| 13 |
-
def query_ai(question):
|
| 14 |
-
url = "http://127.0.0.1:8000/query/"
|
| 15 |
-
params = {"input_text": question}
|
| 16 |
-
|
| 17 |
-
with requests.get(url, params=params, stream=True,timeout=
|
| 18 |
-
if response.status_code == 200:
|
| 19 |
-
full_response = ""
|
| 20 |
-
for chunk in response.iter_content(chunk_size=1024):
|
| 21 |
-
if chunk:
|
| 22 |
-
text_chunk = chunk.decode("utf-8")
|
| 23 |
-
full_response += text_chunk
|
| 24 |
-
yield full_response # Streamed response
|
| 25 |
-
|
| 26 |
-
# Custom CSS for spacing fix
|
| 27 |
-
st.markdown("""
|
| 28 |
-
<style>
|
| 29 |
-
.chat-box {
|
| 30 |
-
background-color: #1e1e1e;
|
| 31 |
-
padding: 12px;
|
| 32 |
-
border-radius: 10px;
|
| 33 |
-
margin-top: 5px;
|
| 34 |
-
font-size: 154x;
|
| 35 |
-
font-family: monospace;
|
| 36 |
-
white-space: pre-wrap;
|
| 37 |
-
word-wrap: break-word;
|
| 38 |
-
line-height: 1.2;
|
| 39 |
-
color: #ffffff;
|
| 40 |
-
}
|
| 41 |
-
</style>
|
| 42 |
-
""", unsafe_allow_html=True)
|
| 43 |
-
|
| 44 |
-
user_input = st.text_input("Ask a question:", "", key="user_input")
|
| 45 |
-
submit_button = st.button("Submit")
|
| 46 |
-
|
| 47 |
-
if submit_button and user_input:
|
| 48 |
-
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 49 |
-
|
| 50 |
-
# Placeholder for streaming
|
| 51 |
-
response_container = st.empty()
|
| 52 |
-
full_response = ""
|
| 53 |
-
|
| 54 |
-
with st.spinner("🤖 AI is thinking..."):
|
| 55 |
-
for chunk in query_ai(user_input):
|
| 56 |
-
full_response = chunk
|
| 57 |
-
response_container.markdown(f'<div class="chat-box">{full_response}</div>', unsafe_allow_html=True)
|
| 58 |
-
|
| 59 |
-
response_container.empty() # Hides the streamed "Thinking" response after completion
|
| 60 |
-
|
| 61 |
-
# Extract refined answer after "</think>"
|
| 62 |
-
if "</think>" in full_response:
|
| 63 |
-
refined_response = full_response.split("</think>", 1)[-1].strip()
|
| 64 |
-
else:
|
| 65 |
-
refined_response = full_response # Fallback if </think> is missing
|
| 66 |
-
|
| 67 |
-
# Remove extra newlines and excessive spaces
|
| 68 |
-
refined_response = re.sub(r'\n\s*\n', '\n', refined_response.strip())
|
| 69 |
-
|
| 70 |
-
# Expandable AI Thought Process Box
|
| 71 |
-
with st.expander("🤖 AI's Thought Process (Click to Expand)"):
|
| 72 |
-
st.markdown(f'<div class="chat-box">{full_response}</div>', unsafe_allow_html=True)
|
| 73 |
-
|
| 74 |
-
# Display refined answer with clean formatting
|
| 75 |
-
st.write("Answer:")
|
| 76 |
-
st.markdown(refined_response, unsafe_allow_html=True)
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
import re # For space cleanup
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="AI Chatbot", layout="centered")
|
| 7 |
+
st.title("🤖 AI Chatbot")
|
| 8 |
+
|
| 9 |
+
if "messages" not in st.session_state:
|
| 10 |
+
st.session_state.messages = []
|
| 11 |
+
|
| 12 |
+
# Function to query AI API and stream response
|
| 13 |
+
def query_ai(question):
|
| 14 |
+
url = "http://127.0.0.1:8000/query/"
|
| 15 |
+
params = {"input_text": question}
|
| 16 |
+
|
| 17 |
+
with requests.get(url, params=params, stream=True,timeout=None) as response:
|
| 18 |
+
if response.status_code == 200:
|
| 19 |
+
full_response = ""
|
| 20 |
+
for chunk in response.iter_content(chunk_size=1024):
|
| 21 |
+
if chunk:
|
| 22 |
+
text_chunk = chunk.decode("utf-8")
|
| 23 |
+
full_response += text_chunk
|
| 24 |
+
yield full_response # Streamed response
|
| 25 |
+
|
| 26 |
+
# Custom CSS for spacing fix
|
| 27 |
+
st.markdown("""
|
| 28 |
+
<style>
|
| 29 |
+
.chat-box {
|
| 30 |
+
background-color: #1e1e1e;
|
| 31 |
+
padding: 12px;
|
| 32 |
+
border-radius: 10px;
|
| 33 |
+
margin-top: 5px;
|
| 34 |
+
font-size: 154x;
|
| 35 |
+
font-family: monospace;
|
| 36 |
+
white-space: pre-wrap;
|
| 37 |
+
word-wrap: break-word;
|
| 38 |
+
line-height: 1.2;
|
| 39 |
+
color: #ffffff;
|
| 40 |
+
}
|
| 41 |
+
</style>
|
| 42 |
+
""", unsafe_allow_html=True)
|
| 43 |
+
|
| 44 |
+
user_input = st.text_input("Ask a question:", "", key="user_input")
|
| 45 |
+
submit_button = st.button("Submit")
|
| 46 |
+
|
| 47 |
+
if submit_button and user_input:
|
| 48 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
| 49 |
+
|
| 50 |
+
# Placeholder for streaming
|
| 51 |
+
response_container = st.empty()
|
| 52 |
+
full_response = ""
|
| 53 |
+
|
| 54 |
+
with st.spinner("🤖 AI is thinking..."):
|
| 55 |
+
for chunk in query_ai(user_input):
|
| 56 |
+
full_response = chunk
|
| 57 |
+
response_container.markdown(f'<div class="chat-box">{full_response}</div>', unsafe_allow_html=True)
|
| 58 |
+
|
| 59 |
+
response_container.empty() # Hides the streamed "Thinking" response after completion
|
| 60 |
+
|
| 61 |
+
# Extract refined answer after "</think>"
|
| 62 |
+
if "</think>" in full_response:
|
| 63 |
+
refined_response = full_response.split("</think>", 1)[-1].strip()
|
| 64 |
+
else:
|
| 65 |
+
refined_response = full_response # Fallback if </think> is missing
|
| 66 |
+
|
| 67 |
+
# Remove extra newlines and excessive spaces
|
| 68 |
+
refined_response = re.sub(r'\n\s*\n', '\n', refined_response.strip())
|
| 69 |
+
|
| 70 |
+
# Expandable AI Thought Process Box
|
| 71 |
+
with st.expander("🤖 AI's Thought Process (Click to Expand)"):
|
| 72 |
+
st.markdown(f'<div class="chat-box">{full_response}</div>', unsafe_allow_html=True)
|
| 73 |
+
|
| 74 |
+
# Display refined answer with clean formatting
|
| 75 |
+
st.write("Answer:")
|
| 76 |
+
st.markdown(refined_response, unsafe_allow_html=True)
|