File size: 1,532 Bytes
a5de9f5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fasthtml_hf import setup_hf_backup
from fasthtml import FastHTML
from monsterui.core import Theme
from fasthtml.common import *
import os, uvicorn
from starlette.responses import FileResponse
from starlette.datastructures import UploadFile
from fastai.vision.all import *


theme = Theme.blue
app, rt = fast_app(hdrs=theme.headers())

os.makedirs("uploads", exist_ok=True)

def classify(image_path): 
    im = PILImage.create(image_path)
    learn = load_learner("model.pkl")
    cls,idx,probs = learn.predict(im)
    return cls,probs[idx]
 

@app.get("/")
def home():
    return Title("German Bread Classification"), Main(
        H1("German Bread Classification App"),
        Form(
            Input(type="file", name="image", accept="image/*", required=True),
            Button("Classify"),
            enctype="multipart/form-data",
            hx_post="/classify",
            hx_target="#result"
        ),
        Br(), Div(id="result"),
        cls="container"
    )

@app.post("/classify")
async def handle_classify(image:UploadFile):
    
    image_path = f"uploads/{image.filename}"
    with open(image_path, "wb") as f:
        f.write(await image.read())
    
    result = classify(image_path)
    
    return Div(
        P(f"Classification result: {result}"),
        Img(src=f"/uploads/{image.filename}", alt="Uploaded image", style="max-width: 300px;")
    )

@app.get("/uploads/{filename}")
async def serve_upload(filename: str):
    return FileResponse(f"uploads/{filename}")

setup_hf_backup(app)
serve()