Rathapoom commited on
Commit
a9150c4
·
verified ·
1 Parent(s): 9f4a1d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -7
app.py CHANGED
@@ -9,7 +9,7 @@ openai.api_key = st.secrets["OPENAI_API_KEY"]
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
@@ -18,15 +18,24 @@ def generate_questions_with_retry(knowledge_material, question_type, cognitive_l
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}
@@ -61,13 +70,17 @@ else:
61
  # Word count check
62
  if len(knowledge_material.split()) > 3000:
63
  st.warning("Please limit the knowledge material to 3,000 words or fewer.")
64
-
65
  # File uploader for PDFs (limited to 5 MB)
66
  uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf")
67
 
68
  if uploaded_file is not None:
69
  if uploaded_file.size > 5 * 1024 * 1024: # 5 MB limit
70
  st.warning("File size exceeds 5 MB. Please upload a smaller file.")
 
 
 
 
71
 
72
  # Select question type
73
  question_type = st.selectbox("Select question type:",
@@ -77,6 +90,9 @@ else:
77
  cognitive_level = st.selectbox("Select cognitive level:",
78
  ["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"])
79
 
 
 
 
80
  # Extra input field for additional instructions (placed below cognitive level)
81
  extra_instructions = st.text_area("Enter additional instructions (e.g., how you want the questions to be phrased):")
82
 
@@ -87,7 +103,13 @@ else:
87
  if st.button("Generate Questions"):
88
  if len(knowledge_material.split()) <= 3000:
89
  # Generate questions with retry logic
90
- questions = generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions)
 
 
 
 
 
 
91
 
92
  if questions:
93
  st.write("Generated Exam Questions:")
@@ -110,7 +132,13 @@ else:
110
  if st.button("Generate More Questions"):
111
  if len(knowledge_material.split()) <= 3000:
112
  # Regenerate new questions, trying to avoid repeated content
113
- questions = generate_questions_with_retry(knowledge_material, question_type, cognitive_level, extra_instructions)
 
 
 
 
 
 
114
 
115
  # Check if the new set of questions is not the same as the previous set
116
  if questions and questions not in st.session_state['previous_questions']:
 
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, case_based, max_retries=3):
13
  # Adjust the number of questions based on the type
14
  if question_type == "Multiple Choice":
15
  num_questions = 3
 
18
  else: # Open-ended
19
  num_questions = 3
20
 
21
+ # Base prompt
22
  prompt = f"Generate {num_questions} {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}. {extra_instructions}"
23
+
24
+ # If case-based medical situation is selected, modify the prompt
25
+ if case_based:
26
+ prompt = f"Generate {num_questions} {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following medical material: {knowledge_material}. The questions should be based on case-based medical situations, such as patient scenarios. {extra_instructions}"
27
+ if question_type != "Fill in the Blank":
28
+ prompt += " Provide answers with short explanations."
29
+
30
+ else:
31
+ if question_type != "Fill in the Blank":
32
+ prompt += " Provide answers with short explanations."
33
 
34
  retries = 0
35
  while retries < max_retries:
36
  try:
37
  response = client.chat.completions.create(
38
+ model="gpt-4o-mini",
39
  messages=[
40
  {"role": "system", "content": "You are a helpful assistant for generating exam questions."},
41
  {"role": "user", "content": prompt}
 
70
  # Word count check
71
  if len(knowledge_material.split()) > 3000:
72
  st.warning("Please limit the knowledge material to 3,000 words or fewer.")
73
+
74
  # File uploader for PDFs (limited to 5 MB)
75
  uploaded_file = st.file_uploader("Upload a file (PDF)", type="pdf")
76
 
77
  if uploaded_file is not None:
78
  if uploaded_file.size > 5 * 1024 * 1024: # 5 MB limit
79
  st.warning("File size exceeds 5 MB. Please upload a smaller file.")
80
+ else:
81
+ # Here you can add code to extract text from the PDF if needed
82
+ # For simplicity, we're focusing on the text input for now
83
+ st.success("File uploaded successfully! (Text extraction not implemented yet.)")
84
 
85
  # Select question type
86
  question_type = st.selectbox("Select question type:",
 
90
  cognitive_level = st.selectbox("Select cognitive level:",
91
  ["Recall", "Understanding", "Application", "Analysis", "Synthesis", "Evaluation"])
92
 
93
+ # Checkbox for Case-Based Medical Situations
94
+ case_based = st.checkbox("Generate case-based medical exam questions")
95
+
96
  # Extra input field for additional instructions (placed below cognitive level)
97
  extra_instructions = st.text_area("Enter additional instructions (e.g., how you want the questions to be phrased):")
98
 
 
103
  if st.button("Generate Questions"):
104
  if len(knowledge_material.split()) <= 3000:
105
  # Generate questions with retry logic
106
+ questions = generate_questions_with_retry(
107
+ knowledge_material,
108
+ question_type,
109
+ cognitive_level,
110
+ extra_instructions,
111
+ case_based
112
+ )
113
 
114
  if questions:
115
  st.write("Generated Exam Questions:")
 
132
  if st.button("Generate More Questions"):
133
  if len(knowledge_material.split()) <= 3000:
134
  # Regenerate new questions, trying to avoid repeated content
135
+ questions = generate_questions_with_retry(
136
+ knowledge_material,
137
+ question_type,
138
+ cognitive_level,
139
+ extra_instructions,
140
+ case_based
141
+ )
142
 
143
  # Check if the new set of questions is not the same as the previous set
144
  if questions and questions not in st.session_state['previous_questions']: