Spaces:
Sleeping
Sleeping
gabrielmotablima
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -24,14 +24,6 @@ def preload_models():
|
|
24 |
|
25 |
models = preload_models()
|
26 |
|
27 |
-
# Function to process the image and generate a caption
|
28 |
-
def generate_caption(image, model_name):
|
29 |
-
model, tokenizer, image_processor = models[model_name]
|
30 |
-
pixel_values = image_processor(image, return_tensors="pt").pixel_values
|
31 |
-
generated_ids = model.generate(pixel_values)
|
32 |
-
caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
33 |
-
return caption
|
34 |
-
|
35 |
# Predefined images for selection
|
36 |
image_folder = "images"
|
37 |
predefined_images = [
|
@@ -40,17 +32,22 @@ predefined_images = [
|
|
40 |
if fname.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.ppm'))
|
41 |
]
|
42 |
|
43 |
-
#
|
44 |
-
def
|
45 |
if image is None:
|
46 |
return None, None
|
47 |
pil_image = image.convert("RGB")
|
48 |
return pil_image, None
|
49 |
|
50 |
-
|
|
|
51 |
if image is None:
|
52 |
return "Please upload an image to generate a caption."
|
53 |
-
|
|
|
|
|
|
|
|
|
54 |
|
55 |
# Define UI
|
56 |
with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange")) as interface:
|
@@ -84,7 +81,7 @@ with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange"
|
|
84 |
with gr.Row(variant='panel'):
|
85 |
examples = gr.Examples(
|
86 |
examples=predefined_images,
|
87 |
-
fn=
|
88 |
inputs=[image_display],
|
89 |
outputs=[image_display, output_text],
|
90 |
label="Examples"
|
@@ -93,9 +90,9 @@ with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange"
|
|
93 |
# Define actions
|
94 |
model_selector.change(fn=lambda: (None, None), outputs=[image_display, output_text])
|
95 |
|
96 |
-
image_display.upload(fn=
|
97 |
image_display.clear(fn=lambda: None, outputs=[output_text])
|
98 |
|
99 |
-
generate_button.click(fn=
|
100 |
|
101 |
interface.launch(share=False)
|
|
|
24 |
|
25 |
models = preload_models()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Predefined images for selection
|
28 |
image_folder = "images"
|
29 |
predefined_images = [
|
|
|
32 |
if fname.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.ppm'))
|
33 |
]
|
34 |
|
35 |
+
# Function to preprocess the image to RGB format
|
36 |
+
def preprocess_image(image):
|
37 |
if image is None:
|
38 |
return None, None
|
39 |
pil_image = image.convert("RGB")
|
40 |
return pil_image, None
|
41 |
|
42 |
+
# Function to process the image and generate a caption
|
43 |
+
def generate_caption(image, selected_model):
|
44 |
if image is None:
|
45 |
return "Please upload an image to generate a caption."
|
46 |
+
model, tokenizer, image_processor = models[selected_model]
|
47 |
+
pixel_values = image_processor(image, return_tensors="pt").pixel_values
|
48 |
+
generated_ids = model.generate(pixel_values)
|
49 |
+
caption = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
50 |
+
return caption
|
51 |
|
52 |
# Define UI
|
53 |
with gr.Blocks(theme=gr.themes.Citrus(primary_hue="blue", secondary_hue="orange")) as interface:
|
|
|
81 |
with gr.Row(variant='panel'):
|
82 |
examples = gr.Examples(
|
83 |
examples=predefined_images,
|
84 |
+
fn=preprocess_image,
|
85 |
inputs=[image_display],
|
86 |
outputs=[image_display, output_text],
|
87 |
label="Examples"
|
|
|
90 |
# Define actions
|
91 |
model_selector.change(fn=lambda: (None, None), outputs=[image_display, output_text])
|
92 |
|
93 |
+
image_display.upload(fn=preprocess_image, inputs=[image_display], outputs=[image_display, output_text])
|
94 |
image_display.clear(fn=lambda: None, outputs=[output_text])
|
95 |
|
96 |
+
generate_button.click(fn=generate_caption, inputs=[image_display, model_selector], outputs=output_text)
|
97 |
|
98 |
interface.launch(share=False)
|