File size: 1,379 Bytes
0d2dd1f
4ebcacd
0d2dd1f
4ebcacd
 
0d2dd1f
4ebcacd
 
 
 
 
 
 
 
0d2dd1f
4ebcacd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#app.py
import os
import gradio as gr
from gradio_client import Client
from gradio_client.client import Job

def vqa_fuyu(image_path: str, question: str, return_job: bool = False) -> str | Job:
    try:
        client = Client("https://adept-fuyu-8b-demo.hf.space/--replicas/9kcqv/")
        fn = client.submit if return_job else client.predict
        return fn(image_path, question, fn_index=3)
    except Exception:
        gr.Warning("The Fuyu-8B Space is currently unavailable. Please try again later.")
        return ""

def answer_question(image_path: str, question:str) -> tuple[str]:
    jobs = [
        vqa_fuyu(image_path, question, return_job=True),
    ]
    return tuple(job.result() if job else "" for job in jobs)

def ask(q):
    return q

with gr.Blocks(css="style.css") as demo:
    gr.Markdown(DESCRIPTION)
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(type="filepath")
            text_input = gr.Interface(fn=ask, inputs="text", outputs="text")
            run_button = gr.Button("ask question")
        with gr.Column():
            out_fuyu = gr.Textbox(label="Fuyu-8B")

    outputs = [
        out_fuyu,
    ]

run_button.click(
        fn=answer_question,
        inputs=[input_image,text_input],
        outputs=outputs,
        api_name="vqa",
    )

if __name__ == "__main__":
    demo.queue(max_size=20).launch()