Update app.py
Browse files
app.py
CHANGED
@@ -10,8 +10,8 @@ import os
|
|
10 |
import google.generativeai as genai
|
11 |
from vit_model import VisionTransformer
|
12 |
|
13 |
-
# Load class names
|
14 |
-
CLASS_NAMES = ['Pepper__bell___Bacterial_spot', 'Pepper__bell___healthy', 'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy', 'Tomato_Bacterial_spot', 'Tomato_Early_blight', 'Tomato_Late_blight', 'Tomato_Leaf_Mold', 'Tomato_Septoria_leaf_spot', 'Tomato_Spider_mites_Two_spotted_spider_mite', 'Tomato__Target_Spot', 'Tomato__Tomato_YellowLeaf__Curl_Virus', 'Tomato__Tomato_mosaic_virus', 'Tomato_healthy']
|
15 |
|
16 |
# Configure Google Gemini API
|
17 |
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
|
@@ -29,7 +29,7 @@ def preprocess_image(image, target_size=(128, 128)):
|
|
29 |
transform = transforms.Compose([
|
30 |
transforms.Resize(target_size),
|
31 |
transforms.ToTensor(),
|
32 |
-
transforms.Normalize([0.5], [0.5])
|
33 |
])
|
34 |
return transform(image).unsqueeze(0).to(DEVICE)
|
35 |
|
@@ -44,13 +44,15 @@ def vit_inference(image):
|
|
44 |
return CLASS_NAMES[predicted_class], probabilities[predicted_class].item()
|
45 |
|
46 |
# Generate response from Gemini AI
|
47 |
-
def generate_gemini_response(disease_name):
|
48 |
"""Generate a structured diagnosis using Gemini API."""
|
49 |
try:
|
50 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
51 |
prompt = f"""
|
52 |
You are an expert plant pathologist. The detected crop disease is: {disease_name}.
|
53 |
|
|
|
|
|
54 |
Provide a structured analysis including:
|
55 |
- Pathogen details
|
56 |
- Severity level
|
@@ -65,10 +67,11 @@ def generate_gemini_response(disease_name):
|
|
65 |
return f"Error connecting to Gemini API: {str(e)}"
|
66 |
|
67 |
# Initialize Streamlit app
|
68 |
-
st.title("
|
69 |
|
70 |
# Upload image
|
71 |
uploaded_file = st.file_uploader("π€ Upload a plant image", type=["jpg", "jpeg", "png"])
|
|
|
72 |
|
73 |
if uploaded_file:
|
74 |
image = Image.open(uploaded_file).convert("RGB")
|
@@ -78,11 +81,11 @@ if uploaded_file:
|
|
78 |
with st.spinner("Analyzing with Vision Transformer... π"):
|
79 |
predicted_class, confidence = vit_inference(image)
|
80 |
|
81 |
-
st.write(f"
|
82 |
|
83 |
# Connect to Gemini AI for diagnosis
|
84 |
with st.spinner("Generating diagnosis with Gemini AI... π‘"):
|
85 |
-
diagnosis = generate_gemini_response(predicted_class)
|
86 |
st.subheader("π AI Diagnosis")
|
87 |
st.write(diagnosis)
|
88 |
|
@@ -94,5 +97,4 @@ st.markdown("""
|
|
94 |
2. Provide context (optional) about symptoms or concerns.
|
95 |
3. The system detects the disease using AI.
|
96 |
4. Gemini generates a diagnosis with symptoms and treatments.
|
97 |
-
5. Ask follow-up questions, and the AI will remember previous responses.
|
98 |
""")
|
|
|
10 |
import google.generativeai as genai
|
11 |
from vit_model import VisionTransformer
|
12 |
|
13 |
+
# Load class names
|
14 |
+
CLASS_NAMES = ['Pepper__bell___Bacterial_spot', 'Pepper__bell___healthy', 'Potato___Early_blight', 'Potato___Late_blight', 'Potato___healthy', 'Tomato_Bacterial_spot', 'Tomato_Early_blight', 'Tomato_Late_blight', 'Tomato_Leaf_Mold', 'Tomato_Septoria_leaf_spot', 'Tomato_Spider_mites_Two_spotted_spider_mite', 'Tomato__Target_Spot', 'Tomato__Tomato_YellowLeaf__Curl_Virus', 'Tomato__Tomato_mosaic_virus', 'Tomato_healthy']
|
15 |
|
16 |
# Configure Google Gemini API
|
17 |
GEMINI_API_KEY = os.getenv("GOOGLE_API_KEY")
|
|
|
29 |
transform = transforms.Compose([
|
30 |
transforms.Resize(target_size),
|
31 |
transforms.ToTensor(),
|
32 |
+
transforms.Normalize([0.5], [0.5])
|
33 |
])
|
34 |
return transform(image).unsqueeze(0).to(DEVICE)
|
35 |
|
|
|
44 |
return CLASS_NAMES[predicted_class], probabilities[predicted_class].item()
|
45 |
|
46 |
# Generate response from Gemini AI
|
47 |
+
def generate_gemini_response(disease_name, user_context):
|
48 |
"""Generate a structured diagnosis using Gemini API."""
|
49 |
try:
|
50 |
model = genai.GenerativeModel("gemini-1.5-pro")
|
51 |
prompt = f"""
|
52 |
You are an expert plant pathologist. The detected crop disease is: {disease_name}.
|
53 |
|
54 |
+
User-provided context: {user_context}
|
55 |
+
|
56 |
Provide a structured analysis including:
|
57 |
- Pathogen details
|
58 |
- Severity level
|
|
|
67 |
return f"Error connecting to Gemini API: {str(e)}"
|
68 |
|
69 |
# Initialize Streamlit app
|
70 |
+
st.title("AI-Powered Crop Disease Detection with User Context")
|
71 |
|
72 |
# Upload image
|
73 |
uploaded_file = st.file_uploader("π€ Upload a plant image", type=["jpg", "jpeg", "png"])
|
74 |
+
user_context = st.text_area("π Additional Context (Optional)", "Describe symptoms, location, or any observations...")
|
75 |
|
76 |
if uploaded_file:
|
77 |
image = Image.open(uploaded_file).convert("RGB")
|
|
|
81 |
with st.spinner("Analyzing with Vision Transformer... π"):
|
82 |
predicted_class, confidence = vit_inference(image)
|
83 |
|
84 |
+
st.write(f"**Detected Disease:** {predicted_class} (Confidence: {confidence:.2f})")
|
85 |
|
86 |
# Connect to Gemini AI for diagnosis
|
87 |
with st.spinner("Generating diagnosis with Gemini AI... π‘"):
|
88 |
+
diagnosis = generate_gemini_response(predicted_class, user_context)
|
89 |
st.subheader("π AI Diagnosis")
|
90 |
st.write(diagnosis)
|
91 |
|
|
|
97 |
2. Provide context (optional) about symptoms or concerns.
|
98 |
3. The system detects the disease using AI.
|
99 |
4. Gemini generates a diagnosis with symptoms and treatments.
|
|
|
100 |
""")
|