Spaces:
Sleeping
Sleeping
File size: 3,040 Bytes
86886cf 95f5acf 3051b4e 95f5acf 86886cf a933af8 95f5acf 3051b4e 86886cf 3051b4e 95f5acf 3051b4e 95f5acf 86886cf 95f5acf 86886cf 95f5acf 26043fa 95f5acf 3051b4e 95f5acf 3051b4e 95f5acf a933af8 95f5acf 3051b4e 86886cf 1db8eb2 86886cf 1db8eb2 86886cf 3051b4e 86886cf |
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 |
import gradio as gr
from azure.ai.inference import ChatCompletionsClient
from azure.ai.inference.models import (
SystemMessage,
UserMessage,
TextContentItem,
ImageContentItem,
ImageUrl,
ImageDetailLevel,
)
from azure.core.credentials import AzureKeyCredential
# Azure API credentials
token = "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"
endpoint = "https://models.inference.ai.azure.com"
model_name = "gpt-4o"
# Initialize the ChatCompletionsClient
client = ChatCompletionsClient(
endpoint=endpoint,
credential=AzureKeyCredential(token),
)
# Define the function to handle the image and get predictions
def analyze_leaf_disease(image_path, leaf_type):
try:
# Prepare and send the request to the Azure API
response = client.complete(
messages=[
SystemMessage(
content=f"You are a subject matter expert that describes leaf disease in detail for {leaf_type} leaves."
),
UserMessage(
content=[
TextContentItem(text="What's the name of the leaf disease in this image and what is the confidence score? What is the probable reason? What are the medicine or stops to prevent the disease"),
ImageContentItem(
image_url=ImageUrl.load(
image_file=image_path,
image_format="jpg",
detail=ImageDetailLevel.LOW,
)
),
],
),
],
model=model_name,
)
# Extract and return the response content
return response.choices[0].message.content
except Exception as e:
return f"An error occurred: {e}"
# Define the Gradio interface
def handle_proceed(image_path, leaf_type):
# Display detecting status
detecting_status = "Detecting..."
result = analyze_leaf_disease(image_path, leaf_type)
# Clear detecting status after processing
return "", result
with gr.Blocks() as interface:
with gr.Row():
gr.Markdown("""
# Leaf Disease Detector
Upload a leaf image, select the leaf type, and let the AI analyze the disease.
""")
with gr.Row():
image_input = gr.Image(type="filepath", label="Upload an Image or Take a Photo")
leaf_type = gr.Dropdown(
choices=["Tomato", "Tobacco", "Corn", "Paddy", "Maze", "Potato", "Wheat"],
label="Select Leaf Type",
)
proceed_button = gr.Button("Proceed")
with gr.Row():
detecting_label = gr.Label("Detecting...", visible=False)
output_box = gr.Textbox(label="Results", placeholder="Results will appear here.")
# Update the detecting_label and result in outputs
proceed_button.click(handle_proceed, inputs=[image_input, leaf_type], outputs=[detecting_label, output_box])
if __name__ == "__main__":
interface.launch()
|