Spaces:
Sleeping
Sleeping
Download app.py from Presight-AI/app: direct link, hf CLI and curl.
- Browser
- Download file 3.77 kB
-
https://huggingface.co/spaces/Presight-AI/app/resolve/main/app.py
- Command line
-
hf download hf://spaces/Presight-AI/app/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Presight-AI/app/resolve/main/app.py
3.77 kB
| """ | |
| Main Application Runner for AI Prescription Assistant | |
| FastAPI + Static File Serving for Complete Web App | |
| """ | |
| import os | |
| import sys | |
| import uvicorn | |
| from fastapi import FastAPI | |
| from fastapi.staticfiles import StaticFiles | |
| from fastapi.responses import HTMLResponse, RedirectResponse | |
| from fastapi.templating import Jinja2Templates | |
| from fastapi.requests import Request | |
| # Add backend to path | |
| sys.path.append(os.path.join(os.path.dirname(__file__), 'backend')) | |
| # Import the FastAPI app from backend | |
| from backend.main import app as api_app | |
| # Create main app that serves both API and frontend | |
| app = FastAPI( | |
| title="AI Prescription Assistant", | |
| description="Complete AI-powered prescription analysis web application", | |
| version="1.0.0" | |
| ) | |
| # Mount the API under /api | |
| app.mount("/api", api_app) | |
| # Setup templates | |
| templates = Jinja2Templates(directory="templates") | |
| # Setup static files | |
| app.mount("/static", StaticFiles(directory="static"), name="static") | |
| # Serve the main web application | |
| async def read_root(request: Request): | |
| """Serve the Landing page""" | |
| return templates.TemplateResponse("index.html", {"request": request}) | |
| async def read_app(request: Request): | |
| """Serve the App page""" | |
| return templates.TemplateResponse("app.html", {"request": request}) | |
| async def privacy_policy(request: Request): | |
| """Serve the Privacy Policy page""" | |
| return templates.TemplateResponse("privacy_policy.html", {"request": request}) | |
| async def terms_of_service(request: Request): | |
| """Serve the Terms of Service page""" | |
| return templates.TemplateResponse("terms.html", {"request": request}) | |
| async def login_page(): | |
| """Redirect Login to Home (Modal)""" | |
| return RedirectResponse("/") | |
| async def signup_page(): | |
| """Redirect Signup to Home (Modal)""" | |
| return RedirectResponse("/") | |
| async def generate_pdf(request: Request): | |
| """Generate PDF report from prescription data""" | |
| try: | |
| data = await request.json() | |
| from backend.pdf_generator import PDFGenerator | |
| generator = PDFGenerator() | |
| pdf_buffer = generator.generate_report(data) | |
| return Response( | |
| content=pdf_buffer.getvalue(), | |
| media_type="application/pdf", | |
| headers={ | |
| "Content-Disposition": f"attachment; filename=prescription_report_{datetime.now().strftime('%Y%m%d')}.pdf" | |
| } | |
| ) | |
| except Exception as e: | |
| logger.error(f"PDF generation failed: {e}") | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| # Health check for the main app | |
| async def health_check(): | |
| """Main app health check""" | |
| return { | |
| "status": "healthy", | |
| "app": "AI Prescription Assistant", | |
| "version": "1.0.0", | |
| "components": { | |
| "frontend": "active", | |
| "api": "active" | |
| } | |
| } | |
| if __name__ == "__main__": | |
| # Create necessary directories | |
| os.makedirs("uploads", exist_ok=True) | |
| os.makedirs("outputs", exist_ok=True) | |
| os.makedirs("static/images", exist_ok=True) | |
| port = int(os.environ.get("PORT", 7860)) | |
| print(f"[INFO] Starting on port {port}...") | |
| print(f"[INFO] Frontend: http://localhost:{port}") | |
| print(f"[INFO] API Docs: http://localhost:{port}/api/docs") | |
| print(f"[INFO] ReDoc: http://localhost:{port}/api/redoc") | |
| uvicorn.run( | |
| "app:app", | |
| host="0.0.0.0", | |
| port=port, | |
| reload=True, | |
| log_level="info", | |
| access_log=True | |
| ) |