Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,65 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
import random
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
|
| 9 |
-
def generate_mcqs(topic
|
| 10 |
-
"""
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
data = response.json()
|
| 21 |
-
text = data[0]["generated_text"]
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
q_line = parts[0]
|
| 29 |
-
opts = [p.strip() for p in parts[1:5] if p.strip()]
|
| 30 |
-
ans = ""
|
| 31 |
-
for p in parts:
|
| 32 |
-
if "Answer" in p or "Correct" in p:
|
| 33 |
-
ans = p.split(":")[-1].strip()
|
| 34 |
-
if len(opts) == 4:
|
| 35 |
-
questions.append({"question": q_line, "options": opts, "answer": ans})
|
| 36 |
-
return questions if questions else [{"question": "⚠️ Couldn’t parse any question.", "options": [], "answer": ""}]
|
| 37 |
-
except Exception as e:
|
| 38 |
-
return [{"question": f"Error: {e}", "options": [], "answer": ""}]
|
| 39 |
|
| 40 |
-
|
| 41 |
-
# 🎯 Build quiz HTML with checkboxes
|
| 42 |
-
def build_quiz_html(topic):
|
| 43 |
-
mcqs = generate_mcqs(topic)
|
| 44 |
html = "<div style='font-family:Poppins,sans-serif;color:#222;'>"
|
| 45 |
-
for i,
|
|
|
|
| 46 |
html += f"<div style='margin:15px;padding:15px;border-radius:10px;background:#fff9ff;box-shadow:0 3px 10px rgba(0,0,0,0.08);'>"
|
| 47 |
-
html += f"<b style='color:#5b21b6;'>{i}. {
|
| 48 |
-
for opt in
|
| 49 |
html += f"<label style='display:block;margin-bottom:5px;'><input type='checkbox' name='q{i}' value='{opt}' style='accent-color:#7c3aed;'> {opt}</label>"
|
| 50 |
-
html += f"<p style='color:#
|
| 51 |
html += "</div>"
|
|
|
|
| 52 |
html += """
|
| 53 |
<button onclick="document.querySelectorAll('[id^=ans]').forEach(a=>a.style.display='block');"
|
| 54 |
style='margin-top:10px;background:#7c3aed;color:white;border:none;padding:10px 20px;border-radius:8px;cursor:pointer;'>
|
| 55 |
Show Answers
|
| 56 |
</button>
|
| 57 |
-
</div>
|
|
|
|
| 58 |
return html
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# ⚙️ Gradio Interface
|
| 62 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as demo:
|
| 63 |
-
gr.Markdown("<h1 style='text-align:center;color:#5b21b6;'>🧠 EduAI —
|
| 64 |
-
|
|
|
|
| 65 |
gen_btn = gr.Button("🎯 Generate Quiz")
|
| 66 |
quiz_output = gr.HTML("<i>Enter a topic and click Generate to start!</i>")
|
| 67 |
|
| 68 |
-
gen_btn.click(
|
| 69 |
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
import os
|
| 4 |
import random
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
+
# Set your OpenAI API key
|
| 8 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
+
def generate_mcqs(topic, num_questions=3):
|
| 11 |
+
"""
|
| 12 |
+
Generate MCQs dynamically using OpenAI's GPT.
|
| 13 |
+
Returns HTML with checkboxes and Show Answers button.
|
| 14 |
+
"""
|
| 15 |
+
prompt = f"""
|
| 16 |
+
Generate {num_questions} multiple-choice questions on "{topic}".
|
| 17 |
+
Format the output as follows for each question:
|
| 18 |
+
Question: ...
|
| 19 |
+
Options: A) ... B) ... C) ... D) ...
|
| 20 |
+
Answer: ...
|
| 21 |
+
"""
|
| 22 |
|
| 23 |
+
response = openai.Completion.create(
|
| 24 |
+
engine="text-davinci-003",
|
| 25 |
+
prompt=prompt,
|
| 26 |
+
max_tokens=500,
|
| 27 |
+
temperature=0.7
|
| 28 |
+
)
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
text = response.choices[0].text.strip()
|
| 31 |
+
|
| 32 |
+
# Parse questions
|
| 33 |
+
pattern = r"Question:\s*(.*?)\nOptions:\s*A\)\s*(.*?)\s*B\)\s*(.*?)\s*C\)\s*(.*?)\s*D\)\s*(.*?)\nAnswer:\s*(.*)"
|
| 34 |
+
matches = re.findall(pattern, text, re.DOTALL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
html = "<div style='font-family:Poppins,sans-serif;color:#222;'>"
|
| 37 |
+
for i, m in enumerate(matches, 1):
|
| 38 |
+
q_text, opt1, opt2, opt3, opt4, ans = m
|
| 39 |
html += f"<div style='margin:15px;padding:15px;border-radius:10px;background:#fff9ff;box-shadow:0 3px 10px rgba(0,0,0,0.08);'>"
|
| 40 |
+
html += f"<b style='color:#5b21b6;'>{i}. {q_text}</b><br><br>"
|
| 41 |
+
for opt in [opt1, opt2, opt3, opt4]:
|
| 42 |
html += f"<label style='display:block;margin-bottom:5px;'><input type='checkbox' name='q{i}' value='{opt}' style='accent-color:#7c3aed;'> {opt}</label>"
|
| 43 |
+
html += f"<p style='color:#065f46;display:none;' id='ans{i}'><b>✅ Correct Answer:</b> {ans.strip()}</p>"
|
| 44 |
html += "</div>"
|
| 45 |
+
|
| 46 |
html += """
|
| 47 |
<button onclick="document.querySelectorAll('[id^=ans]').forEach(a=>a.style.display='block');"
|
| 48 |
style='margin-top:10px;background:#7c3aed;color:white;border:none;padding:10px 20px;border-radius:8px;cursor:pointer;'>
|
| 49 |
Show Answers
|
| 50 |
</button>
|
| 51 |
+
</div>
|
| 52 |
+
"""
|
| 53 |
return html
|
| 54 |
|
| 55 |
+
# Gradio UI
|
|
|
|
| 56 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="violet")) as demo:
|
| 57 |
+
gr.Markdown("<h1 style='text-align:center;color:#5b21b6;'>🧠 EduAI — AI MCQ Quiz Generator</h1>")
|
| 58 |
+
topic_input = gr.Textbox(label="Enter a topic (e.g., Physics, History)")
|
| 59 |
+
num_questions_input = gr.Slider(1, 10, value=3, step=1, label="Number of Questions")
|
| 60 |
gen_btn = gr.Button("🎯 Generate Quiz")
|
| 61 |
quiz_output = gr.HTML("<i>Enter a topic and click Generate to start!</i>")
|
| 62 |
|
| 63 |
+
gen_btn.click(generate_mcqs, inputs=[topic_input, num_questions_input], outputs=quiz_output)
|
| 64 |
|
| 65 |
demo.launch()
|