Spaces:
Sleeping
Sleeping
Download app.py from AgroTech-AI/AgroIntel-Leaf-AI: direct link, hf CLI and curl.
- Browser
- Download file 17.2 kB
-
https://huggingface.co/spaces/AgroTech-AI/AgroIntel-Leaf-AI/resolve/main/app.py
- Command line
-
hf download hf://spaces/AgroTech-AI/AgroIntel-Leaf-AI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/AgroTech-AI/AgroIntel-Leaf-AI/resolve/main/app.py
17.2 kB
| import os | |
| import streamlit as st | |
| import tensorflow as tf | |
| from PIL import Image, ImageOps | |
| import numpy as np | |
| from deep_translator import GoogleTranslator | |
| from groq import Groq | |
| from gtts import gTTS | |
| import base64 | |
| from streamlit_mic_recorder import speech_to_text | |
| # --- PAGE CONFIG & THEME --- | |
| st.set_page_config(page_title="AgroIntel", page_icon="🌿", layout="wide") | |
| # Custom CSS for Green & White Theme + Mobile Responsiveness + Dabbe + Drag&Drop | |
| page_bg_css = """ | |
| <style> | |
| [data-testid="stAppViewContainer"] { | |
| background-color: #F9FFF6; /* Very light mint white */ | |
| color: #1B5E20; /* Dark Green Text */ | |
| } | |
| [data-testid="stSidebar"] { | |
| background-color: #E8F5E9; /* Light Green Sidebar */ | |
| } | |
| h1, h2, h3, h4, span, p { | |
| color: #1B5E20 !important; | |
| } | |
| /* Tab Container Adjustments */ | |
| .stTabs [data-baseweb="tab-list"] { | |
| gap: 10px; | |
| overflow-x: auto; | |
| padding-bottom: 5px; | |
| } | |
| /* Default Desktop Tab Styling */ | |
| .stTabs [data-baseweb="tab"] { | |
| height: auto; | |
| min-height: 45px; | |
| white-space: nowrap !important; | |
| background-color: #ffffff; | |
| border-radius: 5px; | |
| box-shadow: 0px 2px 5px rgba(0,0,0,0.1); | |
| padding: 10px 15px; | |
| font-size: 16px; | |
| } | |
| .stTabs [aria-selected="true"] { | |
| background-color: #4CAF50 !important; | |
| color: white !important; | |
| } | |
| /* 🎨 Make Radio Buttons look like clickable Cards (Dabbe) */ | |
| div[role="radiogroup"] { | |
| display: flex; | |
| flex-direction: row; | |
| flex-wrap: wrap; | |
| gap: 15px; | |
| width: 100%; | |
| } | |
| div[role="radiogroup"] > label { | |
| background-color: #ffffff; | |
| border: 2px solid #E8F5E9; | |
| border-radius: 12px; | |
| padding: 12px 15px; | |
| box-shadow: 0px 2px 5px rgba(0,0,0,0.05); | |
| flex: 1; | |
| min-width: 110px; | |
| white-space: nowrap; | |
| justify-content: center; | |
| cursor: pointer; | |
| transition: all 0.3s ease; | |
| margin: 0 !important; | |
| } | |
| div[role="radiogroup"] > label:hover { | |
| border-color: #4CAF50; | |
| background-color: #F1F8E9; | |
| } | |
| /* ☁️ BRUTE-FORCE BADA DABBA */ | |
| div[data-testid="stFileUploader"] section { | |
| border: 2px dashed #4CAF50 !important; | |
| background-color: #FAFAFA !important; | |
| border-radius: 15px !important; | |
| padding: 60px 20px !important; | |
| min-height: 200px !important; | |
| display: flex !important; | |
| flex-direction: column !important; | |
| align-items: center !important; | |
| justify-content: center !important; | |
| } | |
| div[data-testid="stFileUploader"] section:hover { | |
| background-color: #F1F8E9 !important; | |
| border-color: #1B5E20 !important; | |
| } | |
| /* Make the upload icon inside bigger */ | |
| [data-testid="stFileUploadDropzone"] svg { | |
| width: 60px !important; | |
| height: 60px !important; | |
| color: #4CAF50 !important; | |
| } | |
| /* Mobile Specific Styling */ | |
| @media (max-width: 768px) { | |
| .stTabs [data-baseweb="tab"] { | |
| padding: 8px 12px; | |
| font-size: 14px; | |
| min-height: 38px; | |
| } | |
| div[role="radiogroup"] > label { | |
| padding: 10px 5px; | |
| font-size: 14px; | |
| min-width: 80px; | |
| } | |
| } | |
| </style> | |
| """ | |
| st.markdown(page_bg_css, unsafe_allow_html=True) | |
| # --- LOAD MODEL --- | |
| def load_my_model(): | |
| return tf.keras.models.load_model('leaf_disease_model.h5') | |
| model = load_my_model() | |
| # --- DATASETS & LABELS (15 Classes) --- | |
| labels = [ | |
| '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' | |
| ] | |
| remedies_dict = { | |
| 'Pepper__bell___Bacterial_spot': "Remove infected plants immediately. Use copper-based fungicides every week. Avoid watering the leaves to keep the foliage dry.", | |
| 'Pepper__bell___healthy': "Your plant is perfectly healthy! Ensure consistent watering and well-draining soil.", | |
| 'Potato___Early_blight': "Use chlorothalonil fungicides. Rotate your crops yearly and avoid planting potatoes in the same spot.", | |
| 'Potato___Late_blight': "Destroy infected debris immediately. Use systemic fungicides like Ridomil and harvest only in dry weather.", | |
| 'Potato___healthy': "Healthy potato leaf! Monitor water levels and maintain proper spacing for good airflow.", | |
| 'Tomato_Bacterial_spot': "Apply copper sprays immediately. Avoid watering leaves directly to prevent the spread of bacteria. Always disinfect pruning tools.", | |
| 'Tomato_Early_blight': "Improve airflow by pruning bottom leaves. Use organic mulch to prevent soil splashing and apply natural bio-fungicides.", | |
| 'Tomato_Late_blight': "This is highly contagious! Pull out and burn infected plants. Treat nearby healthy plants with protective fungicides.", | |
| 'Tomato_Leaf_Mold': "Increase ventilation around your plants. Keep humidity below 85 percent and avoid wetting the leaves during watering.", | |
| 'Tomato_Septoria_leaf_spot': "Remove the lowest affected leaves. Apply a fungicide spray containing Mancozeb and avoid overhead watering.", | |
| 'Tomato_Spider_mites_Two_spotted_spider_mite': "Use Neem Oil spray directly on the pests. Lightly mist the leaves as mites hate high humidity.", | |
| 'Tomato__Target_Spot': "Keep foliage dry and ensure excellent air circulation. Apply appropriate fungicides weekly and remove older infected leaves.", | |
| 'Tomato__Tomato_YellowLeaf__Curl_Virus': "Use sticky traps for whiteflies which spread the virus. Control weeds and uproot severely stunted plants.", | |
| 'Tomato__Tomato_mosaic_virus': "There is no cure for this virus. Burn the infected plants immediately and disinfect all your gardening tools.", | |
| 'Tomato_healthy': "Your tomato plant is thriving! Continue your current watering schedule and ensure it gets enough sunlight." | |
| } | |
| # --- TRACKER HELPER FUNCTION --- | |
| def calculate_green_ratio(image): | |
| img_array = np.array(image.convert('RGB')) | |
| green_mask = (img_array[:,:,1] > img_array[:,:,0]) & (img_array[:,:,1] > img_array[:,:,2]) | |
| green_ratio = np.sum(green_mask) / (img_array.shape[0] * img_array.shape[1]) * 100 | |
| return round(green_ratio, 2) | |
| # --- HEADER & LANGUAGE SETTINGS --- | |
| st.title("🌿 AgroIntel") | |
| language = st.selectbox("🌐 Select Language", ("English", "Hindi", "Kannada")) | |
| lang_map = {"English": "en", "Hindi": "hi", "Kannada": "kn"} | |
| # --- TABS NAVIGATION --- | |
| tab_home, tab_detect, tab_track, tab_chat = st.tabs([ | |
| "🏠 Home / Guide", | |
| "📸 Disease Detection", | |
| "📈 Recovery Tracker", | |
| "💬 AgroBot" | |
| ]) | |
| # --- TAB 1: HOME / GUIDE --- | |
| with tab_home: | |
| st.subheader("Welcome to AgroIntel") | |
| st.write("An AI-powered agricultural assistant to help farmers diagnose crop diseases and track plant health.") | |
| st.divider() | |
| guide_lang = st.radio("Read 'How to Use' in your language:", ("English", "Hindi", "Kannada"), horizontal=True) | |
| guides = { | |
| "English": "**How to Use AgroIntel:**\n1. Go to **Disease Detection** tab to upload/click a photo.\n2. Wait for AI analysis and translated remedies.\n3. Use **Recovery Tracker** to compare old and new photos.\n4. Ask any farming query in **AgroBot** tab.", | |
| "Hindi": "**AgroIntel का उपयोग कैसे करें:**\n1. फोटो अपलोड/खींचने के लिए **Disease Detection** टैब पर जाएं।\n2. AI विश्लेषण और अनुवादित उपायों का इंतज़ार करें।\n3. पुरानी और नई फोटो की तुलना के लिए **Recovery Tracker** का उपयोग करें।\n4. खेती के किसी भी सवाल के लिए **AgroBot** टैब पर पूछेंं।", | |
| "Kannada": "**AgroIntel ಹೇಗೆ ಬಳಸುವುದು:**\n1. ಫೋಟೋ ಅಪ್ಲೋಡ್ ಮಾಡಲು **Disease Detection** ಟ್ಯಾಬ್ಗೆ ಹೋಗಿ.\n2. AI ವಿಶ್ಲೇಷಣೆ ಮತ್ತು ಪರಿಹಾರಗಳಿಗಾಗಿ ಕಾಯಿರಿ.\n3. ಹಳೆಯ ಮತ್ತು ಹೊಸ ಫೋಟೋ ಹೋಲಿಸಲು **Recovery Tracker** ಬಳಸಿ.\n4. ಕೃಷಿ ಪ್ರಶ್ನೆಗಳಿಗಾಗಿ **AgroBot** ಬಳಸಿ.", | |
| } | |
| st.info(guides[guide_lang]) | |
| # --- TAB 2: DISEASE DETECTION --- | |
| with tab_detect: | |
| col1, col2 = st.columns([1, 1]) | |
| with col1: | |
| st.subheader("Input Image") | |
| # Horizontal setting and emojis make it look like premium cards | |
| source_option = st.radio("Choose Input Source:", ("📁 Upload File", "📸 Camera"), horizontal=True) | |
| if source_option == "📁 Upload File": | |
| file = st.file_uploader("Select an image file", type=["jpg", "png", "jpeg"]) | |
| else: | |
| file = st.camera_input("Take a picture of the leaf") | |
| if file: | |
| img = Image.open(file) | |
| with col1: | |
| st.image(img, use_container_width=True) | |
| with col2: | |
| st.subheader("AI Analysis Results") | |
| with st.spinner("Analyzing cell structure..."): | |
| expected_shape = model.input_shape | |
| target_size = (expected_shape[1], expected_shape[2]) | |
| image_resized = ImageOps.fit(img, target_size, Image.Resampling.LANCZOS).convert('RGB') | |
| img_array = np.asarray(image_resized) / 255.0 | |
| img_reshape = img_array[np.newaxis, ...] | |
| prediction = model.predict(img_reshape) | |
| raw_result = labels[np.argmax(prediction)] | |
| if language == "English": | |
| translated_result = raw_result.replace("_", " ") | |
| translated_remedy = remedies_dict.get(raw_result, "No specific remedy found.") | |
| else: | |
| translated_result = GoogleTranslator(source='auto', target=lang_map[language]).translate(raw_result) | |
| remedy = remedies_dict.get(raw_result, "No specific remedy found.") | |
| try: | |
| translated_remedy = GoogleTranslator(source='auto', target=lang_map[language]).translate(remedy) | |
| if not translated_remedy or translated_remedy.strip() == "": | |
| translated_remedy = remedy | |
| except Exception: | |
| translated_remedy = remedy | |
| st.success(f"### 🦠 {translated_result}") | |
| st.warning(f"**🩺 Remedy / Treatment:** \n\n {translated_remedy}") | |
| # --- TAB 3: RECOVERY TRACKER --- | |
| with tab_track: | |
| st.subheader("📈 Plant Health & Recovery Tracker") | |
| st.write("Upload or capture photos to analyze plant healing metrics over time.") | |
| track_col1, track_col2 = st.columns(2) | |
| with track_col1: | |
| st.write("**📅 Day 1 (Infected) Image**") | |
| day1_source = st.radio("Source for Day 1:", ("📁 Upload File", "📸 Camera"), key="d1_radio", horizontal=True) | |
| if day1_source == "📁 Upload File": | |
| day1_file = st.file_uploader("Select Day 1 Image", type=["jpg", "png", "jpeg"], key="d1_up") | |
| else: | |
| day1_file = st.camera_input("Take Day 1 Picture", key="d1_cam") | |
| with track_col2: | |
| st.write("**🌿 Current (Recent) Image**") | |
| current_source = st.radio("Source for Current Day:", ("📁 Upload File", "📸 Camera"), key="cur_radio", horizontal=True) | |
| if current_source == "📁 Upload File": | |
| current_file = st.file_uploader("Select Current Image", type=["jpg", "png", "jpeg"], key="cur_up") | |
| else: | |
| current_file = st.camera_input("Take Current Picture", key="cur_cam") | |
| if day1_file and current_file: | |
| img_day1 = Image.open(day1_file) | |
| img_current = Image.open(current_file) | |
| st.divider() | |
| st.write("### 🔍 Visual Comparison") | |
| show_col1, show_col2 = st.columns(2) | |
| with show_col1: | |
| st.image(img_day1, caption="Day 1", use_container_width=True) | |
| day1_green = calculate_green_ratio(img_day1) | |
| st.metric(label="Healthy Green Area (Day 1)", value=f"{day1_green}%") | |
| with show_col2: | |
| st.image(img_current, caption="Current Day", use_container_width=True) | |
| current_green = calculate_green_ratio(img_current) | |
| diff = round(current_green - day1_green, 2) | |
| st.metric(label="Healthy Green Area (Current)", value=f"{current_green}%", delta=f"{diff}%") | |
| if diff > 0: | |
| st.success("✅ The plant shows signs of recovery! The healthy green surface area has increased.") | |
| else: | |
| st.error("⚠️ The infection might still be spreading or unchanged. Check AgroBot for remedies.") | |
| # --- TAB 4: CHATBOT ASSISTANT (Final & Bulletproof) --- | |
| with tab_chat: | |
| st.subheader(f"💬 AgroBot (Responding in {language})") | |
| st.write("Discuss plant health, get farming tips, or ask about specific diseases.") | |
| st.divider() | |
| # 1. Initialize Chat History | |
| if "messages" not in st.session_state: | |
| st.session_state.messages = [] | |
| # 2. Display Chat History | |
| for message in st.session_state.messages: | |
| with st.chat_message(message["role"], avatar="🌿" if message["role"] == "assistant" else "👤"): | |
| st.markdown(message["content"]) | |
| # 3. Handle Voice Input | |
| audio_query = None | |
| with st.expander("🎤 Use Voice Input (English/Hindi/Kannada)", expanded=False): | |
| # We use speech_to_text to convert voice directly into words! | |
| spoken_text = speech_to_text( | |
| language=lang_map[language], | |
| start_prompt="🎤 Click to Speak", | |
| stop_prompt="🛑 Stop & Send", | |
| key='voice_input' | |
| ) | |
| if spoken_text: | |
| audio_query = spoken_text | |
| st.success(f"Recognized Voice: {audio_query}") | |
| # 4. Handle Text Input | |
| user_prompt = st.chat_input("Type your farming question here...") | |
| final_prompt = user_prompt or audio_query | |
| # 5. Process Input & Generate Response | |
| if final_prompt: | |
| # Display user message | |
| st.session_state.messages.append({"role": "user", "content": final_prompt}) | |
| with st.chat_message("user", avatar="👤"): | |
| st.markdown(final_prompt) | |
| api_key = os.environ.get("GROQ_API_KEY") | |
| if not api_key: | |
| st.warning("⚠️ GROQ_API_KEY is not set in the environment variables.") | |
| else: | |
| client = Groq(api_key=api_key) | |
| with st.spinner("AgroBot is thinking and generating voice..."): | |
| system_prompt = f"You are an expert farmer's assistant. Answer ONLY in {language} language. Keep the answer to 2-3 short, actionable sentences." | |
| try: | |
| # Get AI Text Response | |
| response = client.chat.completions.create( | |
| messages=[{"role": "system", "content": system_prompt}, {"role": "user", "content": final_prompt}], | |
| model="llama-3.1-8b-instant", | |
| ) | |
| ans = response.choices[0].message.content | |
| # Display AI Text | |
| st.session_state.messages.append({"role": "assistant", "content": ans}) | |
| with st.chat_message("assistant", avatar="🌿"): | |
| st.markdown(ans) | |
| # Generate & Play AI Voice (Fail-Safe) | |
| if lang_map[language] in ["en", "hi", "kn"]: | |
| try: | |
| # Truncate text for audio if it's too long (saves API calls and prevents 429 error) | |
| audio_text = ans[:500] if len(ans) > 500 else ans | |
| tts = gTTS(text=audio_text, lang=lang_map[language]) | |
| tts.save("bot_voice.mp3") | |
| # Autoplay is True so the bot speaks automatically! | |
| st.audio("bot_voice.mp3", format="audio/mp3", autoplay=True) | |
| except Exception: | |
| # Silently ignore TTS 429 errors, keep UI clean | |
| pass | |
| except Exception as e: | |
| st.error(f"Error generating text response: {e}") |