Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,27 @@
|
|
| 1 |
-
from
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from quiz_logic import generator
|
| 4 |
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
@app.post("/generate_quiz")
|
| 8 |
+
async def generate_quiz(request: Request):
|
| 9 |
+
data = await request.json()
|
| 10 |
+
topic = data.get("topic", "").strip()
|
| 11 |
+
n_questions = int(data.get("n_questions", 3))
|
| 12 |
+
difficulty = data.get("difficulty", "medium")
|
| 13 |
+
|
| 14 |
+
if not topic:
|
| 15 |
+
return JSONResponse(status_code=400, content={"error": "Topic is required."})
|
| 16 |
+
|
| 17 |
+
try:
|
| 18 |
+
generator.generate_questions(topic, n_questions, difficulty)
|
| 19 |
+
questions = []
|
| 20 |
+
for q in generator.quiz_data:
|
| 21 |
+
questions.append({
|
| 22 |
+
"question": q["question"],
|
| 23 |
+
"options": q["options"]
|
| 24 |
+
})
|
| 25 |
+
return {"questions": questions}
|
| 26 |
+
except Exception as e:
|
| 27 |
+
return JSONResponse(status_code=500, content={"error": str(e)})
|