File size: 3,449 Bytes
526fa24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# In api/main.py
from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel
from typing import List, Optional
import sys
import os

# Add parent directory to path to import modules
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

from ai.huggingface import HuggingFaceService
from ai.agents import AgentCoordinator
from data.content import ContentManager
from data.users import UserManager

app = FastAPI(
    title="Micro Learning API",
    description="API for microlearning content and personalization",
    version="0.1.0"
)

# Initialize services
hf_service = HuggingFaceService()
agent_coordinator = AgentCoordinator(hf_service)
content_manager = ContentManager()
user_manager = UserManager()

# Models
class ContentBase(BaseModel):
    title: str
    text: str
    tags: List[str]
    
class ContentCreate(ContentBase):
    pass
    
class Content(ContentBase):
    id: str
    
class UserBase(BaseModel):
    name: str
    email: str
    
class UserCreate(UserBase):
    pass
    
class User(UserBase):
    id: str
    progress: dict = {}
    
class ProgressUpdate(BaseModel):
    module_id: str
    completion: float  # 0.0 to 1.0
    
# Routes
@app.get("/content/{content_id}", response_model=Content)
async def get_content(content_id: str):
    content = content_manager.get_by_id(content_id)
    if not content:
        raise HTTPException(status_code=404, detail="Content not found")
    return content

@app.post("/content/", response_model=str)
async def create_content(content: ContentCreate):
    content_id = content_manager.save_content(content.dict())
    return content_id

@app.get("/content/{content_id}/summary")
async def get_summary(content_id: str):
    content = content_manager.get_by_id(content_id)
    if not content:
        raise HTTPException(status_code=404, detail="Content not found")
    
    summary = hf_service.summarize_content(content['text'])
    return {"summary": summary[0]['summary_text']}

@app.post("/content/{content_id}/ask")
async def ask_question(content_id: str, question: str):
    content = content_manager.get_by_id(content_id)
    if not content:
        raise HTTPException(status_code=404, detail="Content not found")
    
    answer = hf_service.answer_question(question, content['text'])
    return {"answer": answer['answer'], "confidence": answer['score']}

@app.get("/content/{content_id}/quiz")
async def generate_quiz(content_id: str):
    content = content_manager.get_by_id(content_id)
    if not content:
        raise HTTPException(status_code=404, detail="Content not found")
    
    questions = agent_coordinator.dispatch("quiz", content['text'])
    return {"questions": questions}

@app.post("/users/", response_model=str)
async def create_user(user: UserCreate):
    user_id = user_manager.create_user(user.dict())
    return user_id

@app.get("/users/{user_id}", response_model=User)
async def get_user(user_id: str):
    user = user_manager.get_user(user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
    return user

@app.post("/users/{user_id}/progress")
async def update_progress(user_id: str, update: ProgressUpdate):
    user = user_manager.get_user(user_id)
    if not user:
        raise HTTPException(status_code=404, detail="User not found")
        
    user_manager.update_progress(user_id, update.module_id, update.completion)
    return {"status": "updated"}