import requests import os, io import gradio as gr # from PIL import Image # API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-panoptic" # headers = {"Authorization": "Bearer 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"} API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50-dc5-panoptic" headers = {"Authorization": "Bearer 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"} def image_classifier(inp): return {'cat': 0.3, 'dog': 0.7} def query(filename): with open(filename, "rb") as f: data = f.read() response = requests.post(API_URL, headers=headers, data=data) return response.json() def rb(img): # initialiaze io to_bytes converter img_byte_arr = io.BytesIO() # define quality of saved array img.save(img_byte_arr, format='JPEG', subsampling=0, quality=100) # converts image array to bytesarray img_byte_arr = img_byte_arr.getvalue() # response = requests.post(API_URL, headers=headers, data=bytes(img.tobytes("raw"))) response = requests.post(API_URL, headers=headers, data=img_byte_arr) return response.json() # train = os.listdir("./") # print(train) output = query("./09_truck.jpg") inputs = gr.inputs.Image(type="pil", label="Upload an image") demo = gr.Interface(fn=rb, inputs=inputs, outputs="json") demo.launch()