Spaces:
Runtime error
Runtime error
import gradio as gr | |
import numpy as np | |
import json | |
import os | |
import requests | |
header_key = os.environ["CVFIAHMED_KEY"] | |
def get_caption_onnx_api(imgf): | |
headers = { | |
'Content-Type': 'application/octet-stream', | |
'Ocp-Apim-Subscription-Key': header_key, | |
} | |
params = { | |
'features': 'description', | |
'model-version': 'latest', | |
'language': 'en', | |
'descriptionExclude': 'Celebrities,Landmarks', | |
} | |
with open(imgf, 'rb') as f: | |
data = f.read() | |
response = requests.post('https://cvfiahmed.cognitiveservices.azure.com/vision/v2022-07-31-preview/operations/imageanalysis:analyze', params=params, headers=headers, data=data) | |
return json.loads(response.content)['descriptionResult']['values'][0]['text'] | |
def add_image(state, image): | |
cap_onnx = get_caption_onnx_api(image.name) | |
state = state + [(f"![](/file={image.name})", cap_onnx)] | |
# print (image) | |
# print( np.fliplr(image) ) | |
# print(state) | |
return state, state | |
def add_text(state, text): | |
state = state + [(text, text + "?")] | |
return state, state | |
with gr.Blocks(css="#chatbot .overflow-y-auto{height:500px}") as demo: | |
chatbot = gr.Chatbot(elem_id="chatbot") | |
state = gr.State([]) | |
with gr.Row(): | |
with gr.Column(scale=0.85): | |
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False) | |
with gr.Column(scale=0.15, min_width=0): | |
btn = gr.UploadButton("Upload image here", file_types=["image", '.png', 'gif']) | |
txt.submit(add_text, [state, txt], [state, chatbot]) | |
txt.submit(lambda :"", None, txt) | |
btn.upload(add_image, [state, btn], [state, chatbot]) | |
demo.launch(share = False, server_name="0.0.0.0") |