Rathapoom commited on
Commit
58d3b83
1 Parent(s): dbd52a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -13
app.py CHANGED
@@ -38,46 +38,109 @@ def generate_questions_with_retry(knowledge_material, question_type, cognitive_l
38
  # Adjust number of questions based on type
39
  if question_type == "Multiple Choice":
40
  num_questions = 3
 
 
 
 
 
 
 
 
 
41
  elif question_type == "Fill in the Blank":
42
  num_questions = 10
 
 
 
 
 
 
 
 
43
  elif question_type == "True/False":
44
  num_questions = 5
 
 
 
 
 
 
 
 
45
  else: # Open-ended
46
  num_questions = 3
 
 
 
 
 
 
 
 
 
 
47
 
48
  # Base prompt
49
- prompt = f"Generate {num_questions} {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material: {knowledge_material}. {extra_instructions}"
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
  # Modify prompt for case-based medical situations
52
  if case_based:
53
- 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}"
54
- if question_type != "Fill in the Blank":
55
- prompt += " Provide answers with short explanations."
 
 
 
 
 
 
 
 
 
56
 
57
- # Add specific handling for Multiple Choice and True/False
58
- if question_type == "Multiple Choice" and num_choices:
59
- prompt += f" Each multiple choice question should have {num_choices} choices."
60
 
61
- if question_type == "True/False":
62
- prompt += " Provide short explanations for each question based on the given material, without stating True or False explicitly."
 
 
 
 
 
63
 
64
  retries = 0
65
  while retries < max_retries:
66
  try:
67
  response = client.chat.completions.create(
68
- model="gpt-4o-mini", # or your preferred model
69
  messages=[
70
- {"role": "system", "content": "You are a helpful assistant for generating exam questions."},
71
  {"role": "user", "content": prompt}
72
- ]
 
 
73
  )
74
  return response.choices[0].message.content
75
  except Exception as e:
76
  retries += 1
 
77
  if retries == max_retries:
78
  st.error(f"Failed to generate questions after {max_retries} attempts. Error: {str(e)}")
79
  return None
80
- time.sleep(2) # Wait before retrying
81
 
82
  # ระบบ login
83
  if 'username' not in st.session_state:
 
38
  # Adjust number of questions based on type
39
  if question_type == "Multiple Choice":
40
  num_questions = 3
41
+ format_instructions = f"""
42
+ For each multiple choice question:
43
+ 1. Present the question clearly
44
+ 2. Provide {num_choices} choices labeled with A, B, C{', D' if num_choices > 3 else ''}{', E' if num_choices > 4 else ''}
45
+ 3. After all questions, provide an ANSWER KEY section with:
46
+ - The correct answer letter for each question
47
+ - A brief explanation of why this is the correct answer
48
+ - Why other options are incorrect
49
+ """
50
  elif question_type == "Fill in the Blank":
51
  num_questions = 10
52
+ format_instructions = """
53
+ For each fill-in-the-blank question:
54
+ 1. Present the question with a clear blank space indicated by _____
55
+ 2. After all questions, provide an ANSWER KEY section with:
56
+ - The correct answer for each blank
57
+ - A brief explanation of why this answer is correct
58
+ - Any alternative acceptable answers if applicable
59
+ """
60
  elif question_type == "True/False":
61
  num_questions = 5
62
+ format_instructions = """
63
+ For each true/false question:
64
+ 1. Present the statement clearly
65
+ 2. After all questions, provide an ANSWER KEY section with:
66
+ - Whether the statement is True or False
67
+ - A detailed explanation of why the statement is true or false
68
+ - The specific part of the source material that supports this answer
69
+ """
70
  else: # Open-ended
71
  num_questions = 3
72
+ format_instructions = """
73
+ For each open-ended question:
74
+ 1. Present the question clearly
75
+ 2. After all questions, provide an ANSWER KEY section with:
76
+ - A structured scoring checklist of key points (minimum 3-5 points per question)
77
+ - Each key point should be worth a specific number of marks
78
+ - Total marks available for each question
79
+ - Sample answer that would receive full marks
80
+ - Common points that students might miss
81
+ """
82
 
83
  # Base prompt
84
+ prompt = f"""Generate {num_questions} {question_type.lower()} exam questions based on {cognitive_level.lower()} level from the following material:
85
+
86
+ {knowledge_material}
87
+
88
+ {format_instructions}
89
+
90
+ {extra_instructions}
91
+
92
+ Please format the output clearly with:
93
+ 1. Questions section (numbered 1, 2, 3, etc.)
94
+ 2. Answer Key section (clearly separated from questions)
95
+ 3. Each answer should include explanation for better understanding
96
+
97
+ Make sure all questions and answers are directly related to the provided material."""
98
 
99
  # Modify prompt for case-based medical situations
100
  if case_based:
101
+ prompt = f"""Generate {num_questions} {question_type.lower()} case-based medical exam questions based on {cognitive_level.lower()} level.
102
+
103
+ Use this material as the medical knowledge base:
104
+ {knowledge_material}
105
+
106
+ Each question should:
107
+ 1. Start with a medical case scenario/patient presentation
108
+ 2. Include relevant clinical details
109
+ 3. Ask about diagnosis, treatment, or management
110
+ 4. Be at {cognitive_level.lower()} cognitive level
111
+
112
+ {format_instructions}
113
 
114
+ {extra_instructions}
 
 
115
 
116
+ Please format the output with:
117
+ 1. Cases and Questions (numbered 1, 2, 3, etc.)
118
+ 2. Detailed Answer Key section including:
119
+ - Correct answers
120
+ - Clinical reasoning
121
+ - Key diagnostic or treatment considerations
122
+ - Common pitfalls to avoid"""
123
 
124
  retries = 0
125
  while retries < max_retries:
126
  try:
127
  response = client.chat.completions.create(
128
+ model="gpt-4o-mini",
129
  messages=[
130
+ {"role": "system", "content": "You are an expert exam question generator with deep knowledge in medical education. Create clear, well-structured questions with detailed answer keys and explanations."},
131
  {"role": "user", "content": prompt}
132
+ ],
133
+ temperature=0.7,
134
+ max_tokens=3000 # Increased to accommodate answers and explanations
135
  )
136
  return response.choices[0].message.content
137
  except Exception as e:
138
  retries += 1
139
+ st.warning(f"Attempt {retries} failed. Retrying... Error: {str(e)}")
140
  if retries == max_retries:
141
  st.error(f"Failed to generate questions after {max_retries} attempts. Error: {str(e)}")
142
  return None
143
+ time.sleep(2)
144
 
145
  # ระบบ login
146
  if 'username' not in st.session_state: