Spaces:
Sleeping
Sleeping
File size: 7,643 Bytes
67f169b 4f91be2 67f169b 4f91be2 67f169b deff9b7 67f169b 0ef1d43 67f169b 0ef1d43 deff9b7 b1c910f deff9b7 0ef1d43 deff9b7 b1c910f deff9b7 67f169b 4f91be2 b1c910f 0ef1d43 8cd587a 0ef1d43 deff9b7 b1c910f 0ef1d43 deff9b7 e130fe4 0ef1d43 b1c910f e130fe4 deff9b7 67f169b e130fe4 ba67897 e130fe4 67f169b deff9b7 ba67897 e130fe4 deff9b7 ba67897 deff9b7 ba67897 deff9b7 e130fe4 deff9b7 4f91be2 deff9b7 ba67897 b1c910f deff9b7 ba67897 deff9b7 e130fe4 ba67897 4f91be2 b1c910f e130fe4 ba67897 b1c910f deff9b7 b1c910f 67f169b e130fe4 67f169b e130fe4 67f169b e130fe4 b1c910f e130fe4 67f169b e130fe4 b1c910f e130fe4 67f169b e130fe4 67f169b e130fe4 67f169b 4f91be2 deff9b7 1f49f47 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
import gradio as gr
from groq import Groq
import os
from PIL import Image, ImageDraw, ImageFont
from datetime import datetime
import json
import tempfile
# Initialize Groq client
client = Groq(
api_key=os.getenv("GROQ_API_KEY")
)
class QuizApp:
def __init__(self):
self.current_questions = []
def generate_questions(self, text, num_questions):
prompt = f"""Create {num_questions} multiple choice questions based on this text:
{text}
Each question should:
1. Have a clear, concise question text
2. Have exactly 4 options
3. Have only one correct answer
4. Be educational and test understanding
Return in this exact JSON format:
[
{{
"question": "What is the main topic discussed?",
"options": [
"Correct answer",
"Wrong answer 1",
"Wrong answer 2",
"Wrong answer 3"
],
"correct_answer": 0
}}
]"""
try:
response = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a quiz generator that creates clear, single-choice questions."},
{"role": "user", "content": prompt}
],
model="llama-3.2-3b-preview",
temperature=0.5,
max_tokens=2048
)
response_text = response.choices[0].message.content.strip()
response_text = response_text.replace("```json", "").replace("```", "").strip()
questions = json.loads(response_text)
self.current_questions = questions
return True, questions
except Exception as e:
print(f"Error generating questions: {e}")
return False, []
def calculate_score(self, answers):
if not answers or not self.current_questions:
return 0
total = len(self.current_questions)
correct = 0
for i, (q, a) in enumerate(zip(self.current_questions, answers)):
if a is not None and q['correct_answer'] == int(a):
correct += 1
return (correct / total) * 100
def create_quiz_interface():
quiz_app = QuizApp()
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
# State variables
current_questions = gr.State([])
# Header
gr.Markdown("""
# 🎓 CertifyMe AI
### Transform Your Knowledge into Recognized Achievements
""")
# Tabs
with gr.Tabs() as tabs:
# Step 1: Profile Setup
with gr.Tab("📋 Step 1: Profile Setup") as tab1:
with gr.Row():
name = gr.Textbox(label="Full Name", placeholder="Enter your full name")
email = gr.Textbox(label="Email", placeholder="Enter your email")
text_input = gr.Textbox(
label="Learning Content",
placeholder="Enter the text content you want to be assessed on",
lines=10
)
num_questions = gr.Slider(
minimum=1,
maximum=5,
value=3,
step=1,
label="Number of Questions"
)
with gr.Row():
company_logo = gr.Image(label="Company Logo (Optional)", type="filepath")
participant_photo = gr.Image(label="Your Photo (Optional)", type="filepath")
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
# Step 2: Take Assessment
with gr.Tab("📝 Step 2: Take Assessment") as tab2:
questions_markdown = gr.Markdown("")
answers = []
for i in range(5): # Pre-create radio buttons
radio = gr.Radio(
choices=[],
label=f"Question {i+1}",
visible=False
)
answers.append(radio)
submit_btn = gr.Button("Submit Assessment", variant="primary", size="lg")
# Step 3: Get Certified
with gr.Tab("🎓 Step 3: Get Certified") as tab3:
score_display = gr.Number(label="Your Score")
course_name = gr.Textbox(
label="Certification Title",
value="Professional Assessment Certification"
)
certificate_display = gr.Image(label="Your Certificate")
def update_questions(text, num_questions):
success, questions = quiz_app.generate_questions(text, num_questions)
if not success:
return (
gr.update(value="Failed to generate questions. Please try again."),
*[gr.update(visible=False, choices=[]) for _ in range(5)],
questions,
0
)
# Update question display
questions_html = ""
for i, q in enumerate(questions, 1):
questions_html += f"### Question {i}\n{q['question']}\n\n"
# Update radio buttons
updates = []
for i in range(5):
if i < len(questions):
updates.append(gr.update(
visible=True,
choices=questions[i]["options"],
value=None
))
else:
updates.append(gr.update(visible=False, choices=[]))
return (gr.update(value=questions_html), *updates, questions, 1)
def submit_quiz(q1, q2, q3, q4, q5, questions):
answers = [q1, q2, q3, q4, q5][:len(questions)]
score = quiz_app.calculate_score(answers)
return score, 2
# Event handlers
generate_btn.click(
fn=update_questions,
inputs=[text_input, num_questions],
outputs=[questions_markdown, *answers, current_questions, gr.State(1)]
).then(
fn=lambda x: gr.update(selected=x),
inputs=[gr.State(1)],
outputs=tabs
)
submit_btn.click(
fn=submit_quiz,
inputs=[*answers, current_questions],
outputs=[score_display, gr.State(2)]
).then(
fn=lambda x: gr.update(selected=x),
inputs=[gr.State(2)],
outputs=tabs
)
def generate_certificate(score, user_name, course, logo, photo):
if score >= 80:
# Certificate generation code here (same as before)
return "Certificate generated!"
return None
score_display.change(
fn=generate_certificate,
inputs=[score_display, name, course_name, company_logo, participant_photo],
outputs=certificate_display
)
return demo
if __name__ == "__main__":
if not os.getenv("GROQ_API_KEY"):
print("Please set your GROQ_API_KEY environment variable")
exit(1)
demo = create_quiz_interface()
demo.launch() |