File size: 1,779 Bytes
acc4ffe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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")