RUVNE commited on
Commit ·
968a979
1
Parent(s): 4418b9b
Add application file
Browse files
main.py
DELETED
|
@@ -1,97 +0,0 @@
|
|
| 1 |
-
from fastapi import FastAPI, UploadFile, File
|
| 2 |
-
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
from PIL import Image
|
| 5 |
-
from PIL import Image
|
| 6 |
-
import numpy as np
|
| 7 |
-
import uvicorn
|
| 8 |
-
from tensorflow.keras.models import load_model
|
| 9 |
-
from transformers import (AutoTokenizer)
|
| 10 |
-
import os
|
| 11 |
-
|
| 12 |
-
# === Inisialisasi FastAPI ===
|
| 13 |
-
app = FastAPI()
|
| 14 |
-
|
| 15 |
-
# === CORS (opsional) ===
|
| 16 |
-
app.add_middleware(
|
| 17 |
-
CORSMiddleware,
|
| 18 |
-
allow_origins=["*"],
|
| 19 |
-
allow_credentials=True,
|
| 20 |
-
allow_methods=["*"],
|
| 21 |
-
allow_headers=["*"],
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
-
# === Load Model Klasifikasi Gambar ===
|
| 25 |
-
model_path = os.path.join(os.path.dirname(__file__), "saved_model", "multidisease_model.h5")
|
| 26 |
-
if not os.path.exists(model_path):
|
| 27 |
-
raise FileNotFoundError(f"Model tidak ditemukan di path: {model_path}")
|
| 28 |
-
image_model = load_model(model_path)
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
# Label (ubah sesuai model Anda)
|
| 32 |
-
label_map = {
|
| 33 |
-
0: "BacterialBlight",
|
| 34 |
-
1: "Blast",
|
| 35 |
-
2: "Brownspot",
|
| 36 |
-
3: "Healthy",
|
| 37 |
-
4: "Leaf_Scald",
|
| 38 |
-
5: "Tungro",
|
| 39 |
-
}
|
| 40 |
-
label_descriptions = {
|
| 41 |
-
"BacterialBlight": "Penyakit akibat bakteri yang menyebabkan bercak air dan layu.",
|
| 42 |
-
"Blast": "Penyakit jamur yang menyerang leher malai dan daun.",
|
| 43 |
-
"Brownspot": "Terdapat bercak coklat bulat di permukaan daun.",
|
| 44 |
-
"Healthy": "Tanaman padi dalam kondisi sehat tanpa gejala penyakit.",
|
| 45 |
-
"Leaf_Scald": "Daun mengering dari ujung dan terbakar karena patogen atau cuaca ekstrem.",
|
| 46 |
-
"Tungro": "Penyakit virus yang membuat daun menguning dan pertumbuhan terhambat.",
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
# === Load Chatbot Pipeline ===
|
| 50 |
-
chatbot = pipeline(
|
| 51 |
-
"text-generation",
|
| 52 |
-
model="ARusDian/AgroLens-Chatbot",
|
| 53 |
-
tokenizer="ARusDian/AgroLens-Chatbot",
|
| 54 |
-
)
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
def preprocess_image(image: Image.Image):
|
| 58 |
-
image = image.resize((224, 224)) # sesuaikan ukuran input model
|
| 59 |
-
img_array = np.array(image) / 255.0 # normalisasi
|
| 60 |
-
img_array = np.expand_dims(img_array, axis=0) # tambahkan batch dimensi
|
| 61 |
-
return img_array
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
# === Endpoint: Klasifikasi Gambar ===
|
| 65 |
-
@app.post("/predict-image")
|
| 66 |
-
async def predict_image(file: UploadFile = File(...)):
|
| 67 |
-
image = Image.open(file.file).convert("RGB")
|
| 68 |
-
input_tensor = preprocess_image(image)
|
| 69 |
-
|
| 70 |
-
pred = np.argmax(image_model.predict(input_tensor), axis=1)[0]
|
| 71 |
-
label = label_map.get(pred, "Tidak dikenal")
|
| 72 |
-
description = label_descriptions.get(label, "-")
|
| 73 |
-
|
| 74 |
-
return {"prediction": label, "description": description}
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
# === Endpoint: Chatbot Deskripsi Penyakit ===
|
| 78 |
-
@app.post("/chatbot")
|
| 79 |
-
async def describe(prompt: dict):
|
| 80 |
-
text = prompt["prompt"]
|
| 81 |
-
tokenizer = AutoTokenizer.from_pretrained("ARusDian/AgroLens-Chatbot")
|
| 82 |
-
result = chatbot(
|
| 83 |
-
text,
|
| 84 |
-
max_new_tokens=120,
|
| 85 |
-
temperature=0.5,
|
| 86 |
-
top_p=0.85,
|
| 87 |
-
repetition_penalty=1.4,
|
| 88 |
-
no_repeat_ngram_size=5,
|
| 89 |
-
do_sample=True,
|
| 90 |
-
eos_token_id=tokenizer.eos_token_id,
|
| 91 |
-
)[0]["generated_text"]
|
| 92 |
-
return {"response": result}
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
# === Run (opsional) ===
|
| 96 |
-
if __name__ == "__main__":
|
| 97 |
-
uvicorn.run("main:app", host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|