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()