File size: 3,770 Bytes
e2a1a46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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
@app.get("/", response_class=HTMLResponse)
async def read_root(request: Request):
    """Serve the Landing page"""
    return templates.TemplateResponse("index.html", {"request": request})

@app.get("/app", response_class=HTMLResponse)
async def read_app(request: Request):
    """Serve the App page"""
    return templates.TemplateResponse("app.html", {"request": request})

@app.get("/privacy-policy", response_class=HTMLResponse)
async def privacy_policy(request: Request):
    """Serve the Privacy Policy page"""
    return templates.TemplateResponse("privacy_policy.html", {"request": request})

@app.get("/terms-of-service", response_class=HTMLResponse)
async def terms_of_service(request: Request):
    """Serve the Terms of Service page"""
    return templates.TemplateResponse("terms.html", {"request": request})

@app.get("/login")
async def login_page():
    """Redirect Login to Home (Modal)"""
    return RedirectResponse("/")

@app.get("/signup")
async def signup_page():
    """Redirect Signup to Home (Modal)"""
    return RedirectResponse("/")





@app.post("/api/generate-pdf")
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
@app.get("/health")
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
    )