Spaces:
Sleeping
Sleeping
SarowarSaurav
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
import openai
|
4 |
+
import torch
|
5 |
+
from torchvision import transforms
|
6 |
+
from PIL import Image
|
7 |
+
import requests
|
8 |
+
from io import BytesIO
|
9 |
+
|
10 |
+
# Set your OpenAI API key here or use environment variables
|
11 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
12 |
+
|
13 |
+
# Load a pre-trained model for leaf disease detection
|
14 |
+
# For demonstration, we'll use a generic ResNet model fine-tuned for classification
|
15 |
+
# Replace the model path with your specific model trained for leaf diseases
|
16 |
+
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)
|
17 |
+
model.eval()
|
18 |
+
|
19 |
+
# Define image transformations
|
20 |
+
preprocess = transforms.Compose([
|
21 |
+
transforms.Resize(256),
|
22 |
+
transforms.CenterCrop(224),
|
23 |
+
transforms.ToTensor(),
|
24 |
+
transforms.Normalize(
|
25 |
+
mean=[0.485, 0.456, 0.406], # Standard ImageNet means
|
26 |
+
std=[0.229, 0.224, 0.225] # Standard ImageNet stds
|
27 |
+
)
|
28 |
+
])
|
29 |
+
|
30 |
+
# Load class labels (You should replace this with your specific disease classes)
|
31 |
+
# For demonstration, we'll use ImageNet labels
|
32 |
+
LABELS_URL = "https://s3.amazonaws.com/outcome-blog/imagenet/labels.json"
|
33 |
+
response = requests.get(LABELS_URL)
|
34 |
+
labels = {int(key): value for key, value in response.json().items()}
|
35 |
+
|
36 |
+
def detect_disease(image):
|
37 |
+
# Preprocess the image
|
38 |
+
img = preprocess(image).unsqueeze(0) # Add batch dimension
|
39 |
+
|
40 |
+
# Perform inference
|
41 |
+
with torch.no_grad():
|
42 |
+
outputs = model(img)
|
43 |
+
_, predicted = torch.max(outputs, 1)
|
44 |
+
class_id = predicted.item()
|
45 |
+
disease = labels.get(class_id, "Unknown Disease")
|
46 |
+
|
47 |
+
if disease == "Unknown Disease":
|
48 |
+
return disease, "Sorry, the disease could not be identified. Please consult a local agricultural extension office."
|
49 |
+
|
50 |
+
# Generate remedies using OpenAI's ChatGPT
|
51 |
+
prompt = f"The following disease has been detected on a plant leaf: {disease}. Please provide detailed remedies and treatment options for this disease."
|
52 |
+
|
53 |
+
try:
|
54 |
+
completion = openai.ChatCompletion.create(
|
55 |
+
model="gpt-4",
|
56 |
+
messages=[
|
57 |
+
{"role": "system", "content": "You are a helpful agricultural expert."},
|
58 |
+
{"role": "user", "content": prompt}
|
59 |
+
],
|
60 |
+
temperature=0.7,
|
61 |
+
max_tokens=500
|
62 |
+
)
|
63 |
+
remedies = completion.choices[0].message.content.strip()
|
64 |
+
except Exception as e:
|
65 |
+
remedies = "An error occurred while fetching remedies. Please try again later."
|
66 |
+
|
67 |
+
return disease, remedies
|
68 |
+
|
69 |
+
# Define the Gradio interface
|
70 |
+
iface = gr.Interface(
|
71 |
+
fn=detect_disease,
|
72 |
+
inputs=gr.inputs.Image(type="pil", label="Upload Leaf Image"),
|
73 |
+
outputs=[
|
74 |
+
gr.outputs.Textbox(label="Detected Disease"),
|
75 |
+
gr.outputs.Textbox(label="Remedies")
|
76 |
+
],
|
77 |
+
title="Leaf Disease Detector",
|
78 |
+
description="Upload an image of a leaf, and the system will detect the disease and provide remedies."
|
79 |
+
)
|
80 |
+
|
81 |
+
if __name__ == "__main__":
|
82 |
+
iface.launch()
|