RUVNE commited on
Commit
e48057a
·
1 Parent(s): 451c609

Add application file

Browse files
Files changed (3) hide show
  1. main.py +97 -0
  2. requirements.txt +9 -0
  3. saved_model/multidisease_model.h5 +3 -0
main.py ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ pillow
4
+ numpy
5
+ tensorflow
6
+ transformers
7
+ torch
8
+ python-multipart
9
+ tf-keras
saved_model/multidisease_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8a58caeeab7a7a80b91c0fd594bc6e3d9cf866a2c25db67535bc599379e2f0f4
3
+ size 5174560