Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,39 +17,68 @@ class QuizApp:
|
|
| 17 |
self.user_data = {}
|
| 18 |
|
| 19 |
def generate_questions(self, text, num_questions):
|
| 20 |
-
prompt = f"""
|
| 21 |
{text}
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
[
|
| 25 |
{{
|
| 26 |
-
"question": "Question text",
|
| 27 |
-
"options": [
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
}}
|
| 30 |
]
|
| 31 |
Only return the JSON array, no other text."""
|
| 32 |
|
| 33 |
try:
|
| 34 |
response = client.chat.completions.create(
|
| 35 |
-
messages=[
|
|
|
|
|
|
|
|
|
|
| 36 |
model="llama-3.2-3b-preview",
|
| 37 |
-
temperature=0.
|
| 38 |
-
max_tokens=
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
| 43 |
if isinstance(questions, list):
|
| 44 |
self.current_questions = questions
|
| 45 |
else:
|
| 46 |
-
# If we get an object with a questions key, extract the list
|
| 47 |
self.current_questions = questions.get("questions", [])
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return json.dumps(self.current_questions, indent=2)
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
print(f"Error generating questions: {e}")
|
| 52 |
-
return json.dumps({
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
def calculate_score(self, answers):
|
| 55 |
try:
|
|
@@ -125,86 +154,72 @@ class QuizApp:
|
|
| 125 |
print(f"Error generating certificate: {e}")
|
| 126 |
return None
|
| 127 |
|
| 128 |
-
def create_quiz_app():
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
with gr.Blocks(title="CertifyMe AI") as demo:
|
| 132 |
-
# Store current tab state
|
| 133 |
-
current_tab = gr.State(0)
|
| 134 |
-
|
| 135 |
-
gr.Markdown("""
|
| 136 |
-
# CertifyMe AI
|
| 137 |
-
### Transform Your Knowledge into Recognized Achievements
|
| 138 |
-
Get AI-powered assessments and professional certificates for any learning content.
|
| 139 |
-
""")
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
)
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
)
|
| 173 |
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
with gr.Tab("📝 Step 2: Take Assessment", id=1) as tab2:
|
| 188 |
-
gr.Markdown("""
|
| 189 |
-
### Assessment Instructions:
|
| 190 |
-
1. Review each question carefully
|
| 191 |
-
2. For each question, you may select one or more correct options
|
| 192 |
-
3. Enter your answers in the following format:
|
| 193 |
-
```
|
| 194 |
-
[
|
| 195 |
-
[0], // First question: selected first option
|
| 196 |
-
[1, 2], // Second question: selected second and third options
|
| 197 |
-
[3] // Third question: selected fourth option
|
| 198 |
-
]
|
| 199 |
-
```
|
| 200 |
-
4. Click 'Submit Assessment' when you're ready
|
| 201 |
-
5. You need 80% or higher to earn your certificate
|
| 202 |
-
""")
|
| 203 |
|
| 204 |
-
questions_display = gr.JSON(label="Assessment Questions")
|
| 205 |
-
answers_input = gr.JSON(label="Your Answers")
|
| 206 |
-
submit_btn = gr.Button("Submit Assessment", variant="primary")
|
| 207 |
-
score_display = gr.Number(label="Your Score")
|
| 208 |
|
| 209 |
# Step 3: Get Certified
|
| 210 |
with gr.Tab("🎓 Step 3: Get Certified", id=2) as tab3:
|
|
@@ -223,19 +238,24 @@ def create_quiz_app():
|
|
| 223 |
certificate_display = gr.Image(label="Your Certificate")
|
| 224 |
|
| 225 |
# Helper functions for tab navigation
|
| 226 |
-
|
| 227 |
questions = quiz_app.generate_questions(text, num_questions)
|
| 228 |
-
return questions, 1
|
| 229 |
-
|
| 230 |
-
def
|
| 231 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
return score, 2
|
| 233 |
|
| 234 |
# Event handlers
|
| 235 |
generate_btn.click(
|
| 236 |
fn=generate_and_switch_tab,
|
| 237 |
inputs=[text_input, num_questions],
|
| 238 |
-
outputs=[questions_display, current_tab]
|
| 239 |
).then(
|
| 240 |
fn=lambda tab: gr.Tabs(selected=tab),
|
| 241 |
inputs=[current_tab],
|
|
@@ -243,8 +263,8 @@ def create_quiz_app():
|
|
| 243 |
)
|
| 244 |
|
| 245 |
submit_btn.click(
|
| 246 |
-
fn=
|
| 247 |
-
inputs=[
|
| 248 |
outputs=[score_display, current_tab]
|
| 249 |
).then(
|
| 250 |
fn=lambda tab: gr.Tabs(selected=tab),
|
|
@@ -252,6 +272,7 @@ def create_quiz_app():
|
|
| 252 |
outputs=[tabs]
|
| 253 |
)
|
| 254 |
|
|
|
|
| 255 |
score_display.change(
|
| 256 |
fn=lambda score, user_name, course, logo, photo: (
|
| 257 |
quiz_app.generate_certificate(user_name, score, course, logo, photo)
|
|
@@ -263,6 +284,7 @@ def create_quiz_app():
|
|
| 263 |
|
| 264 |
return demo
|
| 265 |
|
|
|
|
| 266 |
if __name__ == "__main__":
|
| 267 |
if not os.getenv("GROQ_API_KEY"):
|
| 268 |
print("Please set your GROQ_API_KEY environment variable")
|
|
|
|
| 17 |
self.user_data = {}
|
| 18 |
|
| 19 |
def generate_questions(self, text, num_questions):
|
| 20 |
+
prompt = f"""Create {num_questions} multiple choice questions based on this text:
|
| 21 |
{text}
|
| 22 |
|
| 23 |
+
Each question should:
|
| 24 |
+
1. Have a clear, concise question text
|
| 25 |
+
2. Have exactly 4 options
|
| 26 |
+
3. Have only one correct answer
|
| 27 |
+
4. Be educational and test understanding
|
| 28 |
+
|
| 29 |
+
Return the questions in this JSON format:
|
| 30 |
[
|
| 31 |
{{
|
| 32 |
+
"question": "Question text?",
|
| 33 |
+
"options": [
|
| 34 |
+
"Correct answer",
|
| 35 |
+
"Wrong answer 1",
|
| 36 |
+
"Wrong answer 2",
|
| 37 |
+
"Wrong answer 3"
|
| 38 |
+
],
|
| 39 |
+
"correct_answers": [0]
|
| 40 |
}}
|
| 41 |
]
|
| 42 |
Only return the JSON array, no other text."""
|
| 43 |
|
| 44 |
try:
|
| 45 |
response = client.chat.completions.create(
|
| 46 |
+
messages=[
|
| 47 |
+
{"role": "system", "content": "You are a quiz generator that creates clear, single-choice questions."},
|
| 48 |
+
{"role": "user", "content": prompt}
|
| 49 |
+
],
|
| 50 |
model="llama-3.2-3b-preview",
|
| 51 |
+
temperature=0.5,
|
| 52 |
+
max_tokens=2048,
|
| 53 |
+
top_p=0.9
|
| 54 |
)
|
| 55 |
|
| 56 |
+
response_text = response.choices[0].message.content.strip()
|
| 57 |
+
# Remove any markdown formatting
|
| 58 |
+
response_text = response_text.replace("```json", "").replace("```", "").strip()
|
| 59 |
+
|
| 60 |
+
questions = json.loads(response_text)
|
| 61 |
if isinstance(questions, list):
|
| 62 |
self.current_questions = questions
|
| 63 |
else:
|
|
|
|
| 64 |
self.current_questions = questions.get("questions", [])
|
| 65 |
|
| 66 |
+
# Validate question format
|
| 67 |
+
for q in self.current_questions:
|
| 68 |
+
if not all(key in q for key in ["question", "options", "correct_answers"]):
|
| 69 |
+
raise ValueError("Invalid question format")
|
| 70 |
+
if len(q["options"]) != 4:
|
| 71 |
+
raise ValueError("Each question must have exactly 4 options")
|
| 72 |
+
|
| 73 |
return json.dumps(self.current_questions, indent=2)
|
| 74 |
+
|
| 75 |
except Exception as e:
|
| 76 |
print(f"Error generating questions: {e}")
|
| 77 |
+
return json.dumps([{
|
| 78 |
+
"question": "Error generating questions. Please try again.",
|
| 79 |
+
"options": ["Error", "Error", "Error", "Error"],
|
| 80 |
+
"correct_answers": [0]
|
| 81 |
+
}])
|
| 82 |
|
| 83 |
def calculate_score(self, answers):
|
| 84 |
try:
|
|
|
|
| 154 |
print(f"Error generating certificate: {e}")
|
| 155 |
return None
|
| 156 |
|
| 157 |
+
def create_quiz_app():
|
| 158 |
+
quiz_app = QuizApp()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
|
| 160 |
+
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
| 161 |
+
current_tab = gr.State(0)
|
| 162 |
+
current_questions = gr.State([])
|
| 163 |
+
user_answers = gr.State([])
|
| 164 |
+
|
| 165 |
+
gr.Markdown("""
|
| 166 |
+
# 🎓 CertifyMe AI
|
| 167 |
+
### Transform Your Knowledge into Recognized Achievements
|
| 168 |
+
""")
|
| 169 |
+
|
| 170 |
+
with gr.Tabs() as tabs:
|
| 171 |
+
# Step 1: Profile Setup (remains largely the same)
|
| 172 |
+
with gr.Tab("📋 Step 1: Profile Setup", id=0) as tab1:
|
| 173 |
+
# ... (previous profile setup code remains the same)
|
| 174 |
|
| 175 |
+
# Step 2: Take Assessment (completely revised)
|
| 176 |
+
with gr.Tab("📝 Step 2: Take Assessment", id=1) as tab2:
|
| 177 |
+
def format_questions(questions_json):
|
| 178 |
+
try:
|
| 179 |
+
questions = json.loads(questions_json) if isinstance(questions_json, str) else questions_json
|
| 180 |
+
return questions
|
| 181 |
+
except:
|
| 182 |
+
return []
|
| 183 |
+
|
| 184 |
+
def display_questions(questions):
|
| 185 |
+
with gr.Group() as quiz_container:
|
| 186 |
+
answers = []
|
| 187 |
+
for i, q in enumerate(questions):
|
| 188 |
+
gr.Markdown(f"### Question {i+1}")
|
| 189 |
+
gr.Markdown(q["question"])
|
| 190 |
+
radio = gr.Radio(
|
| 191 |
+
choices=q["options"],
|
| 192 |
+
label="Select your answer",
|
| 193 |
+
value=None,
|
| 194 |
+
visible=True
|
| 195 |
+
)
|
| 196 |
+
answers.append(radio)
|
| 197 |
+
return quiz_container
|
| 198 |
|
| 199 |
+
def process_answers(answers):
|
| 200 |
+
processed = []
|
| 201 |
+
for i, ans in enumerate(answers):
|
| 202 |
+
if ans is not None:
|
| 203 |
+
processed.append([i])
|
| 204 |
+
return processed
|
| 205 |
+
|
| 206 |
+
questions_display = gr.JSON(visible=False) # Hidden JSON storage
|
| 207 |
+
quiz_area = gr.Group(visible=False) # Container for dynamic quiz content
|
| 208 |
|
| 209 |
+
submit_btn = gr.Button("Submit Assessment", variant="primary", visible=False)
|
| 210 |
+
score_display = gr.Number(label="Your Score", visible=False)
|
| 211 |
+
|
| 212 |
+
# Update the question display when questions are generated
|
| 213 |
+
questions_display.change(
|
| 214 |
+
fn=lambda q: (
|
| 215 |
+
gr.Group(visible=True),
|
| 216 |
+
gr.Button(visible=True),
|
| 217 |
+
format_questions(q)
|
| 218 |
+
),
|
| 219 |
+
inputs=[questions_display],
|
| 220 |
+
outputs=[quiz_area, submit_btn, current_questions]
|
| 221 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
|
| 224 |
# Step 3: Get Certified
|
| 225 |
with gr.Tab("🎓 Step 3: Get Certified", id=2) as tab3:
|
|
|
|
| 238 |
certificate_display = gr.Image(label="Your Certificate")
|
| 239 |
|
| 240 |
# Helper functions for tab navigation
|
| 241 |
+
def generate_and_switch_tab(text, num_questions):
|
| 242 |
questions = quiz_app.generate_questions(text, num_questions)
|
| 243 |
+
return questions, 1, gr.Markdown(visible=True), gr.Button(visible=True)
|
| 244 |
+
|
| 245 |
+
def calculate_score(selected_answers, questions):
|
| 246 |
+
correct = 0
|
| 247 |
+
total = len(questions)
|
| 248 |
+
for q, ans in zip(questions, selected_answers):
|
| 249 |
+
if set(ans) == set(q["correct_answers"]):
|
| 250 |
+
correct += 1
|
| 251 |
+
score = (correct / total) * 100 if total > 0 else 0
|
| 252 |
return score, 2
|
| 253 |
|
| 254 |
# Event handlers
|
| 255 |
generate_btn.click(
|
| 256 |
fn=generate_and_switch_tab,
|
| 257 |
inputs=[text_input, num_questions],
|
| 258 |
+
outputs=[questions_display, current_tab, quiz_area, submit_btn]
|
| 259 |
).then(
|
| 260 |
fn=lambda tab: gr.Tabs(selected=tab),
|
| 261 |
inputs=[current_tab],
|
|
|
|
| 263 |
)
|
| 264 |
|
| 265 |
submit_btn.click(
|
| 266 |
+
fn=calculate_score,
|
| 267 |
+
inputs=[user_answers, current_questions],
|
| 268 |
outputs=[score_display, current_tab]
|
| 269 |
).then(
|
| 270 |
fn=lambda tab: gr.Tabs(selected=tab),
|
|
|
|
| 272 |
outputs=[tabs]
|
| 273 |
)
|
| 274 |
|
| 275 |
+
# Certificate generation remains the same
|
| 276 |
score_display.change(
|
| 277 |
fn=lambda score, user_name, course, logo, photo: (
|
| 278 |
quiz_app.generate_certificate(user_name, score, course, logo, photo)
|
|
|
|
| 284 |
|
| 285 |
return demo
|
| 286 |
|
| 287 |
+
# Main execution remains the same
|
| 288 |
if __name__ == "__main__":
|
| 289 |
if not os.getenv("GROQ_API_KEY"):
|
| 290 |
print("Please set your GROQ_API_KEY environment variable")
|