generated from kjpou1/ml_project_template
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredict_fasthtml_app.py
More file actions
76 lines (65 loc) · 2.33 KB
/
predict_fasthtml_app.py
File metadata and controls
76 lines (65 loc) · 2.33 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
from fastapi import FastAPI, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
import httpx
import uvicorn
app = FastAPI()
templates = Jinja2Templates(directory="templates")
FASTAPI_REST_URL = "http://127.0.0.1:8008/predict"
@app.get("/", response_class=HTMLResponse)
async def index(request: Request):
"""
Serve the main FastHTML form.
"""
return templates.TemplateResponse("fasthtml_index.html", {"request": request})
@app.post("/submit", response_class=HTMLResponse)
async def submit(
request: Request,
gender: str = Form(...),
ethnicity: str = Form(...),
parental_level_of_education: str = Form(...),
lunch: str = Form(...),
test_preparation_course: str = Form(...),
reading_score: float = Form(...),
writing_score: float = Form(...),
):
"""
Handle form submission and call the FastAPI REST service.
"""
payload = {
"payload": {
"data": {
"gender": gender,
"race_ethnicity": ethnicity,
"parental_level_of_education": parental_level_of_education,
"lunch": lunch,
"test_preparation_course": test_preparation_course,
"reading_score": reading_score,
"writing_score": writing_score,
}
}
}
try:
async with httpx.AsyncClient() as client:
response = await client.post(FASTAPI_REST_URL, json=payload)
response_data = response.json()
if response.status_code == 200:
result = response_data.get("data", {}).get("math_score", "N/A")
return templates.TemplateResponse(
"fasthtml_result.html", {"request": request, "result": result}
)
else:
error_message = response_data.get("message", "Unknown error occurred.")
return templates.TemplateResponse(
"fasthtml_result.html", {"request": request, "error": error_message}
)
except Exception as e:
return templates.TemplateResponse(
"fasthtml_result.html",
{
"request": request,
"error": f"Failed to connect to prediction service: {str(e)}",
},
)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8009)