-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastapi_server.py
More file actions
514 lines (444 loc) · 15.8 KB
/
fastapi_server.py
File metadata and controls
514 lines (444 loc) · 15.8 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
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
from fastapi import FastAPI, Request, HTTPException, status
from fastapi.responses import JSONResponse, HTMLResponse
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel, Field
from typing import Optional, List, Dict, Any
import os
import json
import uvicorn
from datetime import datetime
from groq import Groq
from dotenv import load_dotenv
load_dotenv()
app = FastAPI(
title="Smart Academic Assistant API",
description="AI-Powered Learning Platform API",
version="1.0.0",
)
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
if not GROQ_API_KEY:
raise ValueError("GROQ_API_KEY not found in environment variables. Please check your .env file.")
client = Groq(api_key=GROQ_API_KEY)
model = "llama-3.3-70b-versatile"
class Message(BaseModel):
role: str
content: str
class ChatRequest(BaseModel):
messages: List[Message]
topic: Optional[str] = None
level: Optional[str] = "Intermediate"
class QuizRequest(BaseModel):
topic: str
num_questions: int = 10
level: str = "Intermediate"
class ContentRequest(BaseModel):
topic: str
level: str = "Intermediate"
class MindmapRequest(BaseModel):
topic: str
level: str = "Intermediate"
class LoginRequest(BaseModel):
username: str
password: str
class RegisterRequest(BaseModel):
username: str
password: str
email: str
role: str
@app.get("/")
async def root():
return {
"message": "🎓 Smart Academic Assistant API is running!",
"status": "active",
"docs": "/docs",
"redoc": "/redoc",
"endpoints": {
"health": "/health",
"generate_content": "/generate/content (POST)",
"generate_quiz": "/generate/quiz (POST)",
"generate_mindmap": "/generate/mindmap (POST)",
"chat": "/chat (POST)",
"auth_login": "/auth/login (POST)",
"auth_register": "/auth/register (POST)"
}
}
@app.get("/health")
async def health_check():
return {
"status": "healthy",
"timestamp": datetime.now().isoformat(),
"groq_api_configured": bool(GROQ_API_KEY)
}
@app.post("/generate/content")
async def generate_content(request: ContentRequest):
"""Generate educational content based on topic and level"""
try:
level_descriptions = {
"Beginner": "basic introduction, simple explanations, no prior knowledge assumed",
"Intermediate": "moderate depth, assumes basic knowledge, includes practical examples",
"Advanced": "deep technical details, advanced concepts, real-world applications and best practices"
}
prompt = f"""You are an expert educator. Create comprehensive {request.level} level educational content about "{request.topic}".
Level Description: {level_descriptions.get(request.level, level_descriptions['Intermediate'])}
Format your response as JSON with EXACTLY this structure:
{{
"definition": "A clear, 2-3 sentence definition appropriate for {request.level} level",
"detailed_overview": "A detailed 4-5 paragraph explanation with {request.level} level depth and examples",
"key_concepts": [
"Key concept 1 with detailed explanation",
"Key concept 2 with detailed explanation",
"Key concept 3 with detailed explanation",
"Key concept 4 with detailed explanation",
"Key concept 5 with detailed explanation"
],
"practical_examples": [
"Practical example 1 relevant to {request.level} level",
"Practical example 2 relevant to {request.level} level",
"Practical example 3 relevant to {request.level} level"
],
"summary": "A concise 2-3 sentence summary highlighting key takeaways for {request.level} learners"
}}
Make the content educational, engaging, and appropriate for {request.level} level learners."""
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert educator. Always respond with valid JSON only."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=4096
)
content = response.choices[0].message.content
content = content.replace('```json', '').replace('```', '').strip()
result = json.loads(content)
return {"success": True, "data": result, "topic": request.topic, "level": request.level}
except Exception as e:
return JSONResponse(
status_code=500,
content={"success": False, "error": str(e)}
)
@app.post("/generate/quiz")
async def generate_quiz(request: QuizRequest):
"""Generate quiz questions based on topic and level"""
try:
prompt = f"""Create a {request.level} level quiz about "{request.topic}" with exactly {request.num_questions} questions.
Format your response as JSON with EXACTLY this structure:
{{
"topic": "{request.topic}",
"level": "{request.level}",
"questions": [
{{
"question": "Question text here",
"options": ["Option A", "Option B", "Option C", "Option D"],
"correct": 0,
"explanation": "Detailed explanation"
}}
]
}}
Each question must have exactly 4 options. 'correct' must be the index (0-3) of the correct answer.
Provide ONLY valid JSON in your response."""
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert quiz creator. Always respond with valid JSON only."},
{"role": "user", "content": prompt}
],
temperature=0.8,
max_tokens=4096
)
content = response.choices[0].message.content
content = content.replace('```json', '').replace('```', '').strip()
result = json.loads(content)
return {"success": True, "data": result}
except Exception as e:
return JSONResponse(
status_code=500,
content={"success": False, "error": str(e)}
)
@app.post("/generate/mindmap")
async def generate_mindmap(request: MindmapRequest):
"""Generate learning roadmap/mindmap based on topic and level"""
try:
prompt = f"""Create a {request.level} level learning roadmap/mindmap for mastering "{request.topic}".
Format your response as JSON with EXACTLY this structure:
{{
"topic": "{request.topic}",
"level": "{request.level}",
"central_concept": "The main concept in one sentence",
"main_branches": [
{{
"name": "Branch name 1",
"subtopics": ["Subtopic 1", "Subtopic 2", "Subtopic 3", "Subtopic 4"]
}},
{{
"name": "Branch name 2",
"subtopics": ["Subtopic 1", "Subtopic 2", "Subtopic 3", "Subtopic 4"]
}}
],
"learning_path": ["Step 1", "Step 2", "Step 3", "Step 4", "Step 5", "Step 6"]
}}
Create 4-6 main branches with 4-6 subtopics each.
Provide ONLY valid JSON in your response."""
response = client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": "You are an expert curriculum designer. Always respond with valid JSON only."},
{"role": "user", "content": prompt}
],
temperature=0.7,
max_tokens=4096
)
content = response.choices[0].message.content
content = content.replace('```json', '').replace('```', '').strip()
result = json.loads(content)
return {"success": True, "data": result}
except Exception as e:
return JSONResponse(
status_code=500,
content={"success": False, "error": str(e)}
)
@app.post("/chat")
async def chat(request: ChatRequest):
"""Interactive chat with AI assistant"""
try:
system_prompt = """You are a helpful educational assistant. Help students with their academic questions,
provide explanations, study tips, and guidance. Be supportive and educational."""
messages = [{"role": "system", "content": system_prompt}]
for msg in request.messages:
messages.append({"role": msg.role, "content": msg.content})
response = client.chat.completions.create(
model=model,
messages=messages,
temperature=0.7,
max_tokens=1024
)
return {
"success": True,
"response": response.choices[0].message.content,
"timestamp": datetime.now().isoformat()
}
except Exception as e:
return JSONResponse(
status_code=500,
content={"success": False, "error": str(e)}
)
users_db = {}
@app.post("/auth/register")
async def register(request: RegisterRequest):
"""Register a new user"""
try:
if request.username in users_db:
return JSONResponse(
status_code=400,
content={"success": False, "message": "Username already exists"}
)
users_db[request.username] = {
"username": request.username,
"password": request.password,
"email": request.email,
"role": request.role,
"created_at": datetime.now().isoformat()
}
return {
"success": True,
"message": "Registration successful",
"user": {"username": request.username, "role": request.role}
}
except Exception as e:
return JSONResponse(
status_code=500,
content={"success": False, "message": str(e)}
)
@app.post("/auth/login")
async def login(request: LoginRequest):
"""Login user"""
try:
user = users_db.get(request.username)
if not user or user["password"] != request.password:
return JSONResponse(
status_code=401,
content={"success": False, "message": "Invalid credentials"}
)
return {
"success": True,
"message": "Login successful",
"user": {"username": request.username, "role": user["role"]}
}
except Exception as e:
return JSONResponse(
status_code=500,
content={"success": False, "message": str(e)}
)
@app.get("/materials/school/{subject}")
async def get_school_materials(subject: str):
"""Get school study materials by subject"""
materials = {
"Mathematics": {
"Algebra": "Comprehensive Algebra guide",
"Geometry": "Geometry concepts",
"Calculus": "Calculus fundamentals"
},
"Science": {
"Physics": "Physics fundamentals",
"Chemistry": "Chemical reactions",
"Biology": "Living organisms"
},
"English": {
"Grammar": "Grammar rules",
"Literature": "Literary analysis"
},
"Social Studies": {
"History": "Historical events",
"Geography": "Physical geography"
},
"Computer Science": {
"Programming": "Programming basics",
"Web Development": "Web technologies"
}
}
return {
"success": True,
"subject": subject,
"materials": materials.get(subject, {})
}
@app.get("/materials/placement")
async def get_placement_guides():
"""Get placement preparation guides"""
guides = {
"Aptitude Test Guide": "Complete aptitude preparation",
"Technical Interview Guide": "Technical questions",
"HR Interview Guide": "HR interview tips",
"Resume Building Guide": "Resume creation guide"
}
return {"success": True, "guides": guides}
@app.get("/projects")
async def get_projects():
"""Get project examples"""
projects = [
{
"id": 1,
"title": "E-commerce Website",
"description": "Full-stack e-commerce platform",
"technologies": ["React", "Node.js", "MongoDB"],
"features": ["Authentication", "Products", "Cart", "Payment"]
},
{
"id": 2,
"title": "Task Management App",
"description": "Project management tool",
"technologies": ["Vue.js", "Express", "PostgreSQL"],
"features": ["Tasks", "Real-time", "Collaboration"]
}
]
return {"success": True, "projects": projects}
@app.get("/tests/assessment")
async def get_assessment_tests():
"""Get assessment tests"""
tests = [
{
"id": 1,
"name": "Quantitative Aptitude Test",
"duration": "45 minutes",
"difficulty": "Medium",
"topics": ["Number System", "Algebra", "Geometry"]
},
{
"id": 2,
"name": "Logical Reasoning Test",
"duration": "35 minutes",
"difficulty": "Medium",
"topics": ["Analytical", "Puzzles"]
}
]
return {"success": True, "tests": tests}
@app.get("/time-charts")
async def get_time_charts():
"""Get time allocation charts"""
charts = {
"Quantitative Aptitude": {
"Time Allocation": "60 hours",
"Weekly Plan": "12 hours per week"
},
"Logical Reasoning": {
"Time Allocation": "50 hours",
"Weekly Plan": "10 hours per week"
}
}
return {"success": True, "charts": charts}
@app.get("/dashboard", response_class=HTMLResponse)
async def dashboard():
"""HTML dashboard"""
return """
<!DOCTYPE html>
<html>
<head>
<title>Smart Academic Assistant API</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
}
.container {
max-width: 800px;
margin: 0 auto;
text-align: center;
}
.card {
background: rgba(255,255,255,0.95);
color: #333;
padding: 20px;
border-radius: 10px;
margin: 20px 0;
}
.docs-link {
display: inline-block;
background: #667eea;
color: white;
padding: 10px 20px;
text-decoration: none;
border-radius: 5px;
margin: 10px;
}
</style>
</head>
<body>
<div class="container">
<h1>🎓 Smart Academic Assistant API</h1>
<p>AI-Powered Learning Platform</p>
<div class="card">
<h2>📚 API Documentation</h2>
<a href="/docs" class="docs-link">📖 Interactive API Docs</a>
<a href="/redoc" class="docs-link">📕 ReDoc Documentation</a>
</div>
<div class="card">
<h2>🚀 Ready to Use</h2>
<p>API is running successfully!</p>
<p>Visit <strong>/docs</strong> for interactive documentation</p>
</div>
</div>
</body>
</html>
"""
if __name__ == "__main__":
print("🚀 Starting Smart Academic Assistant API Server...")
print("📍 Server will run at: http://localhost:8000")
print("📚 API Documentation: http://localhost:8000/docs")
print("🔧 Press CTRL+C to stop the server")
print("-" * 50)
uvicorn.run(
app,
host="127.0.0.1",
port=8000,
log_level="info",
reload=False
)