Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,70 @@
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
|
4 |
-
# Set your OpenAI API key
|
5 |
-
openai.api_key = "
|
6 |
|
7 |
-
#
|
8 |
client = openai.OpenAI(api_key=openai.api_key)
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
|
4 |
+
# Set your OpenAI API key from Hugging Face Secrets
|
5 |
+
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
6 |
|
7 |
+
# Initialize OpenAI client
|
8 |
client = openai.OpenAI(api_key=openai.api_key)
|
9 |
|
10 |
+
# Function to generate exam questions using OpenAI API
|
11 |
+
def generate_questions(knowledge_material, question_type, cognitive_level):
|
12 |
+
prompt = f"Generate {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}"
|
13 |
+
response = client.chat.completions.create(
|
14 |
+
model="gpt-4o-mini", # You can use "gpt-4" if available
|
15 |
+
messages=[
|
16 |
+
{"role": "system", "content": "You are a helpful assistant for generating exam questions."},
|
17 |
+
{"role": "user", "content": prompt}
|
18 |
+
]
|
19 |
+
)
|
20 |
+
return response.choices[0].message.content
|
21 |
+
|
22 |
+
# Login page
|
23 |
+
if 'username' not in st.session_state:
|
24 |
+
# Show the login form if the username is not set
|
25 |
+
st.title("Login")
|
26 |
+
username_input = st.text_input("Enter your username:")
|
27 |
+
if st.button("Login"):
|
28 |
+
if username_input:
|
29 |
+
st.session_state['username'] = username_input
|
30 |
+
st.success(f"Welcome, {username_input}!")
|
31 |
+
else:
|
32 |
+
st.warning("Please enter a valid username.")
|
33 |
+
else:
|
34 |
+
# Main App after login
|
35 |
+
st.title(f"Welcome, {st.session_state['username']}! Generate your exam questions")
|
36 |
+
|
37 |
+
# Input field for knowledge material (text)
|
38 |
+
knowledge_material = st.text_area("Enter knowledge material to generate exam questions:")
|
39 |
+
|
40 |
+
# File uploader for PDFs (Optional: Only if you want to implement PDF input later)
|
41 |
+
uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf")
|
42 |
+
|
43 |
+
# Select question type
|
44 |
+
question_type = st.selectbox("Select question type:",
|
45 |
+
["Multiple Choice", "Fill in the Blank", "Open-ended"])
|
46 |
+
|
47 |
+
# Select cognitive level
|
48 |
+
cognitive_level = st.selectbox("Select cognitive level:",
|
49 |
+
["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"])
|
50 |
+
|
51 |
+
# Generate questions button
|
52 |
+
if st.button("Generate Questions"):
|
53 |
+
if knowledge_material:
|
54 |
+
# Generate questions using the OpenAI API
|
55 |
+
questions = generate_questions(knowledge_material, question_type, cognitive_level)
|
56 |
+
|
57 |
+
# Display the generated questions
|
58 |
+
st.write("Generated Exam Questions:")
|
59 |
+
st.write(questions)
|
60 |
+
|
61 |
+
# Option to download the questions as a text file
|
62 |
+
st.download_button(
|
63 |
+
label="Download Questions",
|
64 |
+
data=questions,
|
65 |
+
file_name='generated_questions.txt',
|
66 |
+
mime='text/plain'
|
67 |
+
)
|
68 |
+
else:
|
69 |
+
st.warning("Please enter the knowledge material.")
|
70 |
|