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