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 = "Llama-3.2-90B-Vision-Instruct" # 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): try: # Convert the uploaded image to a compatible format (e.g., save it locally) image.save("uploaded_image.jpg") # Prepare and send the request to the Azure API response = client.complete( messages=[ SystemMessage( content="You are a helpful assistant that describes leaf disease in detail." ), UserMessage( content=[ TextContentItem(text="What's the leaf disease in this image?"), ImageContentItem( image_url=ImageUrl.load( image_file="uploaded_image.jpg", 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 interface = gr.Interface( fn=analyze_leaf_disease, inputs=gr.Image(type="file", label="Upload an Image of a Leaf"), outputs="text", title="Leaf Disease Detector", description="Upload an image of a leaf, and this tool will identify the disease affecting it.", ) # Launch the interface if __name__ == "__main__": interface.launch()