File size: 1,662 Bytes
6f556e4
ba6105b
6f556e4
 
 
 
 
 
 
ba6105b
 
 
 
 
 
 
 
 
 
 
 
 
 
6f556e4
 
 
30b1363
 
 
 
 
 
 
 
 
 
 
6f556e4
ba6105b
 
 
 
 
 
0054547
6f556e4
 
 
 
 
 
ba6105b
6f556e4
 
30b1363
 
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, JSONResponse
import os
import uvicorn

app = FastAPI()

MODEL_DIRECTORY = "models"

# Code-to-plant mapping starting from "000001"
CODE_TO_PLANT = {
    "000001": "Apple",
    "000002": "Cherry",
    "000003": "Corn",
    "000004": "Grape",
    "000005": "Peach",
    "000006": "Pepperbell",
    "000007": "Potato",
    "000008": "Rice",
    "000009": "Strawberry",
    "000010": "Tomato"
}

@app.get("/health")
async def api_health_check():
    return JSONResponse(content={"status": "Service is running"})
    
@app.get("/download/{plant_name}")
async def download_model(plant_name: str):
    filename = f"model_{plant_name}.tflite"
    file_path = os.path.join(MODEL_DIRECTORY, filename)

    # Check if file exists
    if not os.path.isfile(file_path):
        raise HTTPException(status_code=404, detail=f"Model file '{filename}' not found")

    return FileResponse(file_path, filename=filename, media_type="application/octet-stream")

@app.get("/download_by_code/{code}")
async def download_model_by_code(code: str):
    plant_name = CODE_TO_PLANT.get(code)
    if not plant_name:
        raise HTTPException(status_code=404, detail="Invalid 6-digit code provided")

    filename = f"model_{plant_name}.tflite"
    file_path = os.path.join(MODEL_DIRECTORY, filename)

    if not os.path.isfile(file_path):
        raise HTTPException(status_code=404, detail=f"Model file '{filename}' not found")

    return FileResponse(file_path, filename=filename, media_type="application/octet-stream")

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)