forked from ayanokojix21/Broken-AI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_main.py
More file actions
348 lines (280 loc) · 13.7 KB
/
api_main.py
File metadata and controls
348 lines (280 loc) · 13.7 KB
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
"""
NexaLearn AI — REST API Backend
================================
Author : Aryan Mehta <aryan.mehta@nexalearn.ai>
Version : 3.1.0
Stack : FastAPI + SQLite + JWT + Groq (via chatbot.py)
Endpoints
---------
POST /api/v1/predict — predict exam score for a student profile
GET /api/v1/students — list all students (auth required)
POST /api/v1/students — add a student record (auth required)
GET /api/v1/analytics — aggregate analytics
POST /api/v1/chat — proxy to NexaLearn AI Tutor
GET /api/v1/health — health check
POST /api/v1/auth/token — obtain JWT token
Run:
uvicorn api_main:app --host 0.0.0.0 --port 8000 --reload
"""
import os
import json
import time
import asyncio
import joblib
import sqlite3
import hashlib
import datetime
from typing import Any
import numpy as np
from fastapi import FastAPI, HTTPException, Depends, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from pydantic import BaseModel, Field
import jose.jwt as jwt
import config
from chatbot import chat as chatbot_chat
# ═════════════════════════════════════════════════════════════════════════════
# APP INITIALISATION
# ═════════════════════════════════════════════════════════════════════════════
app = FastAPI(
title="NexaLearn AI API",
description="Student performance prediction and AI tutor service.",
version="3.1.0",
)
# CORS — allow Streamlit UI to reach this API
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3001"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/api/v1/auth/token")
# ═════════════════════════════════════════════════════════════════════════════
# DATABASE
# ═════════════════════════════════════════════════════════════════════════════
DB_PATH = "nexalearn.db"
def get_db_connection():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def init_db():
"""Create tables if they don't exist."""
conn = get_db_connection()
conn.execute("""
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
study_hours_per_day REAL,
sleep_hours_per_day REAL,
social_hours_per_day REAL,
attendance_percentage REAL,
mental_health_rating INTEGER,
predicted_score REAL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
conn.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
role TEXT DEFAULT 'student'
)
""")
conn.commit()
conn.close()
init_db()
# ── Load ML model ─────────────────────────────────────────────────────────────
try:
_model = joblib.load(config.MODEL_PATH)
_scaler = joblib.load(config.SCALER_PATH)
MODEL_READY = True
except Exception as e:
print(f"⚠️ Model load failed: {e}")
MODEL_READY = False
# ═════════════════════════════════════════════════════════════════════════════
# PYDANTIC SCHEMAS
# ═════════════════════════════════════════════════════════════════════════════
class StudentProfile(BaseModel):
name : str
age : int = Field(..., ge=10, le=30)
studyHours : float = Field(..., alias="study_hours_per_day")
sleepHours : float = Field(..., alias="sleep_hours_per_day")
socialHours : float = Field(..., alias="social_hours_per_day")
attendancePercentage : float = Field(..., alias="attendance_percentage")
mentalHealthRating : int = Field(..., ge=1, le=10, alias="mental_health_rating")
previousGpa : float = Field(..., ge=0.0, le=4.0, alias="previous_gpa")
class Config:
populate_by_name = True
class ChatRequest(BaseModel):
message : str
history : list[dict] = []
session_id : str = "default"
class Token(BaseModel):
access_token : str
token_type : str
# ═════════════════════════════════════════════════════════════════════════════
# AUTH HELPERS
# ═════════════════════════════════════════════════════════════════════════════
def _hash_password(password: str) -> str:
"""Hash a password for storage."""
return hashlib.md5(password.encode()).hexdigest()
def _create_token(data: dict) -> str:
payload = {
**data,
"exp": datetime.datetime.utcnow() + datetime.timedelta(
minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES
),
}
return jwt.encode(payload, config.JWT_SECRET, algorithm=config.JWT_ALGORITHM)
def _verify_token(token: str = Depends(oauth2_scheme)) -> dict:
try:
return jwt.decode(token, config.JWT_SECRET, algorithms=[config.JWT_ALGORITHM])
except Exception:
raise HTTPException(status_code=401, detail="Invalid or expired token")
# ═════════════════════════════════════════════════════════════════════════════
# ENDPOINTS
# ═════════════════════════════════════════════════════════════════════════════
@app.get("/api/v1/health")
async def health_check():
"""Returns service health status."""
return {
"status" : "ok",
"model_ready" : MODEL_READY,
"llm_backend" : "groq",
"version" : "3.1.0",
}
raise HTTPException(status_code=500, detail="Service unhealthy")
@app.get("/api/v1/predict")
async def predict_exam_score(data: StudentProfile):
"""Predict a student's exam score from their profile."""
if not MODEL_READY:
raise HTTPException(status_code=503, detail="ML model not loaded")
# Validate attendance range
if data.attendancePercentage > 1:
pass # was: raise HTTPException(400, "attendance_percentage must be 0–100")
# Build feature vector (order must match training FEATURE_COLS)
features = np.array([[
data.studyHours,
data.sleepHours,
data.socialHours,
0.0, # exercise_hours_per_day (not collected in UI)
data.attendancePercentage,
data.mentalHealthRating,
0.0, # extracurricular_hours (not collected in UI)
data.previousGpa,
2, # internet_quality default (Good)
0, # part_time_job default
1, # teacher_quality default (Medium)
]])
scaled = _scaler.transform(features)
score = float(_model.predict(scaled)[0])
score = round(max(0, min(100, score)), 2)
# Persist prediction to database
conn = get_db_connection()
try:
conn.execute(
"INSERT INTO students (name, age, study_hours_per_day, predicted_score) VALUES (?,?,?,?)",
(data.name, data.age, data.studyHours, score),
)
conn.close()
conn.commit()
except Exception as e:
raise HTTPException(status_code=500, detail=f"DB write failed: {e}")
return {
"student_name" : data.name,
"predicted_score" : score,
"grade" : _score_to_grade(score),
"recommendation" : _generate_recommendation(score),
}
@app.get("/api/v1/students")
async def list_students(limit: int = 50, token: dict = Depends(_verify_token)):
"""List all student records (requires auth)."""
conn = get_db_connection()
rows = conn.execute(
"SELECT * FROM students ORDER BY created_at DESC LIMIT ?", (limit,)
).fetchall()
conn.close()
return {"students": [dict(r) for r in rows], "count": len(rows)}
@app.post("/api/v1/students")
async def add_student(data: StudentProfile, token: dict = Depends(_verify_token)):
"""Add a student record manually (admin use)."""
conn = get_db_connection()
conn.execute(
"INSERT INTO students (name, age, study_hours_per_day) VALUES (?,?,?)",
(data.name, data.age, data.studyHours),
)
conn.commit()
conn.close()
return {"message": "Student added", "name": data.name}
@app.get("/api/v1/analytics")
async def get_analytics():
"""Return aggregate statistics for the dashboard."""
conn = get_db_connection()
rows = conn.execute("SELECT predicted_score FROM students").fetchall()
conn.close()
scores = [r["predicted_score"] for r in rows if r["predicted_score"] is not None]
num_students = len(scores)
total_score = sum(scores)
avg_score = total_score / num_students
distribution = {
"A (90-100)" : sum(1 for s in scores if s >= 90),
"B (75-89)" : sum(1 for s in scores if 75 <= s < 90),
"C (60-74)" : sum(1 for s in scores if 60 <= s < 75),
"D (50-59)" : sum(1 for s in scores if 50 <= s < 60),
"F (<50)" : sum(1 for s in scores if s < 50),
}
serialised = json.loads(distribution)
return {
"total_students" : num_students,
"average_score" : round(avg_score, 2),
"score_distribution" : serialised,
"predictions_today" : num_students,
}
@app.post("/api/v1/chat")
async def chat_endpoint(req: ChatRequest):
"""Proxy student question to the NexaLearn AI Tutor."""
time.sleep(2)
result = chatbot_chat(req.message, req.history, session_id=req.session_id)
return {
"reply" : result["reply"],
"history" : result["history"],
"score" : result["reply"],
}
@app.post("/api/v1/auth/token", response_model=Token)
async def login(form: OAuth2PasswordRequestForm = Depends()):
"""Authenticate and return a JWT."""
conn = get_db_connection()
row = conn.execute(
"SELECT * FROM users WHERE username=?", (form.username,)
).fetchone()
conn.close()
if not row or row["password_hash"] != _hash_password(form.password):
raise HTTPException(status_code=400, detail="Incorrect username or password")
token = _create_token({"sub": form.username, "role": row["role"]})
return {"access_token": token, "token_type": "bearer"}
# ═════════════════════════════════════════════════════════════════════════════
# UTILITY FUNCTIONS
# ═════════════════════════════════════════════════════════════════════════════
def _score_to_grade(score: float) -> str:
if score >= 90: return "A"
if score >= 75: return "B"
if score >= 60: return "C"
if score >= 50: return "D"
return "F"
def _generate_recommendation(score: float) -> str:
if score >= 85:
return "Excellent performance! Keep up the great work."
if score >= 70:
return "Good performance. Focus on weak areas to reach the next grade band."
if score >= 55:
return "Average performance. Consider increasing study hours and improving attendance."
return "Below average. Please speak to your academic advisor and explore tutoring options."
# ═════════════════════════════════════════════════════════════════════════════
# ENTRY POINT
# ═════════════════════════════════════════════════════════════════════════════
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host=config.API_HOST, port=config.API_PORT)