Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
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"]
|
@@ -7,17 +8,37 @@ openai.api_key = st.secrets["OPENAI_API_KEY"]
|
|
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
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
# Login page
|
23 |
if 'username' not in st.session_state:
|
@@ -37,6 +58,9 @@ else:
|
|
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 |
|
@@ -49,22 +73,51 @@ else:
|
|
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
|
55 |
-
questions =
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
68 |
else:
|
69 |
st.warning("Please enter the knowledge material.")
|
70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import openai
|
3 |
+
import time
|
4 |
|
5 |
# Set your OpenAI API key from Hugging Face Secrets
|
6 |
openai.api_key = st.secrets["OPENAI_API_KEY"]
|
|
|
8 |
# Initialize OpenAI client
|
9 |
client = openai.OpenAI(api_key=openai.api_key)
|
10 |
|
11 |
+
# Function to generate exam questions using OpenAI API with retry logic
|
12 |
+
def generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions, max_retries=3):
|
13 |
+
# Adjust the number of questions based on the type
|
14 |
+
if question_type == "Multiple Choice":
|
15 |
+
num_questions = 3
|
16 |
+
elif question_type == "Fill in the Blank":
|
17 |
+
num_questions = 10
|
18 |
+
else: # Open-ended
|
19 |
+
num_questions = 3
|
20 |
+
|
21 |
+
prompt = f"Generate {num_questions} {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}. {extra_instructions}"
|
22 |
+
if question_type != "Fill in the Blank":
|
23 |
+
prompt += " Provide answers with short explanations."
|
24 |
+
|
25 |
+
retries = 0
|
26 |
+
while retries < max_retries:
|
27 |
+
try:
|
28 |
+
response = client.chat.completions.create(
|
29 |
+
model="gpt-4o-mini",
|
30 |
+
messages=[
|
31 |
+
{"role": "system", "content": "You are a helpful assistant for generating exam questions."},
|
32 |
+
{"role": "user", "content": prompt}
|
33 |
+
]
|
34 |
+
)
|
35 |
+
return response.choices[0].message.content
|
36 |
+
except openai.error.APIConnectionError:
|
37 |
+
retries += 1
|
38 |
+
time.sleep(2) # Wait for 2 seconds before retrying
|
39 |
+
if retries == max_retries:
|
40 |
+
st.error("Failed to connect to OpenAI API after several attempts.")
|
41 |
+
return None
|
42 |
|
43 |
# Login page
|
44 |
if 'username' not in st.session_state:
|
|
|
58 |
# Input field for knowledge material (text)
|
59 |
knowledge_material = st.text_area("Enter knowledge material to generate exam questions:")
|
60 |
|
61 |
+
# Extra input field for additional instructions
|
62 |
+
extra_instructions = st.text_area("Enter additional instructions (e.g., how you want the questions to be phrased):")
|
63 |
+
|
64 |
# File uploader for PDFs (Optional: Only if you want to implement PDF input later)
|
65 |
uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf")
|
66 |
|
|
|
73 |
["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"])
|
74 |
|
75 |
# Generate questions button
|
76 |
+
if 'previous_questions' not in st.session_state:
|
77 |
+
st.session_state['previous_questions'] = []
|
78 |
+
|
79 |
if st.button("Generate Questions"):
|
80 |
if knowledge_material:
|
81 |
+
# Generate questions with retry logic
|
82 |
+
questions = generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions)
|
83 |
+
|
84 |
+
if questions:
|
85 |
+
st.write("Generated Exam Questions:")
|
86 |
+
st.write(questions)
|
87 |
+
|
88 |
+
# Avoid showing repeated content in future requests
|
89 |
+
st.session_state['previous_questions'].append(questions)
|
90 |
+
|
91 |
+
# Option to download the questions as a text file
|
92 |
+
st.download_button(
|
93 |
+
label="Download Questions",
|
94 |
+
data=questions,
|
95 |
+
file_name='generated_questions.txt',
|
96 |
+
mime='text/plain'
|
97 |
+
)
|
98 |
else:
|
99 |
st.warning("Please enter the knowledge material.")
|
100 |
|
101 |
+
# Button to generate more questions based on the same material
|
102 |
+
if st.button("Generate More Questions"):
|
103 |
+
if knowledge_material:
|
104 |
+
# Regenerate new questions, trying to avoid repeated content
|
105 |
+
questions = generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions)
|
106 |
+
|
107 |
+
# Check if the new set of questions is not the same as the previous set
|
108 |
+
if questions and questions not in st.session_state['previous_questions']:
|
109 |
+
st.write("Generated More Exam Questions:")
|
110 |
+
st.write(questions)
|
111 |
+
|
112 |
+
# Append the new questions to the session state
|
113 |
+
st.session_state['previous_questions'].append(questions)
|
114 |
+
|
115 |
+
# Option to download the new set of questions
|
116 |
+
st.download_button(
|
117 |
+
label="Download More Questions",
|
118 |
+
data=questions,
|
119 |
+
file_name='more_generated_questions.txt',
|
120 |
+
mime='text/plain'
|
121 |
+
)
|
122 |
+
else:
|
123 |
+
st.warning("New questions seem to overlap with the previous ones. Try adjusting the instructions.")
|