pratyushjdhv commited on
Commit
48982a9
Β·
verified Β·
1 Parent(s): 9570a04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -28
app.py CHANGED
@@ -4,19 +4,23 @@ import json
4
  import time
5
  import tempfile
6
 
 
 
 
7
  import google.generativeai as genai
8
  from fastapi import FastAPI, HTTPException, UploadFile, File
9
  from fastapi.middleware.cors import CORSMiddleware
10
  from pydantic import BaseModel, Field, validator
11
 
12
  from quiz import QuizManager, SUBJECTS, DIFFICULTIES
 
13
 
14
  # ── Startup checks ───────────────────────────────────────────
15
  api_key = os.getenv("GEMINI_API_KEY")
16
  if not api_key:
17
  raise ValueError("GEMINI_API_KEY environment variable not set")
18
 
19
- # ── App ──────────────────────────────────────────────────────
20
  app = FastAPI(
21
  title="NEET/JEE Quiz Generator API",
22
  description="RAG-powered MCQ generator for competitive exam prep",
@@ -31,10 +35,10 @@ app.add_middleware(
31
  allow_headers=["*"],
32
  )
33
 
34
- # ── Global quiz manager (model loaded once at startup) ────────
35
  quiz_manager = QuizManager()
36
 
37
- # ── Request / Response models ─────────────────────────────────
38
  class TextRequest(BaseModel):
39
  text: str = Field(..., min_length=100)
40
  subject: str = Field(...)
@@ -69,8 +73,8 @@ class HealthResponse(BaseModel):
69
  model: str
70
  version: str
71
 
72
- # ── Endpoints ─────────────────────────────────────────────────
73
- @app.get("/", response_model=HealthResponse)
74
  async def health():
75
  return HealthResponse(
76
  status="healthy",
@@ -88,25 +92,22 @@ async def generate_from_text(req: TextRequest):
88
  status_code=400,
89
  detail="Notes too short. Please provide more content."
90
  )
91
-
92
  session = quiz_manager.create_quiz(
93
- topic = req.topic,
94
- subject = req.subject,
95
- chapter = req.chapter,
96
- exam_type = req.exam_type,
97
- difficulty = req.difficulty,
98
- num_questions = req.num_questions
99
  )
100
-
101
  return QuizResponse(
102
- success = True,
103
- session_id = session.session_id,
104
- total_questions = session.total_questions,
105
- time_limit_minutes = session.time_limit_minutes,
106
- quiz = session.to_dict(),
107
- generated_in_seconds = round(time.time() - start, 2)
108
  )
109
-
110
  except HTTPException:
111
  raise
112
  except RuntimeError as e:
@@ -134,7 +135,9 @@ async def generate_from_pdf(
134
  )
135
 
136
  start = time.time()
137
- with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as tmp:
 
 
138
  content = await file.read()
139
  tmp.write(content)
140
  tmp_path = tmp.name
@@ -147,12 +150,12 @@ async def generate_from_pdf(
147
  num_questions=num_questions
148
  )
149
  return QuizResponse(
150
- success = True,
151
- session_id = session.session_id,
152
- total_questions = session.total_questions,
153
- time_limit_minutes = session.time_limit_minutes,
154
- quiz = session.to_dict(),
155
- generated_in_seconds = round(time.time() - start, 2)
156
  )
157
  except Exception as e:
158
  raise HTTPException(
@@ -224,4 +227,14 @@ def _estimate_rank(percentage: float, exam_type: str) -> str:
224
  if percentage >= 70: return "IIT eligible"
225
  if percentage >= 55: return "NIT Top-tier eligible"
226
  if percentage >= 40: return "NIT eligible"
227
- return "Below JEE cut-off range"
 
 
 
 
 
 
 
 
 
 
 
4
  import time
5
  import tempfile
6
 
7
+ import gradio as gr
8
+ from gradio.routes import mount_gradio_app
9
+
10
  import google.generativeai as genai
11
  from fastapi import FastAPI, HTTPException, UploadFile, File
12
  from fastapi.middleware.cors import CORSMiddleware
13
  from pydantic import BaseModel, Field, validator
14
 
15
  from quiz import QuizManager, SUBJECTS, DIFFICULTIES
16
+ from gradio_ui import build_gradio_app
17
 
18
  # ── Startup checks ───────────────────────────────────────────
19
  api_key = os.getenv("GEMINI_API_KEY")
20
  if not api_key:
21
  raise ValueError("GEMINI_API_KEY environment variable not set")
22
 
23
+ # ── FastAPI app ──────────────────────────────────────────────
24
  app = FastAPI(
25
  title="NEET/JEE Quiz Generator API",
26
  description="RAG-powered MCQ generator for competitive exam prep",
 
35
  allow_headers=["*"],
36
  )
37
 
38
+ # ── Global quiz manager ───────────────────────────────────────
39
  quiz_manager = QuizManager()
40
 
41
+ # ── Pydantic models ───────────────────────────────────────────
42
  class TextRequest(BaseModel):
43
  text: str = Field(..., min_length=100)
44
  subject: str = Field(...)
 
73
  model: str
74
  version: str
75
 
76
+ # ── API Endpoints ─────────────────────────────────────────────
77
+ @app.get("/api/health", response_model=HealthResponse)
78
  async def health():
79
  return HealthResponse(
80
  status="healthy",
 
92
  status_code=400,
93
  detail="Notes too short. Please provide more content."
94
  )
 
95
  session = quiz_manager.create_quiz(
96
+ topic=req.topic,
97
+ subject=req.subject,
98
+ chapter=req.chapter,
99
+ exam_type=req.exam_type,
100
+ difficulty=req.difficulty,
101
+ num_questions=req.num_questions
102
  )
 
103
  return QuizResponse(
104
+ success=True,
105
+ session_id=session.session_id,
106
+ total_questions=session.total_questions,
107
+ time_limit_minutes=session.time_limit_minutes,
108
+ quiz=session.to_dict(),
109
+ generated_in_seconds=round(time.time() - start, 2)
110
  )
 
111
  except HTTPException:
112
  raise
113
  except RuntimeError as e:
 
135
  )
136
 
137
  start = time.time()
138
+ with tempfile.NamedTemporaryFile(
139
+ suffix=".pdf", delete=False
140
+ ) as tmp:
141
  content = await file.read()
142
  tmp.write(content)
143
  tmp_path = tmp.name
 
150
  num_questions=num_questions
151
  )
152
  return QuizResponse(
153
+ success=True,
154
+ session_id=session.session_id,
155
+ total_questions=session.total_questions,
156
+ time_limit_minutes=session.time_limit_minutes,
157
+ quiz=session.to_dict(),
158
+ generated_in_seconds=round(time.time() - start, 2)
159
  )
160
  except Exception as e:
161
  raise HTTPException(
 
227
  if percentage >= 70: return "IIT eligible"
228
  if percentage >= 55: return "NIT Top-tier eligible"
229
  if percentage >= 40: return "NIT eligible"
230
+ return "Below JEE cut-off range"
231
+
232
+ # ── Mount Gradio at root "/" ──────────────────────────────────
233
+ # API routes above still work at their paths
234
+ # Gradio UI is served at "/"
235
+ gradio_app = build_gradio_app()
236
+ app = mount_gradio_app(
237
+ app=app,
238
+ blocks=gradio_app,
239
+ path="/" # Gradio at root, API at /generate/... /score
240
+ )