KH-PAEN commited on
Commit
d664a48
·
verified ·
1 Parent(s): b83913d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import io
3
+ import os
4
+ import requests
5
+ import json
6
+ from PIL import Image
7
+
8
+ endpoint = os.getenv("endpoint")
9
+ predictionKey = os.getenv("predictionKey")
10
+
11
+ # Azure CustomVision Project
12
+ projectID = os.getenv("projectID")
13
+ publishName = os.getenv("publishName")
14
+
15
+ # Function to make a prediction using an image URL
16
+ def predict_image(image):
17
+ url = f"{endpoint}/customvision/v3.0/Prediction/{projectID}/classify/iterations/{publishName}/image"
18
+ headers = {
19
+ "Content-Type": "application/octet-stream",
20
+ "Prediction-Key": prediction_key
21
+ }
22
+
23
+ img_byte_arr = io.BytesIO()
24
+ image.save(img_byte_arr, format='JPEG') # Adjust format if needed (e.g., 'PNG')
25
+ img_byte_arr = img_byte_arr.getvalue()
26
+
27
+ response = requests.post(url, headers=headers, data=img_byte_arr)
28
+ response = (response.json())["predictions"]
29
+ result = ""
30
+ for res in response:
31
+ result += f"{res["tagName"]} :{res["probability"]:.2f}\n"
32
+ return result
33
+
34
+ # Make a prediction
35
+ # prediction_result = predict_image_url(url)
36
+
37
+ # for prediction in prediction_result["predictions"]:
38
+ # print(f"{prediction['tagName']}: {prediction['probability']*100 :.2f}%")
39
+
40
+ interface = gr.Interface(
41
+ fn=predict_image,
42
+ inputs=gr.Image(height=600, width=600, type="pil", label="Upload an Image"),
43
+ outputs=gr.Textbox(label="Classification Result"),
44
+ title="Azure Custom Vision Image Classifier",
45
+ description="Upload an image to classify it using Azure Custom Vision API."
46
+ )
47
+
48
+ if __name__ == "__main__":
49
+ interface.launch()