File size: 1,340 Bytes
d664a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c89d21e
d664a48
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()