Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +27 -9
streamlit_app.py
CHANGED
|
@@ -8,27 +8,45 @@ os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
|
|
| 8 |
st.set_page_config(page_title="OpenRouter Chat", layout="wide")
|
| 9 |
st.title("🧠 OpenRouter Chat Interface")
|
| 10 |
|
|
|
|
| 11 |
if "chat_history" not in st.session_state:
|
| 12 |
st.session_state.chat_history = []
|
| 13 |
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
with st.spinner("Thinking..."):
|
| 19 |
try:
|
| 20 |
response = httpx.post(
|
| 21 |
"http://localhost:7860/query",
|
| 22 |
-
|
|
|
|
| 23 |
)
|
|
|
|
| 24 |
if response.status_code == 200:
|
| 25 |
reply = response.json().get("response", "No response received.")
|
| 26 |
else:
|
| 27 |
reply = f"Error: {response.status_code} - {response.text}"
|
|
|
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
-
reply = f"
|
| 30 |
-
|
|
|
|
| 31 |
st.session_state.chat_history.append(("bot", reply))
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
| 8 |
st.set_page_config(page_title="OpenRouter Chat", layout="wide")
|
| 9 |
st.title("🧠 OpenRouter Chat Interface")
|
| 10 |
|
| 11 |
+
# Initialize chat history if it doesn't exist
|
| 12 |
if "chat_history" not in st.session_state:
|
| 13 |
st.session_state.chat_history = []
|
| 14 |
|
| 15 |
+
# Display chat messages from history
|
| 16 |
+
for role, msg in st.session_state.chat_history:
|
| 17 |
+
with st.chat_message(role):
|
| 18 |
+
st.write(msg)
|
| 19 |
|
| 20 |
+
# Get user input
|
| 21 |
+
if prompt := st.chat_input("Type your message..."):
|
| 22 |
+
# Add user message to chat history
|
| 23 |
+
st.session_state.chat_history.append(("user", prompt))
|
| 24 |
+
|
| 25 |
+
# Display user message immediately
|
| 26 |
+
with st.chat_message("user"):
|
| 27 |
+
st.write(prompt)
|
| 28 |
+
|
| 29 |
+
# Get and display bot response
|
| 30 |
with st.spinner("Thinking..."):
|
| 31 |
try:
|
| 32 |
response = httpx.post(
|
| 33 |
"http://localhost:7860/query",
|
| 34 |
+
json={"prompt": prompt}, # Changed from data to json for better formatting
|
| 35 |
+
timeout=10.0 # Added timeout to prevent hanging
|
| 36 |
)
|
| 37 |
+
|
| 38 |
if response.status_code == 200:
|
| 39 |
reply = response.json().get("response", "No response received.")
|
| 40 |
else:
|
| 41 |
reply = f"Error: {response.status_code} - {response.text}"
|
| 42 |
+
except httpx.RequestError as e:
|
| 43 |
+
reply = f"Request error: {str(e)}"
|
| 44 |
except Exception as e:
|
| 45 |
+
reply = f"Unexpected error: {str(e)}"
|
| 46 |
+
|
| 47 |
+
# Add bot response to chat history
|
| 48 |
st.session_state.chat_history.append(("bot", reply))
|
| 49 |
+
|
| 50 |
+
# Display bot response
|
| 51 |
+
with st.chat_message("bot"):
|
| 52 |
+
st.write(reply)
|