difarft commited on
Commit
51607f3
·
verified ·
1 Parent(s): 34e106e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile
2
+ from fastapi.responses import JSONResponse
3
+ from transformers import pipeline
4
+ from PIL import Image
5
+ import io
6
+
7
+ # Inisialisasi FastAPI
8
+ app = FastAPI(title="🌿 Plant Disease API (ResNet50)",
9
+ description="API untuk deteksi penyakit tanaman menggunakan ResNet50 dari Hugging Face.",
10
+ version="1.0")
11
+
12
+ # Load model dari Hugging Face
13
+ model = pipeline("image-classification", model="SanketJadhav/PlantDiseaseClassifier-Resnet50")
14
+
15
+ @app.on_event("startup")
16
+ async def load_model():
17
+ print("✅ Model ResNet50 siap digunakan (CPU mode).")
18
+
19
+ @app.get("/")
20
+ async def root():
21
+ return {"message": "🌿 Plant Disease API is running"}
22
+
23
+ @app.post("/predict")
24
+ async def predict(file: UploadFile = File(...)):
25
+ try:
26
+ # Baca file gambar
27
+ image_data = await file.read()
28
+ image = Image.open(io.BytesIO(image_data)).convert("RGB")
29
+
30
+ # Prediksi
31
+ results = model(image)
32
+
33
+ # Ambil 3 prediksi teratas
34
+ top3 = sorted(results, key=lambda x: x['score'], reverse=True)[:3]
35
+
36
+ response = {
37
+ "filename": file.filename,
38
+ "predictions": top3
39
+ }
40
+
41
+ return JSONResponse(content=response)
42
+
43
+ except Exception as e:
44
+ return JSONResponse(content={"error": str(e)}, status_code=500)