File size: 3,397 Bytes
07e8a7a
 
 
 
 
 
 
 
 
 
8e60511
07e8a7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8049e2
1590642
07e8a7a
 
 
 
 
 
 
 
 
a69d7e3
07e8a7a
 
 
 
 
 
 
 
 
 
 
 
 
d324a64
 
07e8a7a
 
fe09394
1590642
fe09394
07e8a7a
 
 
fe09394
1590642
fe09394
07e8a7a
 
fe09394
1590642
fe09394
07e8a7a
 
 
fe09394
1590642
fe09394
07e8a7a
 
c968cf6
a69d7e3
07e8a7a
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from keras.models import load_model, Sequential
from numpy import asarray
import glob
import os
import requests
from bs4 import BeautifulSoup
from bs4.element import SoupStrainer
import bs4
import gradio as gr

model = load_model("./dl.h5")
class_names = ['bacterial-leaf-spot', 'early-blight', 'late-blight', 'mosaic-virus', 'powdery-mildew']

def predict_image(img):
  img_4d=img.reshape((-1,180,180,3))
  prediction=model.predict(img_4d)[0]
  return {class_names[i]: float(prediction[i]) for i in range(5)}


def listToString(s):
    # initialize an empty string
    str1 = ""
    # traverse in the string
    for ele in s:
        str1 = " "+ele + " " + str1+ " " 

    # return string
    return str1

def cause(img):
  te = predict_image(img)
  res = max(te, key=te.get)

    #TREATMENT
  page=requests.get(f"https://www.planetnatural.com/pest-problem-solver/plant-disease/{res}/")
  url=f"https://www.planetnatural.com/pest-problem-solver/plant-disease/{res}/"

  src = page.content
  soup=BeautifulSoup(src, "html.parser")
  details=[]
  for elem in soup.find_all("div",{'class':'post-entry'}):
      ol = elem.find('ol')
      if ol:
        treat=ol.text
        details.append(treat)

  #SUGGESTIONS
  url2 = 'https://google.com/search?q=treatment+' + res
  request_result=requests.get(url2)
  soup = bs4.BeautifulSoup(request_result.text,"html.parser")
  links = soup.find_all("a")
  suggestions=[]
  for link in links:
      link_href = link.get('href')
      if "url?q=" in link_href and not "webcache" in link_href:
        title = link.find_all('h3', limit=3)
        if len(title)> 0:
              suggestions.append(title[0].getText()+" "+link.get('href').split("?q=")[1].split("&sa=U")[0])

              
  if res == "powdery-mildew":
          cause = "The causes are high humidity."
          sug=listToString(suggestions)
          det=listToString(details)
          return(cause,det,sug)
  elif res == "early-blight":
          cause = "the cause is fungus Alternaria solani which survives in plant debris or on infected plants."
          sug=listToString(suggestions)
          return(cause,details,sug)
      
  elif res == "bacterial-leaf-spot":
          print("the causes of", res,
                " is Xanthomonas campestris pv. vesicatoria. It is a gram-negative, rod-shaped bacterium that can survive in seeds and plant debris from one season to another")
          sug=listToString(suggestions)
          return(cause,details,sug)
      
  elif res == "late-blight":
          cause = "the cause is fungus Phytophthora infestans."
          sug=listToString(suggestions)
          return(cause,details,sug)
      
  elif res == "Leaf_mold":
          cause = "the cause is the fungus Passalora fulva."
          # print("the cause of",res,"is the fungus Passalora fulva.")
          sug=listToString(suggestions)
          return(cause,details,sug)
      
  elif res == "mosaic-virus":
          cause = "the cause is that aphid activity in fields is high"
          sug=listToString(suggestions)
          return(cause,details,sug)
  else:
          text = "Undetected disease"
          cause = "unknown"
          treat = "unknown"
          print (text, cause, treat)


image = gr.inputs.Image(shape=(180 , 180))
label = gr.outputs.Textbox()

gr.Interface(fn=cause, inputs=image, outputs=['text','text','text'],interpretation='default').launch(debug='True')