Spaces:
Sleeping
Sleeping
capradeepgujaran
commited on
Commit
•
ba67897
1
Parent(s):
1f49f47
Update app.py
Browse files
app.py
CHANGED
@@ -14,7 +14,6 @@ client = Groq(
|
|
14 |
class QuizApp:
|
15 |
def __init__(self):
|
16 |
self.current_questions = []
|
17 |
-
self.current_answers = []
|
18 |
self.user_data = {}
|
19 |
|
20 |
def generate_questions(self, text, num_questions):
|
@@ -64,107 +63,75 @@ class QuizApp:
|
|
64 |
return []
|
65 |
|
66 |
def calculate_score(self, answers):
|
67 |
-
if not answers or not self.current_questions:
|
68 |
-
return 0
|
69 |
-
|
70 |
-
total = len(self.current_questions)
|
71 |
-
correct = sum(1 for q, a in zip(self.current_questions, answers)
|
72 |
-
if q['correct_answer'] == a)
|
73 |
-
return (correct / total) * 100
|
74 |
-
|
75 |
-
def generate_certificate(self, name, score, course_name, company_logo=None, participant_photo=None):
|
76 |
try:
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
try:
|
81 |
-
title_font = ImageFont.truetype("arial.ttf", 60)
|
82 |
-
text_font = ImageFont.truetype("arial.ttf", 40)
|
83 |
-
subtitle_font = ImageFont.truetype("arial.ttf", 30)
|
84 |
-
except:
|
85 |
-
title_font = ImageFont.load_default()
|
86 |
-
text_font = ImageFont.load_default()
|
87 |
-
subtitle_font = ImageFont.load_default()
|
88 |
-
|
89 |
-
# Add decorative border
|
90 |
-
draw.rectangle([20, 20, 1180, 780], outline='#4682B4', width=3)
|
91 |
-
|
92 |
-
# Draw certificate content
|
93 |
-
draw.text((600, 100), "CertifyMe AI", font=title_font, fill='#4682B4', anchor="mm")
|
94 |
-
draw.text((600, 160), "Certificate of Achievement", font=subtitle_font, fill='#4682B4', anchor="mm")
|
95 |
-
draw.text((600, 300), "This is to certify that", font=text_font, fill='black', anchor="mm")
|
96 |
-
draw.text((600, 380), name or "Participant", font=text_font, fill='#4682B4', anchor="mm")
|
97 |
-
draw.text((600, 460), "has successfully completed", font=text_font, fill='black', anchor="mm")
|
98 |
-
draw.text((600, 540), course_name or "Assessment", font=text_font, fill='#4682B4', anchor="mm")
|
99 |
-
draw.text((600, 620), f"with a score of {score:.1f}%", font=text_font, fill='black', anchor="mm")
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
try:
|
106 |
-
logo = Image.open(company_logo)
|
107 |
-
logo = logo.resize((150, 150))
|
108 |
-
certificate.paste(logo, (50, 50))
|
109 |
-
except Exception as e:
|
110 |
-
print(f"Error adding logo: {e}")
|
111 |
-
|
112 |
-
if participant_photo is not None:
|
113 |
-
try:
|
114 |
-
photo = Image.open(participant_photo)
|
115 |
-
photo = photo.resize((150, 150))
|
116 |
-
certificate.paste(photo, (1000, 50))
|
117 |
-
except Exception as e:
|
118 |
-
print(f"Error adding photo: {e}")
|
119 |
-
|
120 |
-
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
|
121 |
-
certificate.save(temp_file.name)
|
122 |
-
return temp_file.name
|
123 |
except Exception as e:
|
124 |
-
print(f"Error
|
125 |
-
return
|
|
|
|
|
126 |
|
127 |
def create_quiz_interface():
|
128 |
quiz_app = QuizApp()
|
129 |
|
130 |
-
def
|
131 |
blocks = []
|
132 |
-
|
133 |
-
|
134 |
-
|
|
|
|
|
|
|
135 |
gr.Markdown(q["question"])
|
136 |
radio = gr.Radio(
|
137 |
choices=q["options"],
|
138 |
-
|
139 |
-
|
140 |
)
|
141 |
blocks.append(radio)
|
142 |
return blocks
|
143 |
|
144 |
-
def
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
151 |
|
152 |
-
def
|
153 |
-
|
154 |
-
|
|
|
|
|
|
|
|
|
155 |
|
156 |
-
with gr.Blocks(title="CertifyMe AI") as demo:
|
|
|
157 |
current_tab = gr.State(0)
|
158 |
-
|
159 |
|
|
|
160 |
gr.Markdown("""
|
161 |
# 🎓 CertifyMe AI
|
162 |
### Transform Your Knowledge into Recognized Achievements
|
163 |
""")
|
164 |
|
|
|
165 |
with gr.Tabs() as tabs:
|
166 |
# Step 1: Profile Setup
|
167 |
-
with gr.Tab("📋 Step 1: Profile Setup") as tab1:
|
168 |
with gr.Row():
|
169 |
name = gr.Textbox(label="Full Name", placeholder="Enter your full name")
|
170 |
email = gr.Textbox(label="Email", placeholder="Enter your email")
|
@@ -175,32 +142,32 @@ def create_quiz_interface():
|
|
175 |
lines=10
|
176 |
)
|
177 |
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
)
|
186 |
|
187 |
with gr.Row():
|
188 |
company_logo = gr.Image(label="Company Logo (Optional)", type="filepath")
|
189 |
participant_photo = gr.Image(label="Your Photo (Optional)", type="filepath")
|
190 |
|
191 |
-
generate_btn = gr.Button("Generate Assessment", variant="primary")
|
192 |
|
193 |
# Step 2: Take Assessment
|
194 |
-
with gr.Tab("📝 Step 2: Take Assessment") as tab2:
|
195 |
-
|
196 |
-
|
|
|
197 |
question_blocks = []
|
198 |
-
|
199 |
-
gr.
|
200 |
|
201 |
# Step 3: Get Certified
|
202 |
-
with gr.Tab("🎓 Step 3: Get Certified") as tab3:
|
203 |
-
score_display = gr.Number(label="Your Score"
|
204 |
course_name = gr.Textbox(
|
205 |
label="Certification Title",
|
206 |
value="Professional Assessment Certification"
|
@@ -209,21 +176,26 @@ def create_quiz_interface():
|
|
209 |
|
210 |
# Event handlers
|
211 |
generate_btn.click(
|
212 |
-
fn=
|
213 |
inputs=[text_input, num_questions],
|
214 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
215 |
).then(
|
216 |
-
fn=lambda tab: gr.
|
217 |
inputs=[current_tab],
|
218 |
outputs=[tabs]
|
219 |
)
|
220 |
|
221 |
submit_btn.click(
|
222 |
-
fn=
|
223 |
-
inputs=[
|
224 |
outputs=[score_display, current_tab]
|
225 |
).then(
|
226 |
-
fn=lambda tab: gr.
|
227 |
inputs=[current_tab],
|
228 |
outputs=[tabs]
|
229 |
)
|
|
|
14 |
class QuizApp:
|
15 |
def __init__(self):
|
16 |
self.current_questions = []
|
|
|
17 |
self.user_data = {}
|
18 |
|
19 |
def generate_questions(self, text, num_questions):
|
|
|
63 |
return []
|
64 |
|
65 |
def calculate_score(self, answers):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
try:
|
67 |
+
if not answers or not self.current_questions:
|
68 |
+
return 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
+
total = len(self.current_questions)
|
71 |
+
correct = sum(1 for q, a in zip(self.current_questions, answers)
|
72 |
+
if a is not None and q['correct_answer'] == a)
|
73 |
+
return (correct / total) * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
except Exception as e:
|
75 |
+
print(f"Error calculating score: {e}")
|
76 |
+
return 0
|
77 |
+
|
78 |
+
# Certificate generation method remains the same
|
79 |
|
80 |
def create_quiz_interface():
|
81 |
quiz_app = QuizApp()
|
82 |
|
83 |
+
def create_question_blocks(questions):
|
84 |
blocks = []
|
85 |
+
if not questions:
|
86 |
+
return blocks
|
87 |
+
|
88 |
+
for i, q in enumerate(questions, 1):
|
89 |
+
with gr.Group(visible=True) as group:
|
90 |
+
gr.Markdown(f"### Question {i}")
|
91 |
gr.Markdown(q["question"])
|
92 |
radio = gr.Radio(
|
93 |
choices=q["options"],
|
94 |
+
label=f"Select your answer for Question {i}",
|
95 |
+
interactive=True
|
96 |
)
|
97 |
blocks.append(radio)
|
98 |
return blocks
|
99 |
|
100 |
+
def generate_and_display_questions(text, num_questions):
|
101 |
+
try:
|
102 |
+
questions = quiz_app.generate_questions(text, num_questions)
|
103 |
+
if not questions:
|
104 |
+
return [], gr.Markdown("Failed to generate questions. Please try again."), [], 0
|
105 |
+
|
106 |
+
blocks = create_question_blocks(questions)
|
107 |
+
return blocks, gr.Markdown(""), questions, 1
|
108 |
+
except Exception as e:
|
109 |
+
print(f"Error in generate_and_display_questions: {e}")
|
110 |
+
return [], gr.Markdown("An error occurred. Please try again."), [], 0
|
111 |
|
112 |
+
def submit_answers(answers, questions):
|
113 |
+
try:
|
114 |
+
score = quiz_app.calculate_score(answers)
|
115 |
+
return score, 2
|
116 |
+
except Exception as e:
|
117 |
+
print(f"Error in submit_answers: {e}")
|
118 |
+
return 0, 2
|
119 |
|
120 |
+
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
121 |
+
# State management
|
122 |
current_tab = gr.State(0)
|
123 |
+
stored_questions = gr.State([])
|
124 |
|
125 |
+
# Header
|
126 |
gr.Markdown("""
|
127 |
# 🎓 CertifyMe AI
|
128 |
### Transform Your Knowledge into Recognized Achievements
|
129 |
""")
|
130 |
|
131 |
+
# Tabs
|
132 |
with gr.Tabs() as tabs:
|
133 |
# Step 1: Profile Setup
|
134 |
+
with gr.Tab("📋 Step 1: Profile Setup", id=0) as tab1:
|
135 |
with gr.Row():
|
136 |
name = gr.Textbox(label="Full Name", placeholder="Enter your full name")
|
137 |
email = gr.Textbox(label="Email", placeholder="Enter your email")
|
|
|
142 |
lines=10
|
143 |
)
|
144 |
|
145 |
+
num_questions = gr.Slider(
|
146 |
+
minimum=1,
|
147 |
+
maximum=5,
|
148 |
+
value=3,
|
149 |
+
step=1,
|
150 |
+
label="Number of Questions"
|
151 |
+
)
|
|
|
152 |
|
153 |
with gr.Row():
|
154 |
company_logo = gr.Image(label="Company Logo (Optional)", type="filepath")
|
155 |
participant_photo = gr.Image(label="Your Photo (Optional)", type="filepath")
|
156 |
|
157 |
+
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
158 |
|
159 |
# Step 2: Take Assessment
|
160 |
+
with gr.Tab("📝 Step 2: Take Assessment", id=1) as tab2:
|
161 |
+
error_message = gr.Markdown("")
|
162 |
+
question_container = gr.Group()
|
163 |
+
with question_container:
|
164 |
question_blocks = []
|
165 |
+
|
166 |
+
submit_btn = gr.Button("Submit Assessment", variant="primary", size="lg")
|
167 |
|
168 |
# Step 3: Get Certified
|
169 |
+
with gr.Tab("🎓 Step 3: Get Certified", id=2) as tab3:
|
170 |
+
score_display = gr.Number(label="Your Score")
|
171 |
course_name = gr.Textbox(
|
172 |
label="Certification Title",
|
173 |
value="Professional Assessment Certification"
|
|
|
176 |
|
177 |
# Event handlers
|
178 |
generate_btn.click(
|
179 |
+
fn=generate_and_display_questions,
|
180 |
inputs=[text_input, num_questions],
|
181 |
+
outputs=[
|
182 |
+
question_container,
|
183 |
+
error_message,
|
184 |
+
stored_questions,
|
185 |
+
current_tab
|
186 |
+
]
|
187 |
).then(
|
188 |
+
fn=lambda tab: gr.update(selected=tab),
|
189 |
inputs=[current_tab],
|
190 |
outputs=[tabs]
|
191 |
)
|
192 |
|
193 |
submit_btn.click(
|
194 |
+
fn=submit_answers,
|
195 |
+
inputs=[question_container, stored_questions],
|
196 |
outputs=[score_display, current_tab]
|
197 |
).then(
|
198 |
+
fn=lambda tab: gr.update(selected=tab),
|
199 |
inputs=[current_tab],
|
200 |
outputs=[tabs]
|
201 |
)
|