File size: 1,764 Bytes
9448d10
 
01a0232
 
 
 
9448d10
 
 
 
a40eb07
9448d10
 
 
a40eb07
 
 
 
9448d10
a40eb07
 
 
 
 
 
 
 
 
 
9448d10
01a0232
 
 
a40eb07
 
01a0232
a40eb07
01a0232
 
 
 
a40eb07
 
01a0232
a40eb07
 
 
9448d10
a40eb07
 
 
 
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
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)