Spaces:
Runtime error
Runtime error
chats-bug
commited on
Commit
·
0d54c12
1
Parent(s):
046f505
Fixed git large coco model
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, VisionEncoderDecoderModel, BitsAndBytesConfig
|
3 |
import torch
|
@@ -38,7 +39,6 @@ def generate_caption(
|
|
38 |
model,
|
39 |
image,
|
40 |
tokenizer=None,
|
41 |
-
use_float_16=False,
|
42 |
):
|
43 |
"""
|
44 |
Generate captions for the given image.
|
@@ -61,15 +61,10 @@ def generate_caption(
|
|
61 |
str
|
62 |
The generated caption.
|
63 |
"""
|
64 |
-
|
65 |
-
pixel_values = preprocessor(images=image, return_tensors="pt").pixel_values
|
66 |
-
|
67 |
-
if use_float_16:
|
68 |
-
inputs = inputs.to(torch.float16)
|
69 |
|
70 |
generated_ids = model.generate(
|
71 |
pixel_values=pixel_values,
|
72 |
-
attention_mask=inputs.attention_mask,
|
73 |
max_length=50,
|
74 |
)
|
75 |
|
@@ -117,7 +112,6 @@ def generate_captions(
|
|
117 |
image,
|
118 |
max_length,
|
119 |
temperature,
|
120 |
-
use_sample_image,
|
121 |
):
|
122 |
"""
|
123 |
Generate captions for the given image.
|
@@ -137,10 +131,6 @@ def generate_captions(
|
|
137 |
caption_git_large_coco = ""
|
138 |
caption_oc_coca = ""
|
139 |
|
140 |
-
if use_sample_image:
|
141 |
-
url = "http://images.cocodataset.org/val2017/000000039769.jpg"
|
142 |
-
image = Image.open(requests.get(url, stream=True).raw)
|
143 |
-
|
144 |
# Generate captions for the image using the Blip base model
|
145 |
try:
|
146 |
caption_blip_base = generate_caption(preprocessor_blip_base, model_blip_base, image).strip()
|
@@ -176,7 +166,6 @@ iface = gr.Interface(
|
|
176 |
gr.inputs.Image(type="pil", label="Image"),
|
177 |
gr.inputs.Slider(minimum=16, maximum=64, step=2, default=32, label="Max Length"),
|
178 |
gr.inputs.Slider(minimum=0.5, maximum=1.5, step=0.1, default=1.0, label="Temperature"),
|
179 |
-
gr.inputs.Checkbox(default=False, label="Use example image")
|
180 |
],
|
181 |
# Define the outputs
|
182 |
outputs=[
|
|
|
1 |
+
import traceback
|
2 |
import gradio as gr
|
3 |
from transformers import AutoProcessor, AutoTokenizer, AutoImageProcessor, AutoModelForCausalLM, BlipForConditionalGeneration, VisionEncoderDecoderModel, BitsAndBytesConfig
|
4 |
import torch
|
|
|
39 |
model,
|
40 |
image,
|
41 |
tokenizer=None,
|
|
|
42 |
):
|
43 |
"""
|
44 |
Generate captions for the given image.
|
|
|
61 |
str
|
62 |
The generated caption.
|
63 |
"""
|
64 |
+
pixel_values = preprocessor(images=image, return_tensors="pt").pixel_values.to(device)
|
|
|
|
|
|
|
|
|
65 |
|
66 |
generated_ids = model.generate(
|
67 |
pixel_values=pixel_values,
|
|
|
68 |
max_length=50,
|
69 |
)
|
70 |
|
|
|
112 |
image,
|
113 |
max_length,
|
114 |
temperature,
|
|
|
115 |
):
|
116 |
"""
|
117 |
Generate captions for the given image.
|
|
|
131 |
caption_git_large_coco = ""
|
132 |
caption_oc_coca = ""
|
133 |
|
|
|
|
|
|
|
|
|
134 |
# Generate captions for the image using the Blip base model
|
135 |
try:
|
136 |
caption_blip_base = generate_caption(preprocessor_blip_base, model_blip_base, image).strip()
|
|
|
166 |
gr.inputs.Image(type="pil", label="Image"),
|
167 |
gr.inputs.Slider(minimum=16, maximum=64, step=2, default=32, label="Max Length"),
|
168 |
gr.inputs.Slider(minimum=0.5, maximum=1.5, step=0.1, default=1.0, label="Temperature"),
|
|
|
169 |
],
|
170 |
# Define the outputs
|
171 |
outputs=[
|