Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_client import Client
|
3 |
+
import os
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
import base64
|
7 |
+
|
8 |
+
hf_token = os.environ.get("HF_TKN")
|
9 |
+
|
10 |
+
def convert_base64_to_img(image_string):
|
11 |
+
# Split the input string to separate the metadata header and the base64-encoded data
|
12 |
+
header, encoded_data = image_string.split(",", 1)
|
13 |
+
|
14 |
+
# Now, encoded_data contains the base64-encoded image data
|
15 |
+
image_data = base64.b64decode(encoded_data)
|
16 |
+
|
17 |
+
# Create a BytesIO object to store the image data
|
18 |
+
image_file = io.BytesIO(image_data)
|
19 |
+
|
20 |
+
# Open the image using the BytesIO object
|
21 |
+
img = Image.open(image_file)
|
22 |
+
|
23 |
+
# Save the image as a JPEG file
|
24 |
+
img.save('output.png', 'PNG')
|
25 |
+
|
26 |
+
return "output.png"
|
27 |
+
|
28 |
+
def infer(image_string, question):
|
29 |
+
image_in = convert_base64_to_img(image_string)
|
30 |
+
client = Client("https://fffiloni-moondream1.hf.space/", hf_token=hf_token)
|
31 |
+
result = client.predict(
|
32 |
+
image_in, # filepath in 'image' Image component
|
33 |
+
question, # str in 'Question' Textbox component
|
34 |
+
api_name="/predict"
|
35 |
+
)
|
36 |
+
print(result)
|
37 |
+
return result
|
38 |
+
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
with gr.Row():
|
41 |
+
with gr.Column():
|
42 |
+
image_string = gr.Textbox(interactive=False)
|
43 |
+
question = gr.Textbox(interactive=False)
|
44 |
+
submit_btn = gr.Button("Submit", interactive=False)
|
45 |
+
with gr.Column():
|
46 |
+
answer = gr.Textbox(interactive=False)
|
47 |
+
|
48 |
+
submit_btn.click(
|
49 |
+
fn=infer,
|
50 |
+
inputs=[image_string, question],
|
51 |
+
outputs=[answer]
|
52 |
+
)
|
53 |
+
|
54 |
+
demo.launch()
|