testi / app.py
testingcap's picture
Update app.py
f8049e2
Raw History Blame Contribute Delete
3.4 kB
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')