Spaces:
Sleeping
Sleeping
| 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() | |