import gradio as gr from keras.models import load_model, Sequential from PIL import Image from numpy import asarray import glob import os import requests import cv2 from bs4 import BeautifulSoup from bs4.element import SoupStrainer import bs4 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): str1 = "" for ele in s: str1 = " "+ele + " " + str1+ " " 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, "lxml") details=[url,] 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()) #suggestions.append(link.get('href').split("?q=")[1].split("&sa=U")[0]) suggestions.append(title[0].getText()+" "+link.get('href').split("?q=")[1].split("&sa=U")[0]) if res == "powdery-mildew": cause = "The causes of POWDERY MILDEW are high humidity." sug=listToString(suggestions) return(cause,treat,sug) elif res == "early-blight": cause = "the cause is fungus Alternaria solani which survives in plant debris or on infected plants." print(cause) 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") elif res == "late-blight": cause = "the cause is fungus Phytophthora infestans." elif res == "Leaf_mold": cause = "the cause is the fungus Passalora fulva." # print("the cause of",res,"is the fungus Passalora fulva.") elif res == "mosaic-virus": cause = "the cause is that aphid activity in fields is high" 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')