Spaces:
Runtime error
Runtime error
Fix health check: reuse the advisor's LLM client instead of a separate VL model
Browse filesThe configured HEALTH_MODEL_ID (Qwen2.5-VL-7B-Instruct, provider auto) was
not enabled for this account and failed with a model_not_supported error.
The health diagnostic now shares the advisor's InferenceClient.
Files changed (2)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323# Guarden β Technical DocumentationThis document describes the architecture, data model, and machine-learningcomponents behind **Guarden**, a Gradio application that helpsusers identify plants, track a virtual garden, and receive weather-awarewatering, care recommendations, health checks...---## 1. High-level architectureGuarden is a **single-process Gradio app** (`app.py`, ~950 lines) backed by asmall set of pure-Python modules. There is no database server: each user getsa private, file-based "garden" stored on disk, and three external AI/MLmodels are called on demand via Hugging Face.```ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ Browser (Gradio UI) ββ Location gate β Garden board (drag & drop) β Sidebar (watering / ββ forecast / assistant) β Add-plant drawer ββββββββββββββββββ¬ββββββββββββββββββββββββββββββββββββββββββββββββββββ-ββ Gradio Blocks events (click / change / .then chains)βββββββββββββββββΌββββββββββββββββββββββββββββββββββββββββββββββββββββ-ββ app.py ββ β’ Per-user routing (BrowserState user_id β user_data/<uuid>/) ββ β’ Garden CRUD (load/save garden.json, photos, background, links) ββ β’ Board rendering (HTML + SVG overlay + JS drag/drop bridge) ββ β’ Orchestrates calls into modules/* and external APIs ββββββ¬ββββββββββββββ¬ββββββββββββββββ¬ββββββββββββββββ¬βββββββββββββββββββββ β β ββΌ βΌ βΌ βΌmodules/ modules/ modules/ modules/classifier.py recommender.py watering.py advisor.py(SigLIP ML plant.py weather_utils.py (HF Inference:image (CSV lookup) (Open-Meteo) chat LLM +classifier) vision LLM)ββΌutils/geo.py (Open-Meteogeocoding)```### Per-user data layout```user_data/βββ <uuid>/ # one folder per browser, via gr.BrowserStateβββ garden.json # list of plant dicts (see Β§3)βββ background.jpg # optional custom board backgroundβββ plant_photos/βββ <plant_id>.jpg # uploaded photo for each plant```A `user_id` (UUID4) is generated on first visit and persisted in thebrowser's local storage via `gr.BrowserState`, so the same browser alwaysmaps back to the same `user_data/<uuid>/` folder β no login/auth required.---## 2. Tech stack| Layer | Technology ||---|---|| UI / app framework | **Gradio 6.18** (Blocks API), custom CSS theme (`static/style.css`), small vanilla-JS bridge for the drag-and-drop board (`BOARD_JS` in `app.py`) || Plant genus classification | Fine-tuned **SigLIP** vision transformer (`transformers`, local inference, CPU/GPU) || Gardening chat advisor | LLM via **Hugging Face Inference Providers** (`huggingface_hub.InferenceClient`) || Photo health diagnostic | Vision-language model via **Hugging Face Inference Providers** (multimodal chat) || Care metadata | CSV lookup table (`data/growth_csv/growth_ds.csv`), `pandas` || Weather & geocoding | **Open-Meteo** REST APIs (forecast, archive, geocoding) β no API key needed || Persistence | Flat files: `garden.json` (JSON) + JPEG photos, per user, on local disk || Sprites | Procedurally generated pixel-art PNGs (`modules/pixel_art.py`, pure PIL, no ML) |Runtime dependencies are pinned in `requirements.txt`. Training-onlydependencies (`datasets`, `accelerate`, `torchvision`) live alongside theinference deps because the classifier's training script ships in the samerepo (see Β§4.1).---## 3. Data modelEach plant in `garden.json` is a dict with the following fields (collectedfrom how `app.py` reads/writes them):```jsonc{"id": "20260610_211240_094893", // timestamp-based unique id"nickname": "Living Room Ficus", // user-given name"photo": "user_data/<uid>/plant_photos/<id>.jpg","genus": "Ficus", // predicted by the classifier"confidence": 92.4, // classifier confidence, %"added": "2026-06-10","last_watered": "2026-06-12", // ISO date or null"watering_history": ["2026-06-01", "2026-06-12"], // append-only log"rained": false, // true if last_watered was inferred from rain"watering_frequency_days": "Regular watering", // raw CSV text"sunlight": "full sunlight","soil": "sandy","fertilization_type": "Balanced","notes": "Ficus needs full sunlight. It thrives in sandy soil. ...","position": { "x": 30.0, "y": 40.0 }, // % position on the garden board"neighbors": ["<other plant id>"], // hand-drawn "neighbor" links"health": "Healthy β leaves look ..." // last VLM health diagnosis, if any}```This structure is the single source of truth: the board, the detail card,the watering table, the advisor and the health diagnostic all read/writethis same list of dicts via `load_garden(user_id)` / `save_garden(...)`.---## 4. Machine-learning componentsGuarden uses **three** distinct AI models, each chosen for a different job:a small fine-tuned **vision classifier** for genus recognition (fast, local,deterministic), and two **Hugging Face Inference**-hosted generative modelsfor natural-language and vision-language reasoning (advisor + health check).### 4.1 Plant genus classifier (`modules/classifier.py`)**Task**: given a photo of a plant, predict its **botanical genus** (e.g.`Ficus`, `Aloe`, `Begonia`) out of **289 genus classes**.**Model**: a fine-tuned [`google/siglip-base-patch16-224`](https://huggingface.co/google/siglip-base-patch16-224)(SigLIP β a CLIP-style vision transformer, ViT-B/16, 224Γ224 input, 768-dhidden size) with a `SiglipForImageClassification` head (289-way softmax).The fine-tuned weights are pushed to a private HF Hub repo(`Crocolil/HackatonSmall-storage`) and exported as a clean`config.json` / `model.safetensors` / `preprocessor_config.json` bundle(~372 MB) under `training/clean_export/`.**Training pipeline** (`training/train_classifier.py`):- Loads a `datasets.DatasetDict` (train/test split) of labelled plantphotos, with one `ClassLabel` per genus (`data/hf_plant_dataset/`).- Builds `id2label` / `label2id` from the dataset's `ClassLabel` feature.- Data augmentation (train split): `RandomResizedCrop(scale=0.8β1.0)`,`RandomHorizontalFlip`, `ColorJitter(brightness/contrast/saturation=0.1)`,then `ToTensor` + SigLIP's own image-mean/std normalization.- Eval split: deterministic `Resize` + `CenterCrop` + normalize.- Fine-tuned end-to-end with π€ `Trainer` / `TrainingArguments`:- `num_train_epochs=3`, `per_device_*_batch_size=32`, `lr=5e-5`, `seed=42`- `bf16=True` when CUDA is available- `eval_strategy="epoch"`, `save_strategy="epoch"`,`load_best_model_at_end=True`, `metric_for_best_model="accuracy"`- Metrics: **top-1 accuracy** and **top-5 accuracy**(`compute_metrics` compares `argmax` / top-5 logits vs. labels).- Optional `--push-to-hub` to publish the checkpoint to a private repo.**Inference** (`modules/classifier.py`):- `CLASSIFIER_MODEL_ID` env var points to the Hub repo of the fine-tunedmodel (loaded lazily, cached as module-level globals).- `classify_plant(image)`:1. `AutoImageProcessor` resizes/normalizes the uploaded `PIL.Image`.2. `AutoModelForImageClassification` runs a forward pass (`torch.no_grad()`).3. Softmax over the 289 logits β `(genus_name, confidence)`.- Called from `app.py`'s `add_plants_to_garden()` for every uploaded photo;the predicted genus drives everything downstream (care metadata, spritearchetype, advisor context).### 4.2 Care recommendation engine (`modules/plant.py`, `modules/recommender.py`)Not a learned model β a **deterministic lookup + template** layer that turnsthe classifier's genus output into actionable care info:- `Plant(genus)` looks up `data/growth_csv/growth_ds.csv` (296 genus βcare-profile rows, derived from a public plants-growth dataset) for`Watering`, `Sunlight`, `Soil`, `Fertilization Type`.- If the genus isn't in the CSV (e.g. a class the classifier knows but thegrowth table doesn't cover), `get_plant_info()` falls back to genericdefaults (`"Water when soil is dry"`, `"indirect sunlight"`,`"well-drained"`, `"No"` fertilizer).- `generate_care_notes()` assembles a short natural-language note from thesefields via string templates (no model call) β shown on the plant detailcard under *"Notes"*.### 4.3 Watering scheduler (`modules/watering.py` + `modules/weather_utils.py`)Also rule-based, but **weather-aware**:- `_parse_watering_frequency()` maps the CSV's free-text wateringinstructions (e.g. *"Keep soil consistently moist"*, *"Water weekly"*,*"every 10 days"*) to an integer interval in days, via an exact-matchtable plus regex fallbacks (`DEFAULT_INTERVAL = 4` days if nothingmatches).- `should_water(plant, last_watered, date, lat, lon)` returns `True` if:- `next_watering_date = last_watered + frequency_days` has passed, **and**- `did_or_will_rain(date, lat, lon, threshold=50%)` is `False` β i.e. itdidn't rain in the past (for historical dates) and isn't forecast torain β₯50% (for today/future), so the app doesn't tell you to water aplant that nature is about to water for you.- `load_garden()` also **back-fills `last_watered`** from `last_rained_date()`on every load: if it rained more recently than the recorded watering date,the plant is considered watered by rain (`rained: true`), avoidingover-watering recommendations after a period of inactivity.- The sidebar's "Watering today" table is produced by`get_watering_recommendations()`, which runs `should_water()` for everyplant against the live 7-day forecast.### 4.4 AI gardening advisor β chat (`modules/advisor.py::ask_about_plant`)**Task**: free-form Q&A about a *specific* plant ("Why are the leaves turningyellow?", "Can I plant this next to my tomatoes?").- Model: `ADVISOR_MODEL_ID` (default `Qwen/Qwen2.5-Coder-3B-Instruct`) servedvia **Hugging Face Inference Providers** (`provider="nscale"` by default),through `huggingface_hub.InferenceClient.chat_completion`.- **Grounding / prompt construction** (`_build_system_prompt`): the systemprompt is dynamically built from the plant's care profile (sunlight, soil,watering frequency, fertilization) **and** its live watering status(computed via `_watering_status()` from `last_watered`), so the model knowswhether the plant is overdue or recently watered before answering. If theuser has drawn "neighbor" links on the board, the linked plants'name/genus are injected too, so the model can reason aboutcompanion-planting effects (shared pests, competition for light/water,beneficial pairings).- The model is instructed to answer in **2β4 sentences**, in the samelanguage as the question, and to never recommend toxic/dangeroussubstances.- On any `InferenceClient` error, the function logs`[advisor] HF Inference error: ...` and returns a friendly fallbackmessage instead of crashing the UI.- Wired in `app.py` (`ask_plant_advisor`) to the "π€ Ask the assistant"button in the sidebar's *Plant assistant* panel, which only appears once aplant is selected on the board.### 4.5 Photo-based health diagnostic β vision-language (`modules/advisor.py::diagnose_plant_health`)**Task**: given a *new* photo of the selected plant, assess its health(leaves, stems, soil) and store the verdict on the plant record.- Model: the same `ADVISOR_MODEL_ID` / `ADVISOR_PROVIDER` client as the chatadvisor (Β§4.4), again via `InferenceClient.chat_completion` β but this timewith a **multimodal** message: the uploaded `PIL.Image` is re-encoded as JPEG,base64-encoded, and sent as an OpenAI-style content array(`{"type": "text", ...}` + `{"type": "image_url", "image_url": {"url":"data:image/jpeg;base64,..."}}`).- The prompt asks the model to start its reply with exactly one status wordβ **`Healthy`**, **`Needs attention`**, or **`Sick`** β followed by a 1β3sentence explanation and a suggested action.- `app.py::diagnose_selected_plant_health` persists the raw model responseinto `plant["health"]`, which is then surfaced on the plant detail card(**π©Ί Health:** ...) every time the garden is reloaded β so the diagnosissurvives page refreshes and is visible alongside the watering history.- Same defensive error handling as Β§4.4 (`[advisor] HF Inferencehealth-check error: ...` + fallback message).### 4.6 Procedural pixel-art sprites (`modules/pixel_art.py`) β not ML, but genus-awareWorth a short mention because it *feels* like generative output but is fullydeterministic: each genus is mapped to one of 6 hand-authored 16Γ16 "plantarchetype" sprites (cactus, succulent, fern, flower, palm, trailing) and oneof 4 pot styles, based on the genus's `Growth` / `Soil` / `Sunlight` valuesfrom the same `growth_ds.csv` (e.g. sandy soil + full sun + slow growth βcactus in a terracotta pot). Genera missing from the CSV get a **stablehash-based** archetype/pot assignment so the same unknown genus alwaysrenders the same sprite. Sprites are rendered once with PIL nearest-neighbourupscaling and cached to `static/sprites/<genus>.png`.---## 5. External APIsAll weather/geocoding calls go to **Open-Meteo** (free, no API key):| Function | Endpoint | Used for ||---|---|---|| `utils.geo.city_to_coordinates` | `geocoding-api.open-meteo.com/v1/search` | Turn the user's city into `(lat, lon)` at the location gate || `modules.weather_utils.weather_values` | `api.open-meteo.com/v1/forecast` (16-day daily) | 7-day forecast table (conditions, temp, rain %, wind) || `modules.weather_utils.did_or_will_rain` | forecast (future) or `archive-api.open-meteo.com` (past) | Decide whether a plant should be watered today / was watered by rain || `modules.weather_utils.last_rained_date` | `archive-api.open-meteo.com/v1/archive` (15-day lookback) | Back-fill `last_watered` on garden load |`weather_comment()` maps Open-Meteo's numeric WMO weather codes to shortemoji + text labels (e.g. `80` β "π¦οΈ Slight rain showers") shown in theforecast table.---## 6. UI / front-end notes- Single `gr.Blocks` app, themed with `gr.themes.Soft()` plus a large customstylesheet (`static/style.css`) that overrides Gradio's CSS variables fora green "Guarden" theme (custom button gradients, card radii, etc.).- **Garden board**: plants are rendered as absolutely-positioned `<div>`sprites inside `get_garden_board_html()`. A small injected `<script>`(`BOARD_JS`) uses pointer events to support:- **Drag & drop** β updates `position: {x%, y%}` (hidden `gr.Number` +sync button bridge the JS β Python boundary).- **Click to select** β opens the detail card + action row + assistantpanel for that plant.- **"π Link Neighbours" mode** β click two sprites to toggle a`neighbors` link, drawn as a dashed SVG line between them (re-renderedon every board update, so links follow plants when dragged).- **Sidebar**: watering recommendations table, 7-day forecast table (customCSS turns the Gradio dataframe into card-style rows with a styled headerrow), and the *Plant assistant* panel (chat + health diagnostic), whichonly becomes visible once a plant is selected.- **Per-user custom background**: an uploaded image is saved as`user_data/<uid>/background.jpg` and applied as the board's`background-image` via inline CSS.---## 7. DeploymentThe app is shipped as a **Hugging Face Space** (`sdk: gradio`,`sdk_version: 6.18.0`, entry point `app.py`, see the `README.md` frontmatter). Configuration is entirely via environment variables, with sanedefaults baked in so the app runs locally without any secrets:| Env var | Default | Purpose ||---|---|---|| `WEATHER_CITY` | `"Marseille"` | Initial forecast location before the user sets one || `CLASSIFIER_MODEL_ID` | `"your-username/plant-genus-classifier"` | HF Hub repo of the fine-tuned SigLIP genus classifier || `ADVISOR_MODEL_ID` / `ADVISOR_PROVIDER` | `Qwen/Qwen2.5-Coder-3B-Instruct` / `nscale` | Chat advisor + health-diagnostic model and HF Inference provider (shared) || `HF_TOKEN` | β | Hugging Face token for Inference Providers (advisor + health check) |`app.launch(allowed_paths=[...])` whitelists `user_data/`, `static/` and`plant_photos/` so per-user photos, sprites and backgrounds can be servedback to the browser via Gradio's `/gradio_api/file=` route.---
8 unmodified lines9101112131415161718193 unmodified lines2324252627282930313233343589 unmodified lines1251261271281291301318 unmodified linesADVISOR_MODEL_ID = os.getenv("ADVISOR_MODEL_ID", "Qwen/Qwen2.5-Coder-3B-Instruct")ADVISOR_PROVIDER = os.getenv("ADVISOR_PROVIDER", "nscale")HEALTH_MODEL_ID = os.getenv("HEALTH_MODEL_ID", "Qwen/Qwen2.5-VL-7B-Instruct")HEALTH_PROVIDER = os.getenv("HEALTH_PROVIDER", "auto")_client = None_health_client = Nonedef _get_client() -> InferenceClient:3 unmodified linesreturn _clientdef _get_health_client() -> InferenceClient:global _health_clientif _health_client is None:_health_client = InferenceClient(model=HEALTH_MODEL_ID, provider=HEALTH_PROVIDER, token=os.getenv("HF_TOKEN"))return _health_clientdef _watering_status(last_watered: str | None) -> str:if not last_watered:return "Never watered yet (no record)."89 unmodified lines)try:completion = _get_health_client().chat_completion(messages=[{"role": "user","content": [8 unmodified lines91011121314153 unmodified lines19202122232489 unmodified lines1141151161171181191208 unmodified linesADVISOR_MODEL_ID = os.getenv("ADVISOR_MODEL_ID", "Qwen/Qwen2.5-Coder-3B-Instruct")ADVISOR_PROVIDER = os.getenv("ADVISOR_PROVIDER", "nscale")_client = Nonedef _get_client() -> InferenceClient:3 unmodified linesreturn _clientdef _watering_status(last_watered: str | None) -> str:if not last_watered:return "Never watered yet (no record)."89 unmodified lines)try:completion = _get_client().chat_completion(messages=[{"role": "user","content": [
TECHNICAL_DOCUMENT.md+323
modules/advisor.py+1-12