Spaces:
Running
Running
capradeepgujaran
commited on
Commit
•
67f169b
1
Parent(s):
585829f
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import groq_gradio
|
3 |
+
import os
|
4 |
+
from PIL import Image, ImageDraw, ImageFont
|
5 |
+
from datetime import datetime
|
6 |
+
import json
|
7 |
+
import tempfile
|
8 |
+
|
9 |
+
class QuizApp:
|
10 |
+
def __init__(self):
|
11 |
+
self.current_questions = []
|
12 |
+
self.user_data = {}
|
13 |
+
|
14 |
+
def generate_questions(self, text, num_questions):
|
15 |
+
prompt = f"""Generate {num_questions} multiple choice questions based on this text:
|
16 |
+
{text}
|
17 |
+
|
18 |
+
Return the response in this JSON format:
|
19 |
+
{{
|
20 |
+
"questions": [
|
21 |
+
{{
|
22 |
+
"question": "Question text",
|
23 |
+
"options": ["option1", "option2", "option3", "option4"],
|
24 |
+
"correct_answers": [0, 1]
|
25 |
+
}}
|
26 |
+
]
|
27 |
+
}}
|
28 |
+
Only return the JSON, no other text."""
|
29 |
+
|
30 |
+
# Use groq-gradio's built-in interface for LLM
|
31 |
+
interface = gr.load(
|
32 |
+
name='llama2-70b-4096',
|
33 |
+
src=groq_gradio.registry,
|
34 |
+
)
|
35 |
+
|
36 |
+
response = interface.predict(prompt)
|
37 |
+
questions = json.loads(response)
|
38 |
+
self.current_questions = questions["questions"]
|
39 |
+
return json.dumps(questions["questions"], indent=2)
|
40 |
+
|
41 |
+
def calculate_score(self, answers):
|
42 |
+
answers = json.loads(answers)
|
43 |
+
total = len(self.current_questions)
|
44 |
+
correct = 0
|
45 |
+
|
46 |
+
for q, a in zip(self.current_questions, answers):
|
47 |
+
if set(a) == set(q["correct_answers"]):
|
48 |
+
correct += 1
|
49 |
+
|
50 |
+
return (correct / total) * 100
|
51 |
+
|
52 |
+
def generate_certificate(self, name, score, course_name, company_logo=None, participant_photo=None):
|
53 |
+
# Create certificate
|
54 |
+
certificate = Image.new('RGB', (1200, 800), 'white')
|
55 |
+
draw = ImageDraw.Draw(certificate)
|
56 |
+
|
57 |
+
# Load a default font
|
58 |
+
try:
|
59 |
+
title_font = ImageFont.truetype("arial.ttf", 60)
|
60 |
+
text_font = ImageFont.truetype("arial.ttf", 40)
|
61 |
+
except:
|
62 |
+
# Fallback to default font
|
63 |
+
title_font = ImageFont.load_default()
|
64 |
+
text_font = ImageFont.load_default()
|
65 |
+
|
66 |
+
# Add certificate content
|
67 |
+
draw.text((600, 100), "Certificate of Completion", font=title_font, fill='black', anchor="mm")
|
68 |
+
draw.text((600, 300), f"This is to certify that", font=text_font, fill='black', anchor="mm")
|
69 |
+
draw.text((600, 380), name, font=text_font, fill='black', anchor="mm")
|
70 |
+
draw.text((600, 460), f"has successfully completed", font=text_font, fill='black', anchor="mm")
|
71 |
+
draw.text((600, 540), course_name, font=text_font, fill='black', anchor="mm")
|
72 |
+
draw.text((600, 620), f"with a score of {score:.1f}%", font=text_font, fill='black', anchor="mm")
|
73 |
+
|
74 |
+
# Add date
|
75 |
+
current_date = datetime.now().strftime("%B %d, %Y")
|
76 |
+
draw.text((600, 700), current_date, font=text_font, fill='black', anchor="mm")
|
77 |
+
|
78 |
+
# Add logo if provided
|
79 |
+
if company_logo is not None:
|
80 |
+
logo = Image.open(company_logo)
|
81 |
+
logo = logo.resize((150, 150))
|
82 |
+
certificate.paste(logo, (50, 50))
|
83 |
+
|
84 |
+
# Add photo if provided
|
85 |
+
if participant_photo is not None:
|
86 |
+
photo = Image.open(participant_photo)
|
87 |
+
photo = photo.resize((150, 150))
|
88 |
+
certificate.paste(photo, (1000, 50))
|
89 |
+
|
90 |
+
# Save to temporary file
|
91 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
92 |
+
certificate.save(temp_file.name)
|
93 |
+
return temp_file.name
|
94 |
+
|
95 |
+
def create_quiz_app():
|
96 |
+
quiz_app = QuizApp()
|
97 |
+
|
98 |
+
with gr.Blocks(title="Interactive Quiz Generator") as demo:
|
99 |
+
gr.Markdown("# Interactive Quiz Generator")
|
100 |
+
|
101 |
+
# User Information Tab
|
102 |
+
with gr.Tab("Step 1: User Information"):
|
103 |
+
name = gr.Textbox(label="Full Name", placeholder="Enter your full name")
|
104 |
+
email = gr.Textbox(label="Email", placeholder="Enter your email")
|
105 |
+
text_input = gr.Textbox(label="Content for Quiz",
|
106 |
+
placeholder="Enter the text content for generating questions",
|
107 |
+
lines=10)
|
108 |
+
num_questions = gr.Slider(minimum=1, maximum=10, value=5, step=1,
|
109 |
+
label="Number of Questions")
|
110 |
+
company_logo = gr.Image(label="Company Logo (Optional)", type="filepath")
|
111 |
+
participant_photo = gr.Image(label="Participant Photo (Optional)",
|
112 |
+
type="filepath")
|
113 |
+
generate_btn = gr.Button("Generate Quiz")
|
114 |
+
|
115 |
+
# Quiz Tab
|
116 |
+
with gr.Tab("Step 2: Take Quiz"):
|
117 |
+
questions_display = gr.JSON(label="Questions")
|
118 |
+
answers_input = gr.JSON(label="Your Answers (Enter indices of correct options)")
|
119 |
+
submit_btn = gr.Button("Submit Answers")
|
120 |
+
score_display = gr.Number(label="Your Score")
|
121 |
+
|
122 |
+
# Certificate Tab
|
123 |
+
with gr.Tab("Step 3: Get Certificate"):
|
124 |
+
course_name = gr.Textbox(label="Course Name",
|
125 |
+
value="Interactive Quiz Course")
|
126 |
+
certificate_display = gr.Image(label="Certificate")
|
127 |
+
|
128 |
+
# Event handlers
|
129 |
+
generate_btn.click(
|
130 |
+
fn=quiz_app.generate_questions,
|
131 |
+
inputs=[text_input, num_questions],
|
132 |
+
outputs=questions_display
|
133 |
+
)
|
134 |
+
|
135 |
+
submit_btn.click(
|
136 |
+
fn=quiz_app.calculate_score,
|
137 |
+
inputs=[answers_input],
|
138 |
+
outputs=score_display
|
139 |
+
)
|
140 |
+
|
141 |
+
score_display.change(
|
142 |
+
fn=lambda score, user_name, course, logo, photo: (
|
143 |
+
quiz_app.generate_certificate(user_name, score, course, logo, photo)
|
144 |
+
if score >= 80 else None
|
145 |
+
),
|
146 |
+
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
147 |
+
outputs=certificate_display
|
148 |
+
)
|
149 |
+
|
150 |
+
return demo
|
151 |
+
|
152 |
+
if __name__ == "__main__":
|
153 |
+
demo = create_quiz_app()
|
154 |
+
demo.launch()
|