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