Dricz commited on
Commit
37f2485
·
verified ·
1 Parent(s): fefe5b0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -30
app.py CHANGED
@@ -1,4 +1,3 @@
1
-
2
  import gradio as gr
3
  import matplotlib.pyplot as plt
4
  from PIL import Image
@@ -6,58 +5,85 @@ from ultralytics import YOLO
6
  import cv2
7
  import numpy as np
8
  import requests
9
- from io import BytesIO
10
  import os
11
 
12
  model = YOLO('Corn-Disease50Epoch.pt')
13
  name = ['Leaf Blight', 'Corn Rust', 'Gray Leaf Spot', 'Healthy']
14
  image_directory = "/home/user/app/images"
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def response2(image, image_size=640, conf_threshold=0.3, iou_threshold=0.6):
17
  results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size)
18
-
19
  text = ""
20
- name_weap = ""
21
  solution = ""
22
 
 
 
23
  for r in results:
24
  im_array = r.plot()
25
- im = Image.fromarray(im_array[..., ::-1])
26
-
27
  for r in results:
28
  conf = np.array(r.boxes.conf.cpu())
29
- cls = np.array(r.boxes.cls.cpu())
30
- cls = cls.astype(int)
31
- xywh = np.array(r.boxes.xywh.cpu())
32
- xywh = xywh.astype(int)
33
-
34
  for con, cl, xy in zip(conf, cls, xywh):
35
- cone = con.astype(float)
36
- conef = round(cone, 3)
37
- conef = conef * 100
38
- text += (f"Detected {name[cl]} with confidence {round(conef, 1)}% at ({xy[0]},{xy[1]})\n")
39
-
40
- if name[cl] == "Corn Rust":
41
- solution = (f"{solution} Apply fungicides with active ingredients like propiconazole or tebuconazole when symptoms appear.\n")
42
- elif name[cl] == "Gray Leaf Spot":
43
- solution = (f"{solution} Use fungicides containing strobilurins (e.g., azoxystrobin) or triazoles.\n")
44
- elif name[cl] == "Leaf Blight":
45
- solution = (f"{solution} Treat with fungicides such as mancozeb or chlorothalonil during the early stages.\n")
46
-
47
- return im, text, solution
48
 
49
  def pil_to_cv2(pil_image):
50
- open_cv_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
51
- return open_cv_image
52
 
53
  def process_video(video_path):
54
  cap = cv2.VideoCapture(video_path)
55
-
56
  while cap.isOpened():
57
  ret, frame = cap.read()
58
  if not ret:
59
  break
60
-
61
  pil_img = Image.fromarray(frame[..., ::-1])
62
  result = model.predict(source=pil_img)
63
  for r in result:
@@ -76,7 +102,7 @@ inputs = [
76
  outputs = [
77
  gr.Image(type="pil", label="Output Image"),
78
  gr.Textbox(label="Result"),
79
- gr.Textbox(label="Solution")
80
  ]
81
 
82
  examples = [
@@ -108,4 +134,5 @@ image_iface = gr.Interface(
108
  demo = gr.TabbedInterface([image_iface, video_iface], ["Image Inference", "Video Inference"])
109
 
110
  if __name__ == '__main__':
111
- demo.launch()
 
 
 
1
  import gradio as gr
2
  import matplotlib.pyplot as plt
3
  from PIL import Image
 
5
  import cv2
6
  import numpy as np
7
  import requests
8
+ import json
9
  import os
10
 
11
  model = YOLO('Corn-Disease50Epoch.pt')
12
  name = ['Leaf Blight', 'Corn Rust', 'Gray Leaf Spot', 'Healthy']
13
  image_directory = "/home/user/app/images"
14
 
15
+ def get_deepseek_solution(disease_name):
16
+ try:
17
+ response = requests.post(
18
+ url="https://openrouter.ai/api/v1/chat/completions",
19
+ headers={
20
+ "Authorization": f"Bearer {API_KEY}",
21
+ "Content-Type": "application/json"
22
+ },
23
+ data=json.dumps({
24
+ "model": "deepseek/deepseek-r1-distill-llama-70b:free",
25
+ "messages": [
26
+ {
27
+ "role": "system",
28
+ "content": "Anda adalah asisten yang hanya dapat memberikan jawaban berdasarkan materi yang diberikan."
29
+ },
30
+ {
31
+ "role": "user",
32
+ "content": f"Apa penyebab dan solusi penyakit jagung '{disease_name}'?"
33
+ }
34
+ ]
35
+ })
36
+ )
37
+
38
+ if response.status_code == 200:
39
+ result = response.json()
40
+ return result.get("choices", [{}])[0].get("message", {}).get("content", "").strip()
41
+ else:
42
+ return "DeepSeek gagal memberikan jawaban (kode error: {}).".format(response.status_code)
43
+
44
+ except requests.exceptions.RequestException as e:
45
+ return f"Gagal terhubung ke DeepSeek: {e}"
46
+
47
  def response2(image, image_size=640, conf_threshold=0.3, iou_threshold=0.6):
48
  results = model.predict(image, conf=conf_threshold, iou=iou_threshold, imgsz=image_size)
49
+
50
  text = ""
 
51
  solution = ""
52
 
53
+ detected_diseases = set()
54
+
55
  for r in results:
56
  im_array = r.plot()
57
+ im = Image.fromarray(im_array[..., ::-1])
 
58
  for r in results:
59
  conf = np.array(r.boxes.conf.cpu())
60
+ cls = np.array(r.boxes.cls.cpu()).astype(int)
61
+ xywh = np.array(r.boxes.xywh.cpu()).astype(int)
62
+
 
 
63
  for con, cl, xy in zip(conf, cls, xywh):
64
+ confidence = round(float(con) * 100, 1)
65
+ text += f"Detected {name[cl]} with confidence {confidence}% at ({xy[0]},{xy[1]})\n"
66
+ detected_diseases.add(name[cl])
67
+
68
+ for disease in detected_diseases:
69
+ if disease != "Healthy":
70
+ solution += f"\n--- {disease} ---\n"
71
+ deepseek_explanation = get_deepseek_solution(disease)
72
+ solution += deepseek_explanation + "\n"
73
+ else:
74
+ solution += "\nTanaman tampak sehat. Tidak ada tindakan diperlukan.\n"
75
+
76
+ return im, text.strip(), solution.strip()
77
 
78
  def pil_to_cv2(pil_image):
79
+ return cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
 
80
 
81
  def process_video(video_path):
82
  cap = cv2.VideoCapture(video_path)
 
83
  while cap.isOpened():
84
  ret, frame = cap.read()
85
  if not ret:
86
  break
 
87
  pil_img = Image.fromarray(frame[..., ::-1])
88
  result = model.predict(source=pil_img)
89
  for r in result:
 
102
  outputs = [
103
  gr.Image(type="pil", label="Output Image"),
104
  gr.Textbox(label="Result"),
105
+ gr.Textbox(label="AI-Powered Solution")
106
  ]
107
 
108
  examples = [
 
134
  demo = gr.TabbedInterface([image_iface, video_iface], ["Image Inference", "Video Inference"])
135
 
136
  if __name__ == '__main__':
137
+ demo.launch()
138
+