Spaces:
Sleeping
Sleeping
import streamlit as st | |
import openai | |
# Set your OpenAI API key | |
openai.api_key = st.secrets["OPENAI_API_KEY"] | |
# Streamlit app | |
st.title("Test OpenAI API in Streamlit") | |
# User input | |
user_input = st.text_area("Enter prompt to generate questions:") | |
if st.button("Generate Questions"): | |
if user_input: | |
# Use the new chat-based API | |
response = openai.ChatCompletion.create( | |
model="gpt-4o-mini", # You can use "gpt-4" if available | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant that generates exam questions."}, | |
{"role": "user", "content": f"Generate exam questions from the following material: {user_input}"} | |
] | |
) | |
# Display the generated questions | |
st.write("Generated Questions:") | |
st.write(response['choices'][0]['message']['content']) | |
else: | |
st.warning("Please enter a prompt.") | |