Spaces:
Sleeping
Sleeping
| """ | |
| FastAPI Learning Assistant for Maryam & Zayn educational platform | |
| """ | |
| import os | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.middleware.cors import CORSMiddleware | |
| from pydantic import BaseModel | |
| from typing import List, Optional | |
| from dotenv import load_dotenv | |
| from agent import MaryamZaynChatbot | |
| # Load environment variables | |
| load_dotenv() | |
| # Initialize FastAPI app | |
| app = FastAPI( | |
| title="Maryam & Zayn Learning Assistant API", | |
| description="AI-powered learning assistant for Maryam & Zayn — Pakistan's first character-based educational platform", | |
| version="1.0.0" | |
| ) | |
| # CORS configuration for frontend access | |
| app.add_middleware( | |
| CORSMiddleware, | |
| allow_origins=["*"], # Configure specific origins in production | |
| allow_credentials=True, | |
| allow_methods=["*"], | |
| allow_headers=["*"], | |
| ) | |
| # Request/Response models | |
| class ChatMessage(BaseModel): | |
| role: str | |
| content: str | |
| class ChatRequest(BaseModel): | |
| message: str | |
| history: Optional[List[ChatMessage]] = [] | |
| class ChatResponse(BaseModel): | |
| response: str | |
| success: bool | |
| # Initialize chatbot | |
| chatbot = MaryamZaynChatbot() | |
| async def root(): | |
| """Health check endpoint""" | |
| return { | |
| "status": "online", | |
| "message": "Maryam & Zayn Learning Assistant is running", | |
| "version": "1.0.0" | |
| } | |
| async def health_check(): | |
| """Health check for deployment monitoring""" | |
| return {"status": "healthy"} | |
| async def chat(request: ChatRequest): | |
| """ | |
| Chat endpoint for learning questions | |
| - **message**: User's question about subjects or platform features | |
| - **history**: Optional conversation history for context | |
| """ | |
| try: | |
| # Get response from chatbot agent | |
| response = await chatbot.chat( | |
| message=request.message, | |
| history=request.history | |
| ) | |
| return ChatResponse( | |
| response=response, | |
| success=True | |
| ) | |
| except Exception as e: | |
| raise HTTPException( | |
| status_code=500, | |
| detail=f"Error processing chat: {str(e)}" | |
| ) | |
| async def get_info(): | |
| """Get platform information""" | |
| return { | |
| "platform": "Maryam & Zayn", | |
| "tagline": "Pakistan's first character-based learning platform", | |
| "target": "Pakistani children in Classes 1–8 (ages 5–13)", | |
| "subjects": [ | |
| "Urdu", "English", "Mathematics", | |
| "Islamiat", "General Knowledge", "Basic Science" | |
| ], | |
| "availability": ["Android", "iOS", "Web"] | |
| } | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run( | |
| "main:app", | |
| host="0.0.0.0", | |
| port=int(os.getenv("PORT", 7860)), | |
| reload=True | |
| ) | |