""" PaddockAI โ€” Livestock & Pasture Assessment Tool Snap a photo on your phone โ†’ get instant ML-powered analysis. Uses Qwen3-VL-8B vision-language model via HF Inference API. """ import os import io import re import base64 import datetime import gradio as gr from PIL import Image from huggingface_hub import InferenceClient # --------------------------------------------------------------------------- # Model & provider config โ€” tries multiple providers for resilience # --------------------------------------------------------------------------- MODEL_ID = "Qwen/Qwen3-VL-8B-Instruct" PROVIDERS = ["novita", "together", "auto"] def call_vlm(messages: list, max_tokens: int = 1024) -> str: """Call VLM with automatic provider failover.""" token = os.environ.get("HF_TOKEN") last_error = None for provider in PROVIDERS: try: client = InferenceClient(provider=provider, api_key=token) response = client.chat.completions.create( model=MODEL_ID, messages=messages, max_tokens=max_tokens, temperature=0.3, ) result = response.choices[0].message.content # Strip chain-of-thought blocks from Qwen3 if "" in result: result = re.sub(r".*?", "", result, flags=re.DOTALL).strip() return result except Exception as e: last_error = e continue raise last_error or RuntimeError("All providers failed") # --------------------------------------------------------------------------- # Expert agricultural prompts # --------------------------------------------------------------------------- SYSTEM_PROMPT = ( "You are PaddockAI, an expert livestock and pasture assessment assistant " "for farmers, feedlotters, and graziers worldwide.\n\n" "Rules:\n" "- Be specific: give numbers, scores, and concrete recommendations.\n" "- Use industry-standard scoring systems (BCS 1-5, pasture condition 1-5).\n" "- If you cannot determine something from the image, say so honestly.\n" "- Format with markdown headers and bullet points for phone readability.\n" "- Keep responses practical โ€” these people are in the paddock, not at a desk." ) TASK_PROMPTS = { "๐Ÿ„ Cattle Health Check": """Perform a thorough visual health assessment of the cattle in this image. **Assess and report on:** 1. **Body Condition Score (BCS)** โ€” 1โ€“5 scale (1=emaciated, 3=good, 5=obese). Note fat cover over ribs, spine, hips, tailhead. 2. **Coat & Skin** โ€” Condition, shine, lesions, lumps, hair loss, external parasites (lice, ticks, mange). 3. **Eyes & Nose** โ€” Discharge, swelling, cloudiness, pinkeye/IBR signs. 4. **Gait & Posture** โ€” Lameness, stiffness, favouring a leg, abnormal stance. 5. **Respiratory** โ€” Laboured breathing, nasal discharge, coughing posture. 6. **General Demeanour** โ€” Alert vs dull, separated from mob, ear position. **Provide:** - Overall health rating: โœ… Healthy / โš ๏ธ Monitor / ๐Ÿšจ Needs Attention - Specific concerns identified - Recommended actions (draft for inspection, call vet, treat for X) If multiple animals are visible, assess the mob's general condition and flag any individuals of concern.""", "๐ŸŒฟ Pasture Condition": """Assess the pasture/paddock condition shown in this image. **Evaluate:** 1. **Ground Cover (%)** โ€” Vegetation vs bare soil. 2. **Feed on Offer (FOO)** โ€” Estimate kg DM/ha. <500 very low, 500-1000 low, 1000-2000 moderate, 2000-3000 good, 3000+ high. 3. **Pasture Composition** โ€” Grass species (phalaris, ryegrass, natives, clover, lucerne). Desirable vs undesirable ratio. 4. **Weed Burden** โ€” Identify weeds, estimate severity (low/moderate/high). 5. **Pasture Quality** โ€” Green vs dry/hayed-off, growth stage (vegetative, reproductive, senescent). 6. **Grazing Pressure** โ€” Over-grazing signs (short, bare patches, pugging) or under-grazing (rank, gone to seed). 7. **Soil Condition** โ€” Erosion, waterlogging, compaction, pugging damage. **Provide:** - Pasture condition score: 1 (very poor) to 5 (excellent) - Estimated carrying capacity (DSE/ha or head/ha) - Top 3 management recommendations""", "๐Ÿ“Š Body Condition Score": """Perform a detailed Body Condition Score (BCS) assessment. **1โ€“5 scale (half scores OK):** | Score | Description | |-------|-------------| | 1.0 | Emaciated โ€” bones prominent, no fat cover | | 2.0 | Thin โ€” some bone visible, slight cover | | 3.0 | Good store โ€” ribs felt with firm pressure | | 4.0 | Fat โ€” ribs cannot be felt, fat deposits visible | | 5.0 | Obese โ€” heavy fat deposits, mobility affected | **For each animal, describe:** - Fat cover: spine, ribs, hips/hooks, tailhead, brisket - Muscle development: loin, rump, shoulder - Breed type if identifiable - BCS score with justification **Then provide:** - Target BCS for the likely class (breeder, feeder, store) - Whether current BCS is on track or needs action - Feed/management recommendations to reach target""", "๐Ÿ”ข Count Stock": """Count the livestock visible in this image as accurately as possible. **Report:** 1. **Total count** โ€” Exact number or confident range (e.g., 45-50 head). 2. **Method** โ€” How you counted (sections, rows, clusters). 3. **Confidence** โ€” High/Medium/Low. 4. **Species & class** โ€” Cattle (cows, calves, steers, bulls), sheep, goats, etc. 5. **Mob density** โ€” Too tight (welfare concern) or well-spaced. 6. **Separated animals** โ€” Any away from mob (sick, calving, or needing check). For drone/aerial shots, estimate area covered and density.""", "๐Ÿฅ Injury / Lameness": """Assess this image for signs of injury, lameness, or physical ailment. **Look for:** 1. **Lameness** โ€” Favouring a leg, head bob, shortened stride, reluctance to bear weight, swollen joints. 2. **Injuries** โ€” Cuts, wounds, swelling, abscesses, broken horns, eye injuries, prolapse. 3. **Skin conditions** โ€” Ringworm, warts, lumps, cancer eye, sunburn, photosensitisation. 4. **Structural** โ€” Poor feet, overgrown hooves, down animal, difficulty rising. 5. **Dystocia signs** โ€” If calving/lambing: progress, presentation, time concerns. **Provide:** - Urgency: ๐ŸŸข Minor / ๐ŸŸก Moderate โ€” monitor closely / ๐Ÿ”ด Urgent โ€” immediate attention - Likely cause or differentials - Recommended immediate action - Whether a vet call is warranted โš ๏ธ **Visual assessment only โ€” not a veterinary diagnosis. Always consult your vet for serious concerns.**""", "๐ŸŒพ General / Other": """Provide a comprehensive agricultural assessment of this image. Describe what you see: livestock, pasture, infrastructure, landscape, weather conditions. Identify anything noteworthy: potential issues, good management practices, areas for improvement. Give practical recommendations.""", } # --------------------------------------------------------------------------- # Image processing # --------------------------------------------------------------------------- def image_to_base64(image: Image.Image, max_size: int = 1280) -> str: """Convert PIL Image to base64 JPEG, resizing if needed.""" if image.mode == "RGBA": image = image.convert("RGB") w, h = image.size if max(w, h) > max_size: ratio = max_size / max(w, h) image = image.resize((int(w * ratio), int(h * ratio)), Image.LANCZOS) buf = io.BytesIO() image.save(buf, format="JPEG", quality=85) return base64.b64encode(buf.getvalue()).decode("utf-8") # --------------------------------------------------------------------------- # Core analysis function # --------------------------------------------------------------------------- def analyze(image, task: str, custom_notes: str, history: list): """Run VLM analysis on the uploaded image.""" if image is None: return "๐Ÿ“ท **Please upload or take a photo first.**", history prompt = TASK_PROMPTS[task] if custom_notes and custom_notes.strip(): prompt += f"\n\n**Additional context from the user:** {custom_notes.strip()}" b64 = image_to_base64(image) try: messages = [ {"role": "system", "content": SYSTEM_PROMPT}, { "role": "user", "content": [ {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{b64}"}}, {"type": "text", "text": prompt}, ], }, ] result = call_vlm(messages) # Add to history timestamp = datetime.datetime.now().strftime("%H:%M:%S") entry = f"### {task} โ€” {timestamp}\n\n{result}\n\n---\n" history = history or [] history.insert(0, entry) return result, history except Exception as e: error_msg = str(e) if "rate" in error_msg.lower() or "limit" in error_msg.lower(): msg = "โณ **Rate limit reached.** Please wait a moment and try again." elif "401" in error_msg or "auth" in error_msg.lower(): msg = "๐Ÿ”‘ **Authentication error.** The service is temporarily unavailable." else: msg = f"โŒ **Analysis failed:** {error_msg}\n\nPlease try again." return msg, history def format_history(history: list) -> str: """Format assessment history for display.""" if not history: return "*No assessments yet. Upload a photo to get started.*" return "\n".join(history[:10]) # Keep last 10 # --------------------------------------------------------------------------- # UI # --------------------------------------------------------------------------- HEADER_MD = """ # ๐Ÿ„ PaddockAI ### Instant livestock & pasture assessment โ€” right from your phone **How to use:** Upload or snap a photo โ†’ Pick an assessment type โ†’ Tap **Analyze** > โš ๏ธ Not a substitute for veterinary advice. For health concerns, always consult your vet. """ CSS = """ /* Big touch targets for use in the paddock */ .primary-btn { min-height: 56px !important; font-size: 18px !important; font-weight: 600 !important; } .task-radio label { padding: 10px 14px !important; margin: 3px 0 !important; border-radius: 8px !important; } .output-box { font-size: 15px !important; line-height: 1.65 !important; } @media (max-width: 768px) { .primary-btn { min-height: 64px !important; font-size: 20px !important; } .contain { padding: 6px !important; } } """ THEME = gr.themes.Soft( primary_hue="green", secondary_hue="emerald", neutral_hue="stone", ) def build_ui(): with gr.Blocks() as demo: # State for assessment history history_state = gr.State([]) gr.Markdown(HEADER_MD) with gr.Tabs(): # โ”€โ”€ Tab 1: Assess โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ with gr.Tab("๐Ÿ“ท Assess", id="assess"): with gr.Row(equal_height=False): with gr.Column(scale=1, min_width=300): image_input = gr.Image( type="pil", sources=["upload", "webcam", "clipboard"], label="๐Ÿ“ท Photo", height=350, ) task_selector = gr.Radio( choices=list(TASK_PROMPTS.keys()), value="๐Ÿ„ Cattle Health Check", label="Assessment Type", elem_classes=["task-radio"], ) custom_notes = gr.Textbox( label="Notes (optional)", placeholder="e.g., 'Heifer off feed 2 days' or 'Paddock rested 3 weeks'", lines=2, ) analyze_btn = gr.Button( "๐Ÿ” Analyze", variant="primary", size="lg", elem_classes=["primary-btn"], ) with gr.Column(scale=1, min_width=300): output = gr.Markdown( value="*Upload a photo and tap Analyze to get started.*", elem_classes=["output-box"], ) # โ”€โ”€ Tab 2: History โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ with gr.Tab("๐Ÿ“‹ History", id="history"): history_display = gr.Markdown( value="*No assessments yet.*", elem_classes=["output-box"], ) # โ”€โ”€ Tab 3: Tips โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ with gr.Tab("๐Ÿ’ก Tips", id="tips"): gr.Markdown(""" ## ๐Ÿ“ธ Getting the best results ### Cattle photos - **Side-on** is best for body condition scoring - Get close enough to see **eyes and nose** for health checks - **Multiple animals?** Try to get them all clearly visible - For mob assessments, step back for a wider shot ### Pasture / paddock - Include some **sky or horizon** for scale reference - Shoot at **slight downward angle** to show ground cover - Multiple angles from different spots give better estimates ### Counting stock - **Elevated positions** (hill, vehicle roof, drone) work best - Try to capture the **whole mob** in one frame - Mark the image mentally in sections for counting ### Injury / lameness - Get as **close and clear** as safely possible - Photograph from **multiple angles** - Include a **size reference** if possible (hand, boot, etc.) ### General tips - โ˜€๏ธ **Natural daylight** gives the best results - ๐Ÿ“ฑ Clean your phone lens! - ๐Ÿ”„ If the first result seems off, try a different angle or assessment type """) # Footer gr.Markdown( "---\n" "*Powered by [Qwen3-VL-8B](https://huggingface.co/Qwen/Qwen3-VL-8B-Instruct) " "via [HF Inference API](https://huggingface.co/docs/inference-providers) ยท " "Built for farmers, by farmers*" ) # โ”€โ”€ Wiring โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ analyze_btn.click( fn=analyze, inputs=[image_input, task_selector, custom_notes, history_state], outputs=[output, history_state], show_progress="minimal", ).then( fn=format_history, inputs=[history_state], outputs=[history_display], ) return demo # --------------------------------------------------------------------------- # Launch # --------------------------------------------------------------------------- if __name__ == "__main__": demo = build_ui() demo.launch( server_name="0.0.0.0", server_port=7860, theme=THEME, css=CSS, )