Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,29 @@
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from openai import OpenAI # Import the client class
|
| 3 |
|
| 4 |
+
st.title("Streamlit OpenAI GPT‑4‑Turbo Demo")
|
| 5 |
+
|
| 6 |
+
# Ask user for their API key (masked input)
|
| 7 |
+
user_api_key = st.text_input("Enter your OpenAI API key", type="password")
|
| 8 |
+
|
| 9 |
+
# Ask user for a prompt
|
| 10 |
+
user_prompt = st.text_input("Enter your prompt:")
|
| 11 |
+
|
| 12 |
+
if user_api_key:
|
| 13 |
+
# Instantiate the OpenAI client with the provided API key
|
| 14 |
+
client = OpenAI(api_key=user_api_key)
|
| 15 |
+
|
| 16 |
+
if user_prompt and st.button("Get Response"):
|
| 17 |
+
try:
|
| 18 |
+
# Use the new API call via the instantiated client
|
| 19 |
+
response = client.chat.completions.create(
|
| 20 |
+
model="gpt-4-turbo", # Using the newer GPT‑4‑Turbo model
|
| 21 |
+
messages=[{"role": "user", "content": user_prompt}],
|
| 22 |
+
max_tokens=100
|
| 23 |
+
)
|
| 24 |
+
st.write("OpenAI response:")
|
| 25 |
+
st.write(response.choices[0].message.content)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
st.error(f"An error occurred: {e}")
|
| 28 |
+
else:
|
| 29 |
+
st.info("Please enter your OpenAI API key above to get started.")
|