Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from openai import OpenAI # Import the client class | |
| st.title("Streamlit OpenAI GPT‑4‑Turbo Demo") | |
| # Ask user for their API key (masked input) | |
| user_api_key = st.text_input("Enter your OpenAI API key", type="password") | |
| # Ask user for a prompt | |
| user_prompt = st.text_input("Enter your prompt:") | |
| if user_api_key: | |
| # Instantiate the OpenAI client with the provided API key | |
| client = OpenAI(api_key=user_api_key) | |
| if user_prompt and st.button("Get Response"): | |
| try: | |
| # Use the new API call via the instantiated client | |
| response = client.chat.completions.create( | |
| model="gpt-4-turbo", # Using the newer GPT‑4‑Turbo model | |
| messages=[{"role": "user", "content": user_prompt}], | |
| max_tokens=100 | |
| ) | |
| st.write("OpenAI response:") | |
| st.write(response.choices[0].message.content) | |
| except Exception as e: | |
| st.error(f"An error occurred: {e}") | |
| else: | |
| st.info("Please enter your OpenAI API key above to get started.") | |