Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pyscript import document
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
test_data = None
|
| 6 |
+
|
| 7 |
+
def generate_paper(event):
|
| 8 |
+
global test_data
|
| 9 |
+
status = document.getElementById("status")
|
| 10 |
+
api_key = document.getElementById("api_key").value.strip()
|
| 11 |
+
material = document.getElementById("material").value.strip()
|
| 12 |
+
q_count = document.getElementById("q_count").value.strip()
|
| 13 |
+
|
| 14 |
+
if not api_key or not material or not q_count:
|
| 15 |
+
status.innerHTML = "❌ एरर: कृपया API Key, मटेरियल और प्रश्नों की संख्या भरें।"
|
| 16 |
+
return
|
| 17 |
+
|
| 18 |
+
status.innerHTML = "⏳ AI आपका पेपर तैयार कर रहा है... कृपया कुछ सेकंड रुकें..."
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
genai.configure(api_key=api_key)
|
| 22 |
+
model = genai.GenerativeModel('gemini-1.5-flash')
|
| 23 |
+
|
| 24 |
+
system_instruction = (
|
| 25 |
+
"Create an MCQ test based on the text. Output ONLY valid JSON: "
|
| 26 |
+
'{"questions": [{"number": 1, "text": "Question?", "options": {"A": "1", "B": "2", "C": "3", "D": "4"}, "correct": "A", "exp": "reason"}]}'
|
| 27 |
+
)
|
| 28 |
+
prompt = f"{system_instruction}\n\nMaterial:\n{material}\n\nGenerate exactly {q_count} questions."
|
| 29 |
+
|
| 30 |
+
response = model.generate_content(prompt)
|
| 31 |
+
clean_json = response.text.replace("```json", "").replace("```", "").strip()
|
| 32 |
+
test_data = json.loads(clean_json)
|
| 33 |
+
|
| 34 |
+
# Render HTML inside browser
|
| 35 |
+
render_ui()
|
| 36 |
+
status.innerHTML = "✅ पेपर सफलतापूर्वक तैयार हो गया!"
|
| 37 |
+
except Exception as e:
|
| 38 |
+
status.innerHTML = f"❌ कुछ गड़बड़ हुई: {str(e)}"
|
| 39 |
+
|
| 40 |
+
def render_ui():
|
| 41 |
+
document.getElementById("quiz_area").classList.remove("hidden")
|
| 42 |
+
q_box = document.getElementById("questions_box")
|
| 43 |
+
omr_box = document.getElementById("omr_box")
|
| 44 |
+
|
| 45 |
+
q_box.innerHTML = ""
|
| 46 |
+
omr_box.innerHTML = ""
|
| 47 |
+
|
| 48 |
+
for q in test_data["questions"]:
|
| 49 |
+
num = str(q["number"])
|
| 50 |
+
|
| 51 |
+
# Insert Questions
|
| 52 |
+
opts_html = "".join([f"<p class='text-sm text-gray-600 pl-4'>({k}) {v}</p>" for k, v in q["options"].items()])
|
| 53 |
+
q_box.innerHTML += f"<div class='mb-2'><p class='font-semibold text-gray-800'>Q{num}. {q['text']}</p>{opts_html}</div>"
|
| 54 |
+
|
| 55 |
+
# Insert OMR row
|
| 56 |
+
omr_box.innerHTML += f"""
|
| 57 |
+
<div class='flex items-center gap-2 bg-gray-50 p-2 rounded border'>
|
| 58 |
+
<span class='font-bold text-gray-700 w-10'>Q{num}:</span>
|
| 59 |
+
<input type='text' id='omr_{num}' max_length='1' class='w-12 p-1 border rounded text-center uppercase font-bold' placeholder='A/B'>
|
| 60 |
+
<span class='text-xs text-gray-400'>(Type A, B, C, or D)</span>
|
| 61 |
+
</div>
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
def check_results(event):
|
| 65 |
+
if not test_data:
|
| 66 |
+
return
|
| 67 |
+
|
| 68 |
+
score = 0
|
| 69 |
+
total = len(test_data["questions"])
|
| 70 |
+
report_html = "<h3 class='text-xl font-bold text-blue-800 mb-4'>📊 Your Scorecard</h3>"
|
| 71 |
+
|
| 72 |
+
for q in test_data["questions"]:
|
| 73 |
+
num = str(q["number"])
|
| 74 |
+
user_ans = document.getElementById(f"omr_{num}").value.strip().upper()
|
| 75 |
+
correct_ans = q["correct"]
|
| 76 |
+
|
| 77 |
+
if user_ans == correct_ans:
|
| 78 |
+
score += 1
|
| 79 |
+
report_html += f"<p class='text-green-600 font-semibold'>✔️ Q{num}: सही जवाब! ({user_ans})</p>"
|
| 80 |
+
else:
|
| 81 |
+
report_html += f"<p class='text-red-600 font-semibold'>❌ Q{num}: गलत जवाब! (आपका: {user_ans if user_ans else 'खाली'}, सही: {correct_ans})</p>"
|
| 82 |
+
report_html += f"<p class='text-xs text-gray-500 italic mb-3'>💡 व्याख्या: {q['exp']}</p>"
|
| 83 |
+
|
| 84 |
+
report_html += f"<div class='text-lg font-bold text-gray-800 border-t pt-2 mt-2'>🏆 कुल स्कोर: {score} / {total}</div>"
|
| 85 |
+
|
| 86 |
+
res_box = document.getElementById("result_box")
|
| 87 |
+
res_box.classList.remove("hidden")
|
| 88 |
+
res_box.innerHTML = report_html
|
| 89 |
+
|