Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import time | |
| import json | |
| import random | |
| import os | |
| # Fixed Generative AI Questions | |
| FIXED_QUESTIONS = [ | |
| { | |
| "question": "What does the 'attention' mechanism allow a transformer to do?", | |
| "options": [ | |
| "Focus on important parts of the input", | |
| "Increase model speed", | |
| "Reduce token size", | |
| "Remove training data" | |
| ], | |
| "answer": "Focus on important parts of the input" | |
| }, | |
| { | |
| "question": "Which model architecture powers Large Language Models (LLMs)?", | |
| "options": [ | |
| "CNN", | |
| "Transformer", | |
| "RNN", | |
| "GAN" | |
| ], | |
| "answer": "Transformer" | |
| }, | |
| { | |
| "question": "What is a hallucination in Generative AI?", | |
| "options": [ | |
| "Model overheating", | |
| "Model generating false information", | |
| "A GPU error", | |
| "Slow inference speed" | |
| ], | |
| "answer": "Model generating false information" | |
| }, | |
| { | |
| "question": "What is the function of embeddings?", | |
| "options": [ | |
| "Convert text into numeric vectors", | |
| "Render images", | |
| "Store training data", | |
| "Speed up GPUs" | |
| ], | |
| "answer": "Convert text into numeric vectors" | |
| }, | |
| { | |
| "question": "Which technique is used by models like Stable Diffusion?", | |
| "options": [ | |
| "Reinforcement learning", | |
| "Diffusion process", | |
| "GAN filtering", | |
| "Quantum sampling" | |
| ], | |
| "answer": "Diffusion process" | |
| } | |
| ] | |
| # Shuffle for variety | |
| def load_fixed_questions(): | |
| q = FIXED_QUESTIONS.copy() | |
| random.shuffle(q) | |
| return q | |
| # TIMER & SCORING | |
| def start_quiz(mode): | |
| """Initialize quiz session.""" | |
| if mode == "Fixed Generative AI Questions": | |
| questions = load_fixed_questions() | |
| else: | |
| questions = [] # Filled later for AI-generated mode | |
| return ( | |
| questions, | |
| 0, # score | |
| 0, # current question index | |
| False, # quiz finished | |
| 60 # timer (seconds) | |
| ) | |
| def submit_answer(questions, index, score, user_answer): | |
| """Check answer and move forward.""" | |
| if not questions: | |
| return score, index, False, "No questions loaded." | |
| correct = questions[index]["answer"] | |
| if user_answer == correct: | |
| score += 1 | |
| index += 1 | |
| finished = index >= len(questions) | |
| return score, index, finished, f"Correct answer: {correct}" | |
| # Gradio UI | |
| with gr.Blocks(title="Generative AI Quiz") as demo: | |
| gr.Markdown("## 🎯 **Generative AI Quiz App (Fixed Questions + Timer + Scoring)**") | |
| mode = gr.Radio( | |
| ["Fixed Generative AI Questions"], | |
| label="Choose Quiz Mode", | |
| value="Fixed Generative AI Questions" | |
| ) | |
| start_btn = gr.Button("Start Quiz") | |
| timer_display = gr.Markdown("⏳ **Time Left: 60 seconds**") | |
| question_box = gr.Markdown("") | |
| options_box = gr.Radio([], label="Choose your answer:") | |
| next_btn = gr.Button("Submit Answer", visible=False) | |
| result_box = gr.Markdown("") | |
| # State variables | |
| questions_state = gr.State([]) | |
| score_state = gr.State(0) | |
| index_state = gr.State(0) | |
| finished_state = gr.State(False) | |
| timer_state = gr.State(60) | |
| # Start quiz | |
| def on_start(mode): | |
| questions, score, index, finished, timer = start_quiz(mode) | |
| q = questions[index] | |
| return ( | |
| questions, | |
| score, | |
| index, | |
| finished, | |
| timer, | |
| f"⏳ **Time Left: {timer} seconds**", | |
| f"### Q1: {q['question']}", | |
| gr.Radio(choices=q["options"]), | |
| gr.update(visible=True), | |
| "" | |
| ) | |
| start_btn.click( | |
| on_start, | |
| inputs=[mode], | |
| outputs=[ | |
| questions_state, | |
| score_state, | |
| index_state, | |
| finished_state, | |
| timer_state, | |
| timer_display, | |
| question_box, | |
| options_box, | |
| next_btn, | |
| result_box, | |
| ] | |
| ) | |
| # Timer logic | |
| def countdown(timer): | |
| if timer <= 0: | |
| return 0, "⏳ **Time Left: 0 seconds — TIME UP!**" | |
| timer -= 1 | |
| return timer, f"⏳ **Time Left: {timer} seconds**" | |
| demo.load( | |
| countdown, | |
| inputs=[timer_state], | |
| outputs=[timer_state, timer_display], | |
| # every=1 | |
| ) | |
| # On submit answer | |
| def on_submit(questions, index, score, answer): | |
| score, index, finished, message = submit_answer(questions, index, score, answer) | |
| if finished: | |
| return ( | |
| score, | |
| index, | |
| finished, | |
| f"### ✅ Quiz Finished! Your Score: **{score}/{len(questions)}**", | |
| gr.Radio(choices=[], visible=False), | |
| gr.update(visible=False), | |
| "" | |
| ) | |
| q = questions[index] | |
| return ( | |
| score, | |
| index, | |
| finished, | |
| message, | |
| gr.Radio(choices=q["options"]), | |
| gr.update(visible=True), | |
| f"### Q{index+1}: {q['question']}" | |
| ) | |
| next_btn.click( | |
| on_submit, | |
| inputs=[questions_state, index_state, score_state, options_box], | |
| outputs=[ | |
| score_state, | |
| index_state, | |
| finished_state, | |
| result_box, | |
| options_box, | |
| next_btn, | |
| question_box | |
| ] | |
| ) | |
| demo.launch(debug=True) |