import gradio as gr import io import os import requests import json from PIL import Image endpoint = os.getenv("endpoint") predictionKey = os.getenv("predictionKey") # Azure CustomVision Project projectID = os.getenv("projectID") publishName = os.getenv("publishName") # Function to make a prediction using an image URL def predict_image(image): url = f"{endpoint}/customvision/v3.0/Prediction/{projectID}/classify/iterations/{publishName}/image" headers = { "Content-Type": "application/octet-stream", "Prediction-Key": predictionKey } img_byte_arr = io.BytesIO() image.save(img_byte_arr, format='JPEG') # Adjust format if needed (e.g., 'PNG') img_byte_arr = img_byte_arr.getvalue() response = requests.post(url, headers=headers, data=img_byte_arr) response = (response.json())["predictions"] result = "" for res in response: result += f"{res["tagName"]} :{res["probability"]:.2f}\n" return result interface = gr.Interface( fn=predict_image, inputs=gr.Image(height=600, width=600, type="pil", label="Upload an Image"), outputs=gr.Textbox(label="Classification Result"), title="Azure Custom Vision Image Classifier", description="Upload an image to classify it using Azure Custom Vision API." ) if __name__ == "__main__": interface.launch()