Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoImageProcessor, AutoModelForImageClassification
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
import googletrans
|
6 |
+
from googletrans import Translator
|
7 |
+
import os
|
8 |
+
from groq import Groq
|
9 |
+
|
10 |
+
# Load Model
|
11 |
+
MODEL_NAME = "linkanjarad/mobilenet_v2_1.0_224-plant-disease-identification"
|
12 |
+
processor = AutoImageProcessor.from_pretrained(MODEL_NAME)
|
13 |
+
model = AutoModelForImageClassification.from_pretrained(MODEL_NAME)
|
14 |
+
|
15 |
+
# Groq API Key (Set in Hugging Face Secrets)
|
16 |
+
GROQ_API_KEY = os.getenv("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")
|
17 |
+
client = Groq(api_key=GROQ_API_KEY)
|
18 |
+
|
19 |
+
# Disease Descriptions
|
20 |
+
disease_info = {
|
21 |
+
"Bacterial Spot": {"cause": "Bacteria (Xanthomonas spp.)", "remedy": "Use copper-based fungicides."},
|
22 |
+
"Leaf Mold": {"cause": "Fungus (Cladosporium fulvum)", "remedy": "Use resistant plant varieties."},
|
23 |
+
"Healthy": {"cause": "No disease detected", "remedy": "Your plant is healthy!"}
|
24 |
+
}
|
25 |
+
|
26 |
+
# Translator
|
27 |
+
translator = Translator()
|
28 |
+
|
29 |
+
# Streamlit UI
|
30 |
+
st.set_page_config(page_title="Plant Disease Detection", page_icon="🌿", layout="wide")
|
31 |
+
|
32 |
+
st.title("🌿 Plant Disease Detection App")
|
33 |
+
st.write("Upload a leaf image to detect diseases and get solutions.")
|
34 |
+
|
35 |
+
# Image Upload
|
36 |
+
uploaded_file = st.file_uploader("📷 Upload a leaf image...", type=["jpg", "png", "jpeg"])
|
37 |
+
|
38 |
+
if uploaded_file:
|
39 |
+
image = Image.open(uploaded_file).convert("RGB")
|
40 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
41 |
+
|
42 |
+
# Predict Disease
|
43 |
+
inputs = processor(images=image, return_tensors="pt")
|
44 |
+
with torch.no_grad():
|
45 |
+
logits = model(**inputs).logits
|
46 |
+
|
47 |
+
predicted_class_idx = logits.argmax(-1).item()
|
48 |
+
predicted_label = model.config.id2label[predicted_class_idx]
|
49 |
+
confidence = torch.nn.functional.softmax(logits, dim=-1)[0][predicted_class_idx].item() * 100
|
50 |
+
|
51 |
+
st.subheader(f"🔍 **Detected Disease:** {predicted_label} ({confidence:.2f}%)")
|
52 |
+
|
53 |
+
# Get Disease Info
|
54 |
+
if predicted_label in disease_info:
|
55 |
+
cause = disease_info[predicted_label]["cause"]
|
56 |
+
remedy = disease_info[predicted_label]["remedy"]
|
57 |
+
else:
|
58 |
+
cause = "Unknown cause."
|
59 |
+
remedy = "Consult an expert."
|
60 |
+
|
61 |
+
# Select Language
|
62 |
+
language = st.selectbox("🌍 Select Language", list(googletrans.LANGUAGES.values()), index=21) # Default: English
|
63 |
+
|
64 |
+
lang_code = list(googletrans.LANGUAGES.keys())[list(googletrans.LANGUAGES.values()).index(language)]
|
65 |
+
|
66 |
+
# Translate Disease Info
|
67 |
+
cause_translated = translator.translate(cause, dest=lang_code).text
|
68 |
+
remedy_translated = translator.translate(remedy, dest=lang_code).text
|
69 |
+
|
70 |
+
st.info(f"🦠 **Cause:** {cause_translated}")
|
71 |
+
st.success(f"💊 **Remedy:** {remedy_translated}")
|
72 |
+
|
73 |
+
# Chatbot
|
74 |
+
st.subheader("💬 Chat with AI about Plant Diseases")
|
75 |
+
user_query = st.text_input("Type your question about the disease:")
|
76 |
+
|
77 |
+
if user_query:
|
78 |
+
response = client.chat.completions.create(
|
79 |
+
messages=[{"role": "user", "content": user_query}],
|
80 |
+
model="llama-3.3-70b-versatile"
|
81 |
+
)
|
82 |
+
chatbot_response = response.choices[0].message.content
|
83 |
+
st.write("🤖 **Chatbot Response:**", chatbot_response)
|