File size: 24,713 Bytes
c4d9e00
8dba616
c4d9e00
 
 
 
48982a9
0b64bbe
 
 
 
4b63835
0b64bbe
638a57a
8dba616
 
 
 
638a57a
 
 
 
 
 
0b64bbe
 
 
 
c4d9e00
638a57a
0b64bbe
c4d9e00
0b64bbe
 
 
c4d9e00
8dba616
48982a9
0b64bbe
c4d9e00
 
 
 
 
 
0b64bbe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
638a57a
 
 
0b64bbe
638a57a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b64bbe
638a57a
 
 
c4d9e00
0b64bbe
638a57a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b64bbe
638a57a
0b64bbe
638a57a
 
 
 
 
 
 
 
0b64bbe
638a57a
 
 
 
 
 
 
 
0b64bbe
638a57a
 
 
 
 
 
 
0b64bbe
 
c4d9e00
0b64bbe
 
 
 
 
 
 
 
 
 
c4d9e00
638a57a
 
c4d9e00
 
0b64bbe
 
 
c4d9e00
638a57a
 
 
c4d9e00
 
0b64bbe
 
638a57a
c4d9e00
 
0b64bbe
 
638a57a
0b64bbe
638a57a
 
 
 
 
 
0b64bbe
 
638a57a
0b64bbe
 
 
 
 
 
 
 
 
 
 
 
48982a9
 
638a57a
 
 
 
 
 
 
 
 
592c887
638a57a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a61931b
 
 
 
 
 
 
 
 
 
 
638a57a
a61931b
592c887
 
 
 
a61931b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
638a57a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bbfa9b8
638a57a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48982a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
# app.py
import os
import json
import time
import tempfile

import gradio as gr
from fastapi import FastAPI, HTTPException, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field, validator

from quiz import QuizManager, SUBJECTS, DIFFICULTIES

# ── Startup check ─────────────────────────────────────────────
api_key = os.getenv("GEMINI_API_KEY")
if not api_key:
    raise ValueError("GEMINI_API_KEY environment variable not set")

# ── Shared quiz manager (one instance, used by BOTH API & UI) ─
#    This is the key fix β€” no HTTP calls needed
quiz_manager = QuizManager()

# ── FastAPI ───────────────────────────────────────────────────
fastapi_app = FastAPI(
    title="NEET/JEE Quiz Generator API",
    description="RAG-powered MCQ generator for competitive exam prep",
    version="2.0.0"
)

fastapi_app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# ── Pydantic models ───────────────────────────────────────────
class TextRequest(BaseModel):
    text: str = Field(..., min_length=100)
    subject: str = Field(...)
    chapter: str = Field(...)
    topic: str = Field(...)
    exam_type: str = Field("NEET")
    difficulty: str = Field("Medium")
    num_questions: int = Field(10, ge=1, le=30)

    @validator("exam_type")
    def valid_exam(cls, v):
        if v not in ["NEET", "JEE_MAINS", "JEE_ADVANCED"]:
            raise ValueError("Invalid exam type")
        return v

    @validator("difficulty")
    def valid_diff(cls, v):
        if v not in ["Easy", "Medium", "Hard", "Mixed"]:
            raise ValueError("Invalid difficulty")
        return v

class QuizResponse(BaseModel):
    success: bool
    session_id: str
    total_questions: int
    time_limit_minutes: int
    quiz: dict
    generated_in_seconds: float

class HealthResponse(BaseModel):
    status: str
    model: str
    version: str

# ═════════════════════════════════════════════════════════════
# CORE LOGIC  (called by BOTH API endpoints AND Gradio directly)
# ═════════════════════════════════════════════════════════════

def core_generate_from_text(
    text: str,
    subject: str,
    chapter: str,
    topic: str,
    exam_type: str,
    difficulty: str,
    num_questions: int
) -> dict:
    """
    Pure Python function β€” no HTTP involved.
    Returns a dict that both the API endpoint and Gradio can use.
    Raises ValueError / RuntimeError on bad input.
    """
    start      = time.time()
    num_chunks = quiz_manager.load_and_index(text)

    if num_chunks < 2:
        raise ValueError(
            "Notes too short β€” please provide more content."
        )

    session = quiz_manager.create_quiz(
        topic         = topic,
        subject       = subject,
        chapter       = chapter,
        exam_type     = exam_type,
        difficulty    = difficulty,
        num_questions = num_questions
    )

    return {
        "success"             : True,
        "session_id"          : session.session_id,
        "total_questions"     : session.total_questions,
        "time_limit_minutes"  : session.time_limit_minutes,
        "quiz"                : session.to_dict(),
        "generated_in_seconds": round(time.time() - start, 2)
    }


def core_generate_from_pdf(
    pdf_path: str,
    subject: str,
    chapter: str,
    topic: str,
    exam_type: str,
    difficulty: str,
    num_questions: int
) -> dict:
    """Same idea β€” direct Python call, no HTTP."""
    start = time.time()
    quiz_manager.load_and_index(pdf_path)

    session = quiz_manager.create_quiz(
        topic         = topic,
        subject       = subject,
        chapter       = chapter,
        exam_type     = exam_type,
        difficulty    = difficulty,
        num_questions = num_questions
    )

    return {
        "success"             : True,
        "session_id"          : session.session_id,
        "total_questions"     : session.total_questions,
        "time_limit_minutes"  : session.time_limit_minutes,
        "quiz"                : session.to_dict(),
        "generated_in_seconds": round(time.time() - start, 2)
    }


def core_score_quiz(
    answers: dict,
    correct_map: dict,
    exam_type: str = "NEET"
) -> dict:
    """Scoring logic β€” also pure Python."""
    marks_correct  = 4
    marks_negative = 1
    score = correct = wrong = skipped = 0
    details = {}

    for qid, correct_ans in correct_map.items():
        user_ans = answers.get(qid)
        if user_ans is None:
            skipped += 1
            details[qid] = {"status": "skipped", "marks": 0}
        elif user_ans == correct_ans:
            correct += 1
            score   += marks_correct
            details[qid] = {
                "status"     : "correct",
                "marks"      : marks_correct,
                "your_answer": user_ans
            }
        else:
            wrong += 1
            score -= marks_negative
            details[qid] = {
                "status"        : "wrong",
                "marks"         : -marks_negative,
                "your_answer"   : user_ans,
                "correct_answer": correct_ans
            }

    total_possible = len(correct_map) * marks_correct
    percentage     = round(
        (score / total_possible) * 100, 1
    ) if total_possible else 0

    return {
        "score"         : score,
        "total_possible": total_possible,
        "percentage"    : percentage,
        "correct"       : correct,
        "wrong"         : wrong,
        "skipped"       : skipped,
        "rank_estimate" : _estimate_rank(percentage, exam_type),
        "details"       : details
    }


def _estimate_rank(percentage: float, exam_type: str) -> str:
    if exam_type == "NEET":
        if percentage >= 90: return "Top 1000 (AIR)"
        if percentage >= 80: return "Top 10,000"
        if percentage >= 70: return "Top 50,000"
        if percentage >= 60: return "Top 1,00,000"
        return "Below cut-off range"
    else:
        if percentage >= 85: return "IIT Top-10 branch eligible"
        if percentage >= 70: return "IIT eligible"
        if percentage >= 55: return "NIT Top-tier eligible"
        if percentage >= 40: return "NIT eligible"
        return "Below JEE cut-off range"


# ═════════════════════════════════════════════════════════════
# FASTAPI ENDPOINTS  (thin wrappers around core_ functions)
# ═════════════════════════════════════════════════════════════

@fastapi_app.get("/health", response_model=HealthResponse)
async def health():
    return HealthResponse(
        status  = "healthy",
        model   = "nvidia/nemotron-3-super-120b-a12b:free + BGE-small",
        version = "2.0.0"
    )


@fastapi_app.post("/generate/from-text", response_model=QuizResponse)
async def api_generate_from_text(req: TextRequest):
    try:
        result = core_generate_from_text(
            text          = req.text,
            subject       = req.subject,
            chapter       = req.chapter,
            topic         = req.topic,
            exam_type     = req.exam_type,
            difficulty    = req.difficulty,
            num_questions = req.num_questions
        )
        return QuizResponse(**result)
    except ValueError as e:
        raise HTTPException(status_code=400, detail=str(e))
    except RuntimeError as e:
        raise HTTPException(status_code=500, detail=str(e))
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"Generation failed: {str(e)}"
        )


@fastapi_app.post("/generate/from-pdf", response_model=QuizResponse)
async def api_generate_from_pdf(
    file         : UploadFile = File(...),
    subject      : str = "Biology",
    chapter      : str = "Chapter",
    topic        : str = "Main topic",
    exam_type    : str = "NEET",
    difficulty   : str = "Medium",
    num_questions: int = 10
):
    if not file.filename.endswith(".pdf"):
        raise HTTPException(
            status_code=400,
            detail="Only PDF files accepted"
        )

    with tempfile.NamedTemporaryFile(
        suffix=".pdf", delete=False
    ) as tmp:
        tmp.write(await file.read())
        tmp_path = tmp.name

    try:
        result = core_generate_from_pdf(
            pdf_path      = tmp_path,
            subject       = subject,
            chapter       = chapter,
            topic         = topic,
            exam_type     = exam_type,
            difficulty    = difficulty,
            num_questions = num_questions
        )
        return QuizResponse(**result)
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail=f"PDF processing failed: {str(e)}"
        )
    finally:
        os.unlink(tmp_path)


@fastapi_app.post("/score")
async def api_score_quiz(submission: dict):
    return core_score_quiz(
        answers     = submission.get("answers", {}),
        correct_map = submission.get("correct_answers", {}),
        exam_type   = submission.get("exam_type", "NEET")
    )
    
@fastapi_app.get("/test-models")
async def test_models():
    """Visit /test-models to check which free models work"""
    from openai import OpenAI

    api_key = os.getenv("OPENROUTER_API_KEY")
    client  = OpenAI(
        api_key  = api_key,
        base_url = "https://openrouter.ai/api/v1"
    )

    models_to_test = [
        "nvidia/nemotron-3-super-120b-a12b:free",
        "qwen/qwen3-next-80b-a3b-instruct:free",
        "google/gemma-4-31b-it:free",
        "openai/gpt-oss-120b:free",
    ]

    results = {}
    for model_name in models_to_test:
        try:
            res = client.chat.completions.create(
                model    = model_name,
                messages = [{"role": "user", "content": "Say OK"}],
                max_tokens = 5,
                extra_headers = {
                    "HTTP-Referer": "https://huggingface.co",
                    "X-Title"     : "NEET-JEE-Quiz-Generator"
                }
            )
            reply = res.choices[0].message.content.strip()
            results[model_name] = f"βœ… Works β†’ {reply}"
        except Exception as e:
            err = str(e)
            if "429" in err:
                results[model_name] = "⏳ Rate limited (try later)"
            elif "402" in err:
                results[model_name] = "πŸ’³ Requires credits"
            else:
                results[model_name] = f"❌ {err[:120]}"

    return results

# ═════════════════════════════════════════════════════════════
# GRADIO UI  (calls core_ functions directly β€” zero HTTP)
# ═════════════════════════════════════════════════════════════

def gradio_generate_text(
    subject, chapter, exam_type,
    difficulty, num_questions, notes_text
):
    """Gradio handler β€” calls core function directly, no requests lib."""
    if not notes_text or len(notes_text.strip()) < 100:
        return (
            "",
            "❌ Please paste at least a few paragraphs of notes",
            "", ""
        )
    try:
        result = core_generate_from_text(
            text          = notes_text,
            subject       = subject,
            chapter       = chapter,
            topic         = f"{subject} {chapter}",
            exam_type     = exam_type,
            difficulty    = difficulty,
            num_questions = int(num_questions)
        )
        return (
            json.dumps(result, indent=2),
            f" Generated {result['total_questions']} questions "
            f"in {result['generated_in_seconds']}s",
            result["session_id"],
            str(result["time_limit_minutes"])
        )
    except ValueError as e:
        return ("", f"❌ Input error: {e}", "", "")
    except Exception as e:
        return ("", f"❌ Error: {e}", "", "")


def gradio_generate_pdf(
    subject, chapter, exam_type,
    difficulty, num_questions, pdf_path
):
    """Gradio PDF handler β€” also direct call."""
    if pdf_path is None:
        return ("", "❌ Please upload a PDF file", "", "")
    try:
        result = core_generate_from_pdf(
            pdf_path      = pdf_path,
            subject       = subject,
            chapter       = chapter,
            topic = f"{subject} {chapter} key concepts, applications, exam-level MCQs",
            exam_type     = exam_type,
            difficulty    = difficulty,
            num_questions = int(num_questions)
        )
        return (
            json.dumps(result, indent=2),
            f" Generated {result['total_questions']} questions "
            f"in {result['generated_in_seconds']}s",
            result["session_id"],
            str(result["time_limit_minutes"])
        )
    except Exception as e:
        return ("", f"❌ Error: {e}", "", "")


def render_quiz(quiz_json_str: str) -> str:
    """Convert raw JSON output into readable markdown."""
    if not quiz_json_str or not quiz_json_str.strip():
        return ""
    try:
        data      = json.loads(quiz_json_str)
        quiz      = data.get("quiz", {})
        questions = quiz.get("questions", [])

        if not questions:
            return " No questions found in response"

        lines = []
        lines.append(f"#  {quiz.get('exam_type','Quiz')} Mock Test")
        lines.append(
            f"**Subject:** {quiz.get('subject','')} | "
            f"**Chapter:** {quiz.get('chapter','')} | "
            f"**Difficulty:** {quiz.get('difficulty','')}"
        )
        lines.append(
            f"**Questions:** {quiz.get('total_questions',0)} | "
            f"**Time:** {quiz.get('time_limit_minutes',0)} min | "
            f"**Marking:** +{questions[0].get('marks',4)} / "
            f"-{questions[0].get('negative_marks',1)}"
        )
        lines.append("\n---\n")

        for i, q in enumerate(questions, 1):
            opts = q.get("options", {})
            ans  = q.get("answer", "")
            lines.append(
                f"### Q{i}. `[{q.get('difficulty','')}]` "
                f"{q.get('question','')}"
            )
            for key in ["A", "B", "C", "D"]:
                marker = "βœ…" if key == ans else "⬜"
                lines.append(
                    f"{marker} **({key})** {opts.get(key,'')}"
                )
            lines.append(
                f"\n>  **Answer: {ans}** β€” "
                f"{q.get('explanation','')}"
            )
            lines.append("\n---\n")

        return "\n".join(lines)

    except json.JSONDecodeError:
        return " Could not parse quiz JSON"
    except Exception as e:
        return f" Render error: {str(e)}"


def build_gradio_blocks() -> gr.Blocks:
    with gr.Blocks(
        title="NEET/JEE Quiz Generator",
        theme=gr.themes.Soft()
    ) as demo:

        gr.Markdown("""
        # πŸ“š NEET / JEE Mock Quiz Generator
        Paste your study notes or upload a PDF β†’ get exam-ready MCQs instantly
        """)

        with gr.Tabs():

            # ── Tab 1: Text input ────────────────────────────
            with gr.TabItem(" From Text Notes"):
                with gr.Row():
                    with gr.Column(scale=1, min_width=260):
                        gr.Markdown("###  Settings")
                        t_subject = gr.Dropdown(
                            choices=["Biology", "Physics",
                                     "Chemistry", "Mathematics"],
                            value="Biology", label="Subject"
                        )
                        t_chapter = gr.Textbox(
                            label="Chapter Name",
                            placeholder="e.g. Photosynthesis in Higher Plants"
                        )
                        t_exam = gr.Dropdown(
                            choices=["NEET", "JEE_MAINS", "JEE_ADVANCED"],
                            value="NEET", label="Exam Type"
                        )
                        t_diff = gr.Dropdown(
                            choices=["Easy", "Medium", "Hard", "Mixed"],
                            value="Medium", label="Difficulty"
                        )
                        t_num = gr.Slider(
                            minimum=1, maximum=30,
                            value=5, step=1,
                            label="Number of Questions"
                        )
                        t_btn = gr.Button(
                            " Generate Quiz",
                            variant="primary", size="lg"
                        )

                    with gr.Column(scale=2):
                        gr.Markdown("###  Paste Your Notes")
                        t_notes = gr.Textbox(
                            label="Study Notes",
                            placeholder="Paste your notes here...",
                            lines=18
                        )

                with gr.Row():
                    t_status  = gr.Textbox(
                        label="Status", interactive=False, scale=3
                    )
                    t_session = gr.Textbox(
                        label="Session ID", interactive=False, scale=1
                    )
                    t_time    = gr.Textbox(
                        label="Time Limit (min)", interactive=False, scale=1
                    )

                with gr.Tabs():
                    with gr.TabItem(" Rendered Quiz"):
                        t_rendered = gr.Markdown()
                    with gr.TabItem("πŸ”§ Raw JSON"):
                        t_json = gr.Code(
                            language="json",
                            label="JSON Output",
                            lines=20
                        )

            # ── Tab 2: PDF input ─────────────────────────────
            with gr.TabItem(" From PDF"):
                with gr.Row():
                    with gr.Column(scale=1, min_width=260):
                        gr.Markdown("###  Settings")
                        p_subject = gr.Dropdown(
                            choices=["Biology", "Physics",
                                     "Chemistry", "Mathematics"],
                            value="Biology", label="Subject"
                        )
                        p_chapter = gr.Textbox(
                            label="Chapter Name",
                            placeholder="e.g. Laws of Motion"
                        )
                        p_exam = gr.Dropdown(
                            choices=["NEET", "JEE_MAINS", "JEE_ADVANCED"],
                            value="NEET", label="Exam Type"
                        )
                        p_diff = gr.Dropdown(
                            choices=["Easy", "Medium", "Hard", "Mixed"],
                            value="Medium", label="Difficulty"
                        )
                        p_num = gr.Slider(
                            minimum=1, maximum=30,
                            value=5, step=1,
                            label="Number of Questions"
                        )
                        p_btn = gr.Button(
                            "πŸš€ Generate from PDF",
                            variant="primary", size="lg"
                        )

                    with gr.Column(scale=2):
                        gr.Markdown("###  Upload PDF")
                        p_file = gr.File(
                            label="Upload PDF Notes",
                            file_types=[".pdf"],
                            type="filepath"   # gives us the path string
                        )
                        gr.Markdown("""
                        **Tips:** Single chapter PDFs work best.
                        Text-based PDFs only (not scanned images).
                        """)

                with gr.Row():
                    p_status  = gr.Textbox(
                        label="Status", interactive=False, scale=3
                    )
                    p_session = gr.Textbox(
                        label="Session ID", interactive=False, scale=1
                    )
                    p_time    = gr.Textbox(
                        label="Time Limit (min)", interactive=False, scale=1
                    )

                with gr.Tabs():
                    with gr.TabItem(" Rendered Quiz"):
                        p_rendered = gr.Markdown()
                    with gr.TabItem(" Raw JSON"):
                        p_json = gr.Code(
                            language="json",
                            label="JSON Output",
                            lines=20
                        )

            # ── Tab 3: API reference ─────────────────────────
            with gr.TabItem(" API Reference"):
                gr.Markdown("""
                ## Using the REST API from your webapp

                ### Base URL
                ```
                https://YOUR-USERNAME-YOUR-SPACE-NAME.hf.space
                ```

                ### Health check
                ```
                GET /health
                ```

                ### Generate from text
                ```
                POST /generate/from-text
                Content-Type: application/json

                {
                  "text": "your notes...",
                  "subject": "Biology",
                  "chapter": "Photosynthesis",
                  "topic": "Light reactions Calvin cycle",
                  "exam_type": "NEET",
                  "difficulty": "Medium",
                  "num_questions": 10
                }
                ```

                ### Generate from PDF
                ```
                POST /generate/from-pdf
                Content-Type: multipart/form-data

                file: <pdf>
                subject: Biology
                chapter: Photosynthesis
                exam_type: NEET
                difficulty: Medium
                num_questions: 10
                ```

                ### Interactive Swagger docs
                ```
                GET /docs
                ```
                """)

        gr.Markdown(
            "---\nBuilt with πŸ€— Gradio + FastAPI + Gemini + FAISS"
        )

        # ── Wire up buttons ──────────────────────────────────
        t_btn.click(
            fn=gradio_generate_text,
            inputs=[t_subject, t_chapter, t_exam,
                    t_diff, t_num, t_notes],
            outputs=[t_json, t_status, t_session, t_time]
        ).then(
            fn=render_quiz,
            inputs=[t_json],
            outputs=[t_rendered]
        )

        p_btn.click(
            fn=gradio_generate_pdf,
            inputs=[p_subject, p_chapter, p_exam,
                    p_diff, p_num, p_file],
            outputs=[p_json, p_status, p_session, p_time]
        ).then(
            fn=render_quiz,
            inputs=[p_json],
            outputs=[p_rendered]
        )

    return demo


# ═════════════════════════════════════════════════════════════
# MOUNT GRADIO INTO FASTAPI  β€” single process, single port
# ═════════════════════════════════════════════════════════════
gradio_blocks = build_gradio_blocks()

app = gr.mount_gradio_app(
    app    = fastapi_app,
    blocks = gradio_blocks,
    path   = "/"
)