Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import httpx | |
| import os | |
| # Set Streamlit config directory to a writable location | |
| os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit" | |
| st.set_page_config(page_title="OpenRouter Chat", layout="wide") | |
| st.title("🧠 OpenRouter Chat Interface") | |
| # Initialize chat history if it doesn't exist | |
| if "chat_history" not in st.session_state: | |
| st.session_state.chat_history = [] | |
| # Display chat messages from history | |
| for role, msg in st.session_state.chat_history: | |
| with st.chat_message(role): | |
| st.write(msg) | |
| # Get user input | |
| if prompt := st.chat_input("Type your message..."): | |
| # Add user message to chat history | |
| st.session_state.chat_history.append(("user", prompt)) | |
| # Display user message immediately | |
| with st.chat_message("user"): | |
| st.write(prompt) | |
| # Get and display bot response | |
| with st.spinner("Thinking..."): | |
| try: | |
| response = httpx.post( | |
| "http://localhost:7860/query", | |
| json={"prompt": prompt}, # Changed from data to json for better formatting | |
| timeout=10.0 # Added timeout to prevent hanging | |
| ) | |
| if response.status_code == 200: | |
| reply = response.json().get("response", "No response received.") | |
| else: | |
| reply = f"Error: {response.status_code} - {response.text}" | |
| except httpx.RequestError as e: | |
| reply = f"Request error: {str(e)}" | |
| except Exception as e: | |
| reply = f"Unexpected error: {str(e)}" | |
| # Add bot response to chat history | |
| st.session_state.chat_history.append(("bot", reply)) | |
| # Display bot response | |
| with st.chat_message("bot"): | |
| st.write(reply) |