import io import os import requests import streamlit as st from PIL import Image API_BASE = os.environ.get("API_BASE") or st.secrets.get("API_BASE") or "https://sido1991-apiagrilens.hf.space" st.set_page_config(page_title="AgriLens AI", page_icon="🌱", layout="centered") st.title("🌱 AgriLens AI - Plant Disease Diagnosis") with st.sidebar: st.markdown("### API") api_url = st.text_input("API base URL", API_BASE, help="Your FastAPI base") col1, col2 = st.columns(2) with col1: if st.button("Health check"): try: r = requests.get(f"{api_url}/health", timeout=60) st.write(r.status_code, r.json()) except Exception as e: st.error(str(e)) with col2: if st.button("Load model"): try: r = requests.get(f"{api_url}/load", timeout=300) st.write(r.status_code, r.json()) except Exception as e: st.error(str(e)) uploaded = st.file_uploader("Upload a leaf photo", type=["jpg","jpeg","png"]) culture = st.text_input("Culture (optional)") notes = st.text_area("Notes (optional)") if st.button("Analyze") and uploaded: img = Image.open(uploaded).convert("RGB") buf = io.BytesIO() img.save(buf, format="JPEG", quality=90) buf.seek(0) files = {"image": ("image.jpg", buf.getvalue(), "image/jpeg")} data = {} if culture: data["culture"] = culture if notes: data["notes"] = notes with st.spinner("Analyzing..."): r = requests.post(f"{api_url}/diagnose", files=files, data=data, timeout=180) if r.ok: out = r.json() st.image(img, caption="Input") st.subheader("Diagnosis") st.write(out.get("diagnosis", "No text")) else: st.error(f"Error {r.status_code}: {r.text}")