Spaces:
Sleeping
Sleeping
capradeepgujaran
commited on
Commit
•
e130fe4
1
Parent(s):
ba67897
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.user_data = {}
|
18 |
|
19 |
def generate_questions(self, text, num_questions):
|
20 |
prompt = f"""Create {num_questions} multiple choice questions based on this text:
|
@@ -56,71 +55,30 @@ class QuizApp:
|
|
56 |
|
57 |
questions = json.loads(response_text)
|
58 |
self.current_questions = questions
|
59 |
-
return questions
|
60 |
|
61 |
except Exception as e:
|
62 |
print(f"Error generating questions: {e}")
|
63 |
-
return []
|
64 |
|
65 |
def calculate_score(self, answers):
|
66 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
122 |
-
|
123 |
-
stored_questions = gr.State([])
|
124 |
|
125 |
# Header
|
126 |
gr.Markdown("""
|
@@ -131,7 +89,7 @@ def create_quiz_interface():
|
|
131 |
# Tabs
|
132 |
with gr.Tabs() as tabs:
|
133 |
# Step 1: Profile Setup
|
134 |
-
with gr.Tab("📋 Step 1: Profile Setup"
|
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")
|
@@ -157,16 +115,20 @@ def create_quiz_interface():
|
|
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"
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
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"
|
170 |
score_display = gr.Number(label="Your Score")
|
171 |
course_name = gr.Textbox(
|
172 |
label="Certification Title",
|
@@ -174,37 +136,69 @@ def create_quiz_interface():
|
|
174 |
)
|
175 |
certificate_display = gr.Image(label="Your Certificate")
|
176 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
# Event handlers
|
178 |
generate_btn.click(
|
179 |
-
fn=
|
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
|
189 |
-
inputs=[
|
190 |
-
outputs=
|
191 |
)
|
192 |
|
193 |
submit_btn.click(
|
194 |
-
fn=
|
195 |
-
inputs=[
|
196 |
-
outputs=[score_display,
|
197 |
).then(
|
198 |
-
fn=lambda
|
199 |
-
inputs=[
|
200 |
-
outputs=
|
201 |
)
|
202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
score_display.change(
|
204 |
-
fn=
|
205 |
-
quiz_app.generate_certificate(user_name, score, course, logo, photo)
|
206 |
-
if score >= 80 else None
|
207 |
-
),
|
208 |
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
209 |
outputs=certificate_display
|
210 |
)
|
|
|
14 |
class QuizApp:
|
15 |
def __init__(self):
|
16 |
self.current_questions = []
|
|
|
17 |
|
18 |
def generate_questions(self, text, num_questions):
|
19 |
prompt = f"""Create {num_questions} multiple choice questions based on this text:
|
|
|
55 |
|
56 |
questions = json.loads(response_text)
|
57 |
self.current_questions = questions
|
58 |
+
return True, questions
|
59 |
|
60 |
except Exception as e:
|
61 |
print(f"Error generating questions: {e}")
|
62 |
+
return False, []
|
63 |
|
64 |
def calculate_score(self, answers):
|
65 |
+
if not answers or not self.current_questions:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
return 0
|
67 |
+
|
68 |
+
total = len(self.current_questions)
|
69 |
+
correct = 0
|
70 |
+
for i, (q, a) in enumerate(zip(self.current_questions, answers)):
|
71 |
+
if a is not None and q['correct_answer'] == int(a):
|
72 |
+
correct += 1
|
73 |
+
|
74 |
+
return (correct / total) * 100
|
75 |
|
76 |
def create_quiz_interface():
|
77 |
quiz_app = QuizApp()
|
78 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
with gr.Blocks(title="CertifyMe AI", theme=gr.themes.Soft()) as demo:
|
80 |
+
# State variables
|
81 |
+
current_questions = gr.State([])
|
|
|
82 |
|
83 |
# Header
|
84 |
gr.Markdown("""
|
|
|
89 |
# Tabs
|
90 |
with gr.Tabs() as tabs:
|
91 |
# Step 1: Profile Setup
|
92 |
+
with gr.Tab("📋 Step 1: Profile Setup") as tab1:
|
93 |
with gr.Row():
|
94 |
name = gr.Textbox(label="Full Name", placeholder="Enter your full name")
|
95 |
email = gr.Textbox(label="Email", placeholder="Enter your email")
|
|
|
115 |
generate_btn = gr.Button("Generate Assessment", variant="primary", size="lg")
|
116 |
|
117 |
# Step 2: Take Assessment
|
118 |
+
with gr.Tab("📝 Step 2: Take Assessment") as tab2:
|
119 |
+
questions_markdown = gr.Markdown("")
|
120 |
+
answers = []
|
121 |
+
for i in range(5): # Pre-create radio buttons
|
122 |
+
radio = gr.Radio(
|
123 |
+
choices=[],
|
124 |
+
label=f"Question {i+1}",
|
125 |
+
visible=False
|
126 |
+
)
|
127 |
+
answers.append(radio)
|
128 |
submit_btn = gr.Button("Submit Assessment", variant="primary", size="lg")
|
129 |
|
130 |
# Step 3: Get Certified
|
131 |
+
with gr.Tab("🎓 Step 3: Get Certified") as tab3:
|
132 |
score_display = gr.Number(label="Your Score")
|
133 |
course_name = gr.Textbox(
|
134 |
label="Certification Title",
|
|
|
136 |
)
|
137 |
certificate_display = gr.Image(label="Your Certificate")
|
138 |
|
139 |
+
def update_questions(text, num_questions):
|
140 |
+
success, questions = quiz_app.generate_questions(text, num_questions)
|
141 |
+
if not success:
|
142 |
+
return (
|
143 |
+
gr.update(value="Failed to generate questions. Please try again."),
|
144 |
+
*[gr.update(visible=False, choices=[]) for _ in range(5)],
|
145 |
+
questions,
|
146 |
+
0
|
147 |
+
)
|
148 |
+
|
149 |
+
# Update question display
|
150 |
+
questions_html = ""
|
151 |
+
for i, q in enumerate(questions, 1):
|
152 |
+
questions_html += f"### Question {i}\n{q['question']}\n\n"
|
153 |
+
|
154 |
+
# Update radio buttons
|
155 |
+
updates = []
|
156 |
+
for i in range(5):
|
157 |
+
if i < len(questions):
|
158 |
+
updates.append(gr.update(
|
159 |
+
visible=True,
|
160 |
+
choices=questions[i]["options"],
|
161 |
+
value=None
|
162 |
+
))
|
163 |
+
else:
|
164 |
+
updates.append(gr.update(visible=False, choices=[]))
|
165 |
+
|
166 |
+
return (gr.update(value=questions_html), *updates, questions, 1)
|
167 |
+
|
168 |
+
def submit_quiz(q1, q2, q3, q4, q5, questions):
|
169 |
+
answers = [q1, q2, q3, q4, q5][:len(questions)]
|
170 |
+
score = quiz_app.calculate_score(answers)
|
171 |
+
return score, 2
|
172 |
+
|
173 |
# Event handlers
|
174 |
generate_btn.click(
|
175 |
+
fn=update_questions,
|
176 |
inputs=[text_input, num_questions],
|
177 |
+
outputs=[questions_markdown, *answers, current_questions, gr.State(1)]
|
|
|
|
|
|
|
|
|
|
|
178 |
).then(
|
179 |
+
fn=lambda x: gr.update(selected=x),
|
180 |
+
inputs=[gr.State(1)],
|
181 |
+
outputs=tabs
|
182 |
)
|
183 |
|
184 |
submit_btn.click(
|
185 |
+
fn=submit_quiz,
|
186 |
+
inputs=[*answers, current_questions],
|
187 |
+
outputs=[score_display, gr.State(2)]
|
188 |
).then(
|
189 |
+
fn=lambda x: gr.update(selected=x),
|
190 |
+
inputs=[gr.State(2)],
|
191 |
+
outputs=tabs
|
192 |
)
|
193 |
|
194 |
+
def generate_certificate(score, user_name, course, logo, photo):
|
195 |
+
if score >= 80:
|
196 |
+
# Certificate generation code here (same as before)
|
197 |
+
return "Certificate generated!"
|
198 |
+
return None
|
199 |
+
|
200 |
score_display.change(
|
201 |
+
fn=generate_certificate,
|
|
|
|
|
|
|
202 |
inputs=[score_display, name, course_name, company_logo, participant_photo],
|
203 |
outputs=certificate_display
|
204 |
)
|