Spaces:
Running
Running
import streamlit as st | |
import openai | |
# Set your OpenAI API key from Hugging Face Secrets | |
openai.api_key = st.secrets["OPENAI_API_KEY"] | |
# Initialize OpenAI client | |
client = openai.OpenAI(api_key=openai.api_key) | |
# Function to generate exam questions using OpenAI API | |
def generate_questions(knowledge_material, question_type, cognitive_level): | |
prompt = f"Generate {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}" | |
response = client.chat.completions.create( | |
model="gpt-4o-mini", # You can use "gpt-4" if available | |
messages=[ | |
{"role": "system", "content": "You are a helpful assistant for generating exam questions."}, | |
{"role": "user", "content": prompt} | |
] | |
) | |
return response.choices[0].message.content | |
# Login page | |
if 'username' not in st.session_state: | |
# Show the login form if the username is not set | |
st.title("Login") | |
username_input = st.text_input("Enter your username:") | |
if st.button("Login"): | |
if username_input: | |
st.session_state['username'] = username_input | |
st.success(f"Welcome, {username_input}!") | |
else: | |
st.warning("Please enter a valid username.") | |
else: | |
# Main App after login | |
st.title(f"Welcome, {st.session_state['username']}! Generate your exam questions") | |
# Input field for knowledge material (text) | |
knowledge_material = st.text_area("Enter knowledge material to generate exam questions:") | |
# File uploader for PDFs (Optional: Only if you want to implement PDF input later) | |
uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf") | |
# Select question type | |
question_type = st.selectbox("Select question type:", | |
["Multiple Choice", "Fill in the Blank", "Open-ended"]) | |
# Select cognitive level | |
cognitive_level = st.selectbox("Select cognitive level:", | |
["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"]) | |
# Generate questions button | |
if st.button("Generate Questions"): | |
if knowledge_material: | |
# Generate questions using the OpenAI API | |
questions = generate_questions(knowledge_material, question_type, cognitive_level) | |
# Display the generated questions | |
st.write("Generated Exam Questions:") | |
st.write(questions) | |
# Option to download the questions as a text file | |
st.download_button( | |
label="Download Questions", | |
data=questions, | |
file_name='generated_questions.txt', | |
mime='text/plain' | |
) | |
else: | |
st.warning("Please enter the knowledge material.") | |