Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- leaf-disease-huggingface.py +144 -0
- requirements-huggingface.txt +3 -0
leaf-disease-huggingface.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import base64
|
| 4 |
+
import os
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
# Set page configuration
|
| 9 |
+
st.set_page_config(
|
| 10 |
+
page_title="Leaf Disease Identifier",
|
| 11 |
+
page_icon="🍃",
|
| 12 |
+
layout="centered"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
# Custom CSS for styling
|
| 16 |
+
st.markdown("""
|
| 17 |
+
<style>
|
| 18 |
+
.main-title {
|
| 19 |
+
font-size: 36px;
|
| 20 |
+
color: #2C5F2D;
|
| 21 |
+
text-align: center;
|
| 22 |
+
margin-bottom: 20px;
|
| 23 |
+
}
|
| 24 |
+
.subtitle {
|
| 25 |
+
font-size: 18px;
|
| 26 |
+
color: #4A6741;
|
| 27 |
+
text-align: center;
|
| 28 |
+
margin-bottom: 30px;
|
| 29 |
+
}
|
| 30 |
+
.stButton>button {
|
| 31 |
+
background-color: #2C5F2D;
|
| 32 |
+
color: white;
|
| 33 |
+
width: 100%;
|
| 34 |
+
border: none;
|
| 35 |
+
padding: 10px;
|
| 36 |
+
border-radius: 5px;
|
| 37 |
+
}
|
| 38 |
+
.stButton>button:hover {
|
| 39 |
+
background-color: #4A6741;
|
| 40 |
+
}
|
| 41 |
+
</style>
|
| 42 |
+
""", unsafe_allow_html=True)
|
| 43 |
+
|
| 44 |
+
# Title and Description
|
| 45 |
+
st.markdown('<h1 class="main-title">🍃 Leaf Disease Identifier</h1>', unsafe_allow_html=True)
|
| 46 |
+
st.markdown('<p class="subtitle">Upload a leaf image and get instant disease analysis</p>', unsafe_allow_html=True)
|
| 47 |
+
|
| 48 |
+
def encode_image(image):
|
| 49 |
+
"""Encode image to base64"""
|
| 50 |
+
buffered = io.BytesIO()
|
| 51 |
+
image.save(buffered, format="PNG")
|
| 52 |
+
return base64.b64encode(buffered.getvalue()).decode('utf-8')
|
| 53 |
+
|
| 54 |
+
def analyze_leaf_disease(image):
|
| 55 |
+
"""Analyze leaf image using Claude API"""
|
| 56 |
+
# Hugging Face will require you to add API key as an environment variable
|
| 57 |
+
api_key = os.getenv("ANTHROPIC_API_KEY")
|
| 58 |
+
|
| 59 |
+
if not api_key:
|
| 60 |
+
st.error("API Key not configured. Please set up Anthropic API key.")
|
| 61 |
+
return None
|
| 62 |
+
|
| 63 |
+
headers = {
|
| 64 |
+
"Content-Type": "application/json",
|
| 65 |
+
"anthropic-version": "2023-06-01",
|
| 66 |
+
"X-API-Key": api_key
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
payload = {
|
| 70 |
+
"model": "claude-3-5-sonnet-20240620",
|
| 71 |
+
"max_tokens": 1000,
|
| 72 |
+
"messages": [
|
| 73 |
+
{
|
| 74 |
+
"role": "user",
|
| 75 |
+
"content": [
|
| 76 |
+
{
|
| 77 |
+
"type": "image",
|
| 78 |
+
"source": {
|
| 79 |
+
"type": "base64",
|
| 80 |
+
"media_type": "image/png",
|
| 81 |
+
"data": encode_image(image)
|
| 82 |
+
}
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
"type": "text",
|
| 86 |
+
"text": """Analyze this leaf image in detail.
|
| 87 |
+
Identify:
|
| 88 |
+
1. Plant species (if possible)
|
| 89 |
+
2. Specific disease or health condition
|
| 90 |
+
3. Detailed symptoms
|
| 91 |
+
4. Potential causes
|
| 92 |
+
5. Recommended treatment or management strategies
|
| 93 |
+
|
| 94 |
+
Provide a comprehensive and clear explanation."""
|
| 95 |
+
}
|
| 96 |
+
]
|
| 97 |
+
}
|
| 98 |
+
]
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
try:
|
| 102 |
+
response = requests.post(
|
| 103 |
+
"https://api.anthropic.com/v1/messages",
|
| 104 |
+
headers=headers,
|
| 105 |
+
json=payload
|
| 106 |
+
)
|
| 107 |
+
response.raise_for_status()
|
| 108 |
+
result = response.json()
|
| 109 |
+
return result['content'][0]['text']
|
| 110 |
+
|
| 111 |
+
except requests.exceptions.RequestException as e:
|
| 112 |
+
st.error(f"API Request Error: {e}")
|
| 113 |
+
return None
|
| 114 |
+
except KeyError:
|
| 115 |
+
st.error("Unexpected API response format")
|
| 116 |
+
return None
|
| 117 |
+
|
| 118 |
+
def main():
|
| 119 |
+
# Image upload
|
| 120 |
+
uploaded_file = st.file_uploader(
|
| 121 |
+
"Upload a leaf image",
|
| 122 |
+
type=["jpg", "jpeg", "png"],
|
| 123 |
+
help="Upload a clear image of a leaf for disease analysis"
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
if uploaded_file is not None:
|
| 127 |
+
# Display uploaded image
|
| 128 |
+
image = Image.open(uploaded_file)
|
| 129 |
+
st.image(image, caption='Uploaded Leaf Image', use_column_width=True)
|
| 130 |
+
|
| 131 |
+
# Analysis button
|
| 132 |
+
if st.button('Analyze Leaf Disease'):
|
| 133 |
+
with st.spinner('Analyzing image... This might take a moment'):
|
| 134 |
+
analysis = analyze_leaf_disease(image)
|
| 135 |
+
|
| 136 |
+
if analysis:
|
| 137 |
+
st.success('Analysis Complete!')
|
| 138 |
+
st.markdown("### 🔬 Leaf Disease Analysis")
|
| 139 |
+
st.write(analysis)
|
| 140 |
+
else:
|
| 141 |
+
st.error("Failed to analyze the image. Please try again.")
|
| 142 |
+
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
main()
|
requirements-huggingface.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.33.0
|
| 2 |
+
requests==2.31.0
|
| 3 |
+
Pillow==10.3.0
|