File size: 1,588 Bytes
2f69612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d564997
2f69612
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio_client import Client
import os
from PIL import Image
import io
import base64

hf_token = os.environ.get("HF_TKN")

def convert_base64_to_img(image_string):
    # Split the input string to separate the metadata header and the base64-encoded data
    header, encoded_data = image_string.split(",", 1)
    
    # Now, encoded_data contains the base64-encoded image data
    image_data = base64.b64decode(encoded_data)
    
    # Create a BytesIO object to store the image data
    image_file = io.BytesIO(image_data)
    
    # Open the image using the BytesIO object
    img = Image.open(image_file)
    
    # Save the image as a JPEG file
    img.save('output.png', 'PNG')
    
    return "output.png"

def infer(image_string, question):
    image_in = convert_base64_to_img(image_string)
    client = Client("https://fffiloni-moondream1.hf.space/", hf_token=hf_token)
    print(client)
    result = client.predict(
    		image_in,	# filepath  in 'image' Image component
    		question,	# str  in 'Question' Textbox component
    		api_name="/predict"
    )
    print(result)
    return result

with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column():
            image_string = gr.Textbox(interactive=False)
            question = gr.Textbox(interactive=False)
            submit_btn = gr.Button("Submit", interactive=False)
        with gr.Column():
            answer = gr.Textbox(interactive=False)
            
    submit_btn.click(
        fn=infer,
        inputs=[image_string, question],
        outputs=[answer]
    )

demo.launch()