Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,6 +8,9 @@ from oauth2client.service_account import ServiceAccountCredentials
|
|
8 |
# Set up OpenAI client
|
9 |
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
10 |
|
|
|
|
|
|
|
11 |
# Google Sheets setup remains the same
|
12 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
13 |
creds = ServiceAccountCredentials.from_json_keyfile_name("genexam-2c8c645ecc0d.json", scope)
|
@@ -34,6 +37,21 @@ def update_api_usage(username):
|
|
34 |
except Exception as e:
|
35 |
st.error(f"Error updating API usage: {str(e)}")
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
def generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions, case_based, num_choices=None, max_retries=3):
|
38 |
# Adjust number of questions based on type
|
39 |
if question_type == "Multiple Choice":
|
@@ -143,6 +161,7 @@ Please format the output with:
|
|
143 |
time.sleep(2)
|
144 |
|
145 |
# ระบบ login
|
|
|
146 |
if 'username' not in st.session_state:
|
147 |
st.title("Login")
|
148 |
username_input = st.text_input("Enter your username:")
|
@@ -157,106 +176,74 @@ if 'username' not in st.session_state:
|
|
157 |
else:
|
158 |
st.warning("Please enter a valid username.")
|
159 |
else:
|
160 |
-
# Main App after login
|
161 |
st.title(f"Welcome, {st.session_state['username']}! Generate your exam questions")
|
162 |
-
|
163 |
-
# Input field for knowledge material (text) with 3,000-word limit
|
164 |
-
knowledge_material = st.text_area("Enter knowledge material to generate exam questions:")
|
165 |
|
166 |
-
#
|
167 |
-
|
168 |
-
st.warning("Please limit the knowledge material to 3,000 words or fewer.")
|
169 |
-
|
170 |
-
# File uploader for PDFs (limited to 5 MB)
|
171 |
-
uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf")
|
172 |
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
st.
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"])
|
191 |
-
|
192 |
-
# Checkbox for Case-Based Medical Situations
|
193 |
-
case_based = st.checkbox("Generate case-based medical exam questions")
|
194 |
|
195 |
-
#
|
196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
# Generate questions button
|
199 |
-
if 'previous_questions' not in st.session_state:
|
200 |
-
st.session_state['previous_questions'] = []
|
201 |
-
|
202 |
if st.button("Generate Questions"):
|
203 |
-
if
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
if questions:
|
215 |
-
st.write("Generated Exam Questions:")
|
216 |
-
st.write(questions)
|
217 |
-
|
218 |
-
# Avoid showing repeated content in future requests
|
219 |
-
st.session_state['previous_questions'].append(questions)
|
220 |
-
|
221 |
-
# Option to download the questions as a text file
|
222 |
-
st.download_button(
|
223 |
-
label="Download Questions",
|
224 |
-
data=questions,
|
225 |
-
file_name='generated_questions.txt',
|
226 |
-
mime='text/plain'
|
227 |
-
)
|
228 |
-
else:
|
229 |
-
st.warning("Please reduce the word count to 3,000 or fewer.")
|
230 |
-
|
231 |
-
# Button to generate more questions based on the same material
|
232 |
-
if st.button("Generate More Questions"):
|
233 |
-
if len(knowledge_material.split()) <= 3000:
|
234 |
-
# Regenerate new questions, trying to avoid repeated content
|
235 |
-
questions = generate_questions_with_retry(
|
236 |
-
knowledge_material,
|
237 |
-
question_type,
|
238 |
-
cognitive_level,
|
239 |
-
extra_instructions,
|
240 |
-
case_based,
|
241 |
-
num_choices
|
242 |
-
)
|
243 |
-
|
244 |
-
# Check if the new set of questions is not the same as the previous set
|
245 |
-
if questions and questions not in st.session_state['previous_questions']:
|
246 |
-
st.write("Generated More Exam Questions:")
|
247 |
-
st.write(questions)
|
248 |
-
|
249 |
-
# Append the new questions to the session state
|
250 |
-
st.session_state['previous_questions'].append(questions)
|
251 |
-
|
252 |
-
# Option to download the new set of questions
|
253 |
-
st.download_button(
|
254 |
-
label="Download More Questions",
|
255 |
-
data=questions,
|
256 |
-
file_name='more_generated_questions.txt',
|
257 |
-
mime='text/plain'
|
258 |
)
|
259 |
-
|
260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
else:
|
262 |
-
st.warning("Please
|
|
|
8 |
# Set up OpenAI client
|
9 |
client = OpenAI(api_key=st.secrets["OPENAI_API_KEY"])
|
10 |
|
11 |
+
# Constants
|
12 |
+
WORD_LIMIT = 8000
|
13 |
+
|
14 |
# Google Sheets setup remains the same
|
15 |
scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
|
16 |
creds = ServiceAccountCredentials.from_json_keyfile_name("genexam-2c8c645ecc0d.json", scope)
|
|
|
37 |
except Exception as e:
|
38 |
st.error(f"Error updating API usage: {str(e)}")
|
39 |
|
40 |
+
def extract_text_from_pdf(pdf_file):
|
41 |
+
"""Simple PDF text extraction with word limit check"""
|
42 |
+
try:
|
43 |
+
pdf_reader = PyPDF2.PdfReader(io.BytesIO(pdf_file.read()))
|
44 |
+
text_content = ""
|
45 |
+
for page in pdf_reader.pages:
|
46 |
+
text_content += page.extract_text() + "\n"
|
47 |
+
|
48 |
+
word_count = len(text_content.split())
|
49 |
+
if word_count > WORD_LIMIT:
|
50 |
+
return None, f"PDF content exceeds {WORD_LIMIT:,} words (contains {word_count:,} words). Please use a shorter document."
|
51 |
+
return text_content, None
|
52 |
+
except Exception as e:
|
53 |
+
return None, f"Error processing PDF: {str(e)}"
|
54 |
+
|
55 |
def generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions, case_based, num_choices=None, max_retries=3):
|
56 |
# Adjust number of questions based on type
|
57 |
if question_type == "Multiple Choice":
|
|
|
161 |
time.sleep(2)
|
162 |
|
163 |
# ระบบ login
|
164 |
+
# Main Streamlit interface
|
165 |
if 'username' not in st.session_state:
|
166 |
st.title("Login")
|
167 |
username_input = st.text_input("Enter your username:")
|
|
|
176 |
else:
|
177 |
st.warning("Please enter a valid username.")
|
178 |
else:
|
|
|
179 |
st.title(f"Welcome, {st.session_state['username']}! Generate your exam questions")
|
|
|
|
|
|
|
180 |
|
181 |
+
# Create tabs for input methods
|
182 |
+
tab1, tab2 = st.tabs(["Text Input", "PDF Upload"])
|
|
|
|
|
|
|
|
|
183 |
|
184 |
+
with tab1:
|
185 |
+
knowledge_material = st.text_area("Enter knowledge material to generate exam questions:")
|
186 |
+
word_count = len(knowledge_material.split())
|
187 |
+
if word_count > WORD_LIMIT:
|
188 |
+
st.error(f"Text exceeds {WORD_LIMIT:,} words. Please shorten your content.")
|
189 |
+
|
190 |
+
with tab2:
|
191 |
+
st.info(f"Maximum content length: {WORD_LIMIT:,} words")
|
192 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf")
|
193 |
+
|
194 |
+
if uploaded_file is not None:
|
195 |
+
pdf_content, error = extract_text_from_pdf(uploaded_file)
|
196 |
+
if error:
|
197 |
+
st.error(error)
|
198 |
+
else:
|
199 |
+
st.success("PDF processed successfully!")
|
200 |
+
knowledge_material = pdf_content
|
|
|
|
|
|
|
|
|
201 |
|
202 |
+
# Question generation options
|
203 |
+
col1, col2 = st.columns(2)
|
204 |
+
|
205 |
+
with col1:
|
206 |
+
question_type = st.selectbox(
|
207 |
+
"Select question type:",
|
208 |
+
["Multiple Choice", "Fill in the Blank", "Open-ended", "True/False"]
|
209 |
+
)
|
210 |
+
|
211 |
+
if question_type == "Multiple Choice":
|
212 |
+
num_choices = st.selectbox("Select number of choices:", [3, 4, 5])
|
213 |
+
|
214 |
+
cognitive_level = st.selectbox(
|
215 |
+
"Select cognitive level:",
|
216 |
+
["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"]
|
217 |
+
)
|
218 |
+
|
219 |
+
with col2:
|
220 |
+
case_based = st.checkbox("Generate case-based medical exam questions")
|
221 |
+
extra_instructions = st.text_area("Additional instructions (optional):")
|
222 |
|
223 |
# Generate questions button
|
|
|
|
|
|
|
224 |
if st.button("Generate Questions"):
|
225 |
+
if 'knowledge_material' in locals() and knowledge_material.strip():
|
226 |
+
with st.spinner("Generating questions..."):
|
227 |
+
# Your existing generate_questions_with_retry function call here
|
228 |
+
questions = generate_questions_with_retry(
|
229 |
+
knowledge_material,
|
230 |
+
question_type,
|
231 |
+
cognitive_level,
|
232 |
+
extra_instructions,
|
233 |
+
case_based,
|
234 |
+
num_choices if question_type == "Multiple Choice" else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
)
|
236 |
+
|
237 |
+
if questions:
|
238 |
+
st.write("### Generated Exam Questions:")
|
239 |
+
st.write(questions)
|
240 |
+
|
241 |
+
# Download button
|
242 |
+
st.download_button(
|
243 |
+
label="Download Questions",
|
244 |
+
data=questions,
|
245 |
+
file_name='generated_questions.txt',
|
246 |
+
mime='text/plain'
|
247 |
+
)
|
248 |
else:
|
249 |
+
st.warning("Please enter knowledge material or upload a PDF file first.")
|