| from fastai.vision.all import * |
| import gradio as gr |
| import os |
| from langchain.agents import load_tools, initialize_agent, AgentType |
| from langchain.llms import OpenAI |
|
|
| title = "CropDoc" |
| description = " Upload a picture of a Soybean plant leaf or drag and drop one of the examples below to the upload box to find out if the soybean plant is suffering from a disease or if it's healthy!\n" |
| with gr.Blocks() as demo: |
| with gr.Tab("Disease identifier"): |
| learn = load_learner('modelbest1.pkl') |
|
|
| categories = ('Brown Spot','Bacterial Blight', 'Bacterial Pustule','Frogeye Leaf Spot', 'Cercospora Leaf Blight','Downy Mildew', 'Healthy Soybean Leaf') |
| def classify_image(img): |
| pred,idx,probs = learn.predict(img) |
| return dict(zip(categories, map(float,probs))) |
|
|
| |
| |
| |
| |
| |
| |
|
|
|
|
| image = gr.inputs.Image(shape=(192, 192)) |
| label = gr.outputs.Label() |
| examples = ['BacterialBlight.jpg','BrownSpot.jpg','DownyMildew.jpeg','FrogeyeLeafSpot.jpg','HealthyLeaf.jpg'] |
| |
|
|
| intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples = examples, title=title, description=description) |
| |
| with gr.Tab("CropDoc Bot"): |
|
|
|
|
| |
| os.environ['OPENAI_API_KEY'] = 'api key for openai gpt 3.5' |
|
|
| |
| llm = OpenAI(temperature=0) |
| tools = load_tools(["wikipedia"]) |
| agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True) |
| |
|
|
| |
| conversation_history = [] |
| |
|
|
| def chatbot(prompt): |
| |
| |
| if prompt: |
| |
| if prompt == 'clear': |
| conversation_history.clear() |
| return "Context history cleared" |
| else: |
| |
| conversation_history.append({"role": "user", "content": prompt}) |
| |
| text = agent.run(conversation_history) |
| |
| conversation_history.append({"role": "bot", "content": text}) |
| return text |
| return "Type a prompt to learn about the plant disease!" |
|
|
|
|
|
|
|
|
| |
| iface = gr.Interface(fn=chatbot, inputs=gr.inputs.Textbox(lines=5, placeholder="Type your prompt here..."), |
| outputs=gr.outputs.Textbox(), title="CropDoc Bot", |
| description="Please update with your GPT Api Key. Ask CropDoc about the disease affecting your crops\n" +"\n"+ "Type 'clear' and press submit to clear the context of the chat\n") |
| |
|
|
|
|
|
|
| |
|
|
| demo.launch() |
|
|