paddockai / app.py
Vandersaag's picture
Update app.py
a7559f5 verified
Raw History Blame Contribute Delete
15.3 kB
"""
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 <think> blocks from Qwen3
if "<think>" in result:
result = re.sub(r"<think>.*?</think>", "", 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,
)