Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,121 @@
|
|
1 |
-
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import openai
|
3 |
import os
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
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(
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
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 |
-
|
32 |
-
return None
|
33 |
except Exception as e:
|
34 |
-
|
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", "gpt-4o-mini"] # 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
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", "gpt-4o-mini"] # 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()
|
89 |
+
|
90 |
import openai
|
91 |
import os
|
92 |
|
93 |
+
def get_available_models(api_key):
|
94 |
+
"""
|
95 |
+
Retrieves and prints the names of all available models from the OpenAI API.
|
96 |
+
|
97 |
+
Args:
|
98 |
+
api_key (str): Your OpenAI API key.
|
99 |
+
"""
|
100 |
+
openai.api_key = api_key # Set the API key
|
101 |
|
|
|
|
|
102 |
try:
|
103 |
+
client = openai.OpenAI()
|
104 |
+
models = client.models.list()
|
105 |
+
|
106 |
+
print("Available models:")
|
107 |
+
for model in models.data:
|
108 |
+
print(model.id) # Print the model ID (name)
|
109 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
except openai.AuthenticationError as e:
|
111 |
+
print(f"Authentication error: {e}")
|
|
|
112 |
except Exception as e:
|
113 |
+
print(f"An error occurred: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
114 |
|
115 |
if __name__ == "__main__":
|
116 |
+
# Replace "YOUR_API_KEY" with your actual OpenAI API key, or get it from environment variables
|
117 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
118 |
+
if not api_key:
|
119 |
+
print("Please set the OPENAI_API_KEY environment variable or replace 'YOUR_API_KEY' with your key.")
|
120 |
+
else:
|
121 |
+
get_available_models(api_key)
|