Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,170 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from gradio_client import Client
|
3 |
+
import os
|
4 |
+
import json
|
5 |
+
|
6 |
+
def postprocess_kosmos_out(result):
|
7 |
+
token = ""
|
8 |
+
for res in result[1]:
|
9 |
+
token += res["token"]
|
10 |
+
return token
|
11 |
+
|
12 |
+
def generate_caption_fuyu(image_path, caption_bool):
|
13 |
+
try:
|
14 |
+
from gradio_client import Client
|
15 |
+
|
16 |
+
client = Client("https://adept-fuyu-8b-demo.hf.space/--replicas/7fa6n/")
|
17 |
+
result = client.predict(
|
18 |
+
image_path,
|
19 |
+
caption_bool,
|
20 |
+
fn_index=2
|
21 |
+
)
|
22 |
+
return result
|
23 |
+
except Exception as e:
|
24 |
+
print(e)
|
25 |
+
gr.Warning("The Fuyu-8B Space is currently unavailable. Please try again later.")
|
26 |
+
return ""
|
27 |
+
|
28 |
+
def generate_answer_fuyu(image_path, question):
|
29 |
+
try:
|
30 |
+
from gradio_client import Client
|
31 |
+
|
32 |
+
client = Client("https://adept-fuyu-8b-demo.hf.space/--replicas/7fa6n/")
|
33 |
+
result = client.predict(
|
34 |
+
image_path,
|
35 |
+
question,
|
36 |
+
fn_index=3
|
37 |
+
)
|
38 |
+
print(result)
|
39 |
+
return result
|
40 |
+
except Exception as e:
|
41 |
+
print(e)
|
42 |
+
gr.Warning("The Fuyu-8B Space is currently unavailable. Please try again later.")
|
43 |
+
return ""
|
44 |
+
|
45 |
+
|
46 |
+
def generate_caption_kosmos(image_path, caption_bool):
|
47 |
+
|
48 |
+
client = Client("https://merve-kosmos2.hf.space/--replicas/972ip/")
|
49 |
+
try:
|
50 |
+
if caption_bool:
|
51 |
+
caption = "Detailed"
|
52 |
+
else:
|
53 |
+
caption = "Brief"
|
54 |
+
result = client.predict(image_path, caption, None,
|
55 |
+
api_name="/generate_predictions"
|
56 |
+
)
|
57 |
+
return postprocess_kosmos_out(result)
|
58 |
+
except Exception as e:
|
59 |
+
print(e)
|
60 |
+
gr.Warning("The KOSMOS-2 Space is currently unavailable. Please try again later.")
|
61 |
+
return ""
|
62 |
+
|
63 |
+
def generate_answer_kosmos(image_path, question):
|
64 |
+
try:
|
65 |
+
from gradio_client import Client
|
66 |
+
|
67 |
+
client = Client("https://merve-kosmos2.hf.space/--replicas/972ip/")
|
68 |
+
result = client.predict(
|
69 |
+
image_path,
|
70 |
+
None,
|
71 |
+
question,
|
72 |
+
fn_index=3
|
73 |
+
)
|
74 |
+
|
75 |
+
return postprocess_kosmos_out(result)
|
76 |
+
except Exception as e:
|
77 |
+
print(e)
|
78 |
+
gr.Warning("The KOSMOS-2 Space is currently unavailable. Please try again later.")
|
79 |
+
return ""
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
def generate_caption(image_path, caption_bool):
|
84 |
+
|
85 |
+
kosmos_caption = generate_caption_kosmos(image_path, caption_bool)
|
86 |
+
fuyu_caption = generate_caption_fuyu(image_path, caption_bool)
|
87 |
+
|
88 |
+
return kosmos_caption, fuyu_caption
|
89 |
+
|
90 |
+
|
91 |
+
def generate_answers(image_path, question):
|
92 |
+
|
93 |
+
kosmos_answer = generate_answer_kosmos(image_path, question)
|
94 |
+
fuyu_answer = generate_answer_fuyu(image_path, question)
|
95 |
+
|
96 |
+
return kosmos_answer, fuyu_answer
|
97 |
+
|
98 |
+
title = "# Comparing Vision Language Models"
|
99 |
+
|
100 |
+
css = """
|
101 |
+
#mkd {
|
102 |
+
height: 500px;
|
103 |
+
overflow: auto;
|
104 |
+
border: 1px solid #ccc;
|
105 |
+
}
|
106 |
+
"""
|
107 |
+
|
108 |
+
with gr.Blocks(css=css) as demo:
|
109 |
+
gr.HTML("<h1><center>Compare Vision Language Models 🖼️ 💬 <center><h1>")
|
110 |
+
gr.Markdown("Vision Language Models are essentially language models with a capability of understanding images.")
|
111 |
+
gr.Markdown("To try this Space, simply try either captioning or visual question answering. ")
|
112 |
+
gr.Markdown("If prompted to wait and try again, please try again. This Space uses other Spaces as APIs, so it might take time to get those Spaces up and running if they're stopped.")
|
113 |
+
gr.Markdown("Lastly, Fuyu-8B and KOSMOS-2 has the capability of locating images in object detection-like manner. Feel free to try them in their own Spaces.")
|
114 |
+
|
115 |
+
with gr.Row():
|
116 |
+
with gr.Tab("Visual Question Answering"):
|
117 |
+
with gr.Column():
|
118 |
+
input_image = gr.Image(label = "Input Image", type="filepath")
|
119 |
+
question = gr.Textbox(label = "question")
|
120 |
+
run_button = gr.Button("Answer")
|
121 |
+
with gr.Column():
|
122 |
+
answer_kosmos = gr.Textbox(label="Answer generated by KOSMOS-2")
|
123 |
+
answer_fuyu = gr.Textbox(label="Answer generated by Fuyu-8B")
|
124 |
+
|
125 |
+
outputs_answer = [
|
126 |
+
answer_kosmos, answer_fuyu
|
127 |
+
]
|
128 |
+
|
129 |
+
gr.Examples(
|
130 |
+
examples = [["./cat.png", "What is behind the cat?"]],
|
131 |
+
inputs=[input_image, question],
|
132 |
+
outputs=outputs_answer,
|
133 |
+
fn=generate_answers,
|
134 |
+
cache_examples=True
|
135 |
+
)
|
136 |
+
|
137 |
+
run_button.click(
|
138 |
+
fn=generate_answers,
|
139 |
+
inputs=[input_image,question],
|
140 |
+
outputs=outputs_answer
|
141 |
+
)
|
142 |
+
with gr.Tab("Image Captioning"):
|
143 |
+
|
144 |
+
with gr.Column():
|
145 |
+
input_image = gr.Image(label = "Input Image", type="filepath")
|
146 |
+
detailed_caption = gr.Checkbox(label = "Detailed Captioning")
|
147 |
+
run_button = gr.Button("Caption")
|
148 |
+
with gr.Column():
|
149 |
+
caption_kosmos = gr.Textbox(label="Caption generated by KOSMOS-2")
|
150 |
+
caption_fuyu = gr.Textbox(label="Caption generated by Fuyu-8B")
|
151 |
+
|
152 |
+
outputs_caption = [caption_kosmos, caption_fuyu]
|
153 |
+
|
154 |
+
gr.Examples(
|
155 |
+
examples = [["./cat.png", True], ["./cat.png", False]],
|
156 |
+
inputs=[input_image, detailed_caption],
|
157 |
+
outputs=outputs_caption,
|
158 |
+
fn=generate_caption,
|
159 |
+
cache_examples=True
|
160 |
+
)
|
161 |
+
|
162 |
+
|
163 |
+
run_button.click(
|
164 |
+
fn=generate_caption,
|
165 |
+
inputs=[input_image,detailed_caption],
|
166 |
+
outputs=outputs_caption
|
167 |
+
)
|
168 |
+
|
169 |
+
if __name__ == "__main__":
|
170 |
+
demo.queue().launch(debug=True)
|