Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import openai
|
3 |
+
import os
|
4 |
+
|
5 |
+
# Function to get the API key from Streamlit secrets
|
6 |
+
def get_api_key():
|
7 |
+
try:
|
8 |
+
return st.secrets["API_KEY"]
|
9 |
+
except KeyError:
|
10 |
+
st.error("API_KEY not found in Streamlit secrets. Please add it.")
|
11 |
+
return None
|
12 |
+
|
13 |
+
# Function to interact with the OpenAI API with streaming
|
14 |
+
def generate_response(messages, model_name, api_key): # Modified to accept 'messages'
|
15 |
+
try:
|
16 |
+
client = openai.OpenAI(api_key=api_key) # Instantiate OpenAI client with api_key
|
17 |
+
|
18 |
+
stream = client.chat.completions.create(
|
19 |
+
model=model_name,
|
20 |
+
messages=messages, # Use the entire conversation history
|
21 |
+
stream=True,
|
22 |
+
)
|
23 |
+
return stream
|
24 |
+
except openai.APIError as e:
|
25 |
+
st.error(f"OpenAI API Error with {model_name}: {e}")
|
26 |
+
return None
|
27 |
+
except openai.RateLimitError as e:
|
28 |
+
st.error(f"OpenAI Rate Limit Error with {model_name}: {e}")
|
29 |
+
return None
|
30 |
+
except openai.AuthenticationError as e:
|
31 |
+
st.error(f"OpenAI Authentication Error with {model_name}: {e}")
|
32 |
+
return None
|
33 |
+
except Exception as e:
|
34 |
+
st.error(f"An unexpected error occurred with {model_name}: {e}")
|
35 |
+
return None
|
36 |
+
|
37 |
+
# Main Streamlit app
|
38 |
+
def main():
|
39 |
+
st.title("Chatbot with Model Switching and Streaming")
|
40 |
+
|
41 |
+
# Initialize conversation history in session state
|
42 |
+
if "messages" not in st.session_state:
|
43 |
+
st.session_state.messages = []
|
44 |
+
|
45 |
+
# Display previous messages
|
46 |
+
for message in st.session_state.messages:
|
47 |
+
with st.chat_message(message["role"]):
|
48 |
+
st.markdown(message["content"])
|
49 |
+
|
50 |
+
# Get user input
|
51 |
+
prompt = st.chat_input("Say something")
|
52 |
+
|
53 |
+
if prompt:
|
54 |
+
# Add user message to the state
|
55 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
56 |
+
with st.chat_message("user"):
|
57 |
+
st.markdown(prompt)
|
58 |
+
|
59 |
+
# Define model priority
|
60 |
+
models = ["gpt-4o-mini", "gpt-3.5-turbo"] # Add more models as needed
|
61 |
+
|
62 |
+
# Get API key
|
63 |
+
api_key = get_api_key()
|
64 |
+
if not api_key:
|
65 |
+
return
|
66 |
+
|
67 |
+
full_response = ""
|
68 |
+
# Prepare messages for OpenAI:
|
69 |
+
openai_messages = st.session_state.messages
|
70 |
+
for model in models:
|
71 |
+
stream = generate_response(openai_messages, model, api_key) # Pass the messages
|
72 |
+
if stream:
|
73 |
+
with st.chat_message("assistant"):
|
74 |
+
message_placeholder = st.empty()
|
75 |
+
for chunk in stream:
|
76 |
+
if chunk.choices and chunk.choices[0].delta and chunk.choices[0].delta.content:
|
77 |
+
full_response += chunk.choices[0].delta.content
|
78 |
+
message_placeholder.markdown(full_response + "▌")
|
79 |
+
message_placeholder.markdown(full_response)
|
80 |
+
break # Break after successful response
|
81 |
+
full_response = "" # Reset for the next model attempt
|
82 |
+
|
83 |
+
if full_response:
|
84 |
+
# Add bot message to state
|
85 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
86 |
+
|
87 |
+
if __name__ == "__main__":
|
88 |
+
main()
|