Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,27 +1,43 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
-
from fastapi.responses import FileResponse
|
| 3 |
-
from fastapi.responses import JSONResponse
|
| 4 |
import os
|
| 5 |
import uvicorn
|
| 6 |
|
| 7 |
app = FastAPI()
|
| 8 |
|
| 9 |
-
# Directory where model files are stored
|
| 10 |
MODEL_DIRECTORY = "models"
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@app.get("/health")
|
| 13 |
async def api_health_check():
|
| 14 |
return JSONResponse(content={"status": "Service is running"})
|
| 15 |
|
| 16 |
-
@app.get("/
|
| 17 |
-
async def
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
filename = f"model_{plant_name}.tflite"
|
| 19 |
file_path = os.path.join(MODEL_DIRECTORY, filename)
|
| 20 |
|
| 21 |
-
# Check if file exists
|
| 22 |
if not os.path.isfile(file_path):
|
| 23 |
raise HTTPException(status_code=404, detail=f"Model file '{filename}' not found")
|
| 24 |
|
| 25 |
return FileResponse(file_path, filename=filename, media_type="application/octet-stream")
|
|
|
|
| 26 |
if __name__ == "__main__":
|
| 27 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import FileResponse, JSONResponse
|
|
|
|
| 3 |
import os
|
| 4 |
import uvicorn
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
| 8 |
MODEL_DIRECTORY = "models"
|
| 9 |
|
| 10 |
+
# Code-to-plant mapping starting from "000001"
|
| 11 |
+
CODE_TO_PLANT = {
|
| 12 |
+
"000001": "Apple",
|
| 13 |
+
"000002": "Cherry",
|
| 14 |
+
"000003": "Corn",
|
| 15 |
+
"000004": "Grape",
|
| 16 |
+
"000005": "Peach",
|
| 17 |
+
"000006": "Pepperbell",
|
| 18 |
+
"000007": "Potato",
|
| 19 |
+
"000008": "Rice",
|
| 20 |
+
"000009": "Strawberry",
|
| 21 |
+
"000010": "Tomato"
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
@app.get("/health")
|
| 25 |
async def api_health_check():
|
| 26 |
return JSONResponse(content={"status": "Service is running"})
|
| 27 |
|
| 28 |
+
@app.get("/download_by_code/{code}")
|
| 29 |
+
async def download_model_by_code(code: str):
|
| 30 |
+
plant_name = CODE_TO_PLANT.get(code)
|
| 31 |
+
if not plant_name:
|
| 32 |
+
raise HTTPException(status_code=404, detail="Invalid 6-digit code provided")
|
| 33 |
+
|
| 34 |
filename = f"model_{plant_name}.tflite"
|
| 35 |
file_path = os.path.join(MODEL_DIRECTORY, filename)
|
| 36 |
|
|
|
|
| 37 |
if not os.path.isfile(file_path):
|
| 38 |
raise HTTPException(status_code=404, detail=f"Model file '{filename}' not found")
|
| 39 |
|
| 40 |
return FileResponse(file_path, filename=filename, media_type="application/octet-stream")
|
| 41 |
+
|
| 42 |
if __name__ == "__main__":
|
| 43 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|