File size: 1,834 Bytes
d731ef7
f76da4e
d731ef7
 
 
bf87feb
d731ef7
e4e5d58
d731ef7
 
a7309bb
d731ef7
 
 
 
 
 
a7309bb
d731ef7
 
 
 
 
 
1cd64f0
d731ef7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cd64f0
d731ef7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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}")