Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -65,17 +65,17 @@ trans_model = MarianMTModel.from_pretrained(model_name)
|
|
| 65 |
def caption_random_image():
|
| 66 |
|
| 67 |
# AA: pick random row - from DF
|
| 68 |
-
sample = df.sample(1).iloc[0]
|
| 69 |
|
| 70 |
# BB: Pick a random image path - image from folder
|
| 71 |
-
|
| 72 |
|
| 73 |
# BB: Load into PIL - image from folder - image from folder
|
| 74 |
-
|
| 75 |
|
| 76 |
|
| 77 |
# AA: Image - for DF
|
| 78 |
-
image = sample["image"]
|
| 79 |
|
| 80 |
# Unconditional image captioning
|
| 81 |
inputs = processor(image, return_tensors="pt")
|
|
@@ -84,9 +84,9 @@ def caption_random_image():
|
|
| 84 |
caption_eng = processor.decode(out[0], skip_special_tokens=True)
|
| 85 |
|
| 86 |
# Translate caption from English to Spanish
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
| 91 |
return image, caption_eng, caption_es
|
| 92 |
|
|
@@ -96,8 +96,8 @@ demo = gr.Interface(
|
|
| 96 |
inputs=None,
|
| 97 |
outputs=[
|
| 98 |
gr.Image(type="pil", label="Random Image"),
|
| 99 |
-
gr.Textbox(label="Caption (English)")
|
| 100 |
-
|
| 101 |
],
|
| 102 |
title="Image Captioning (with English to Spanish translation)",
|
| 103 |
description="Selects a random image (from either the local folder or henryscheible/coco data subset); generates a BLIP caption; then translates the (English) caption to Spanish."
|
|
|
|
| 65 |
def caption_random_image():
|
| 66 |
|
| 67 |
# AA: pick random row - from DF
|
| 68 |
+
##sample = df.sample(1).iloc[0]
|
| 69 |
|
| 70 |
# BB: Pick a random image path - image from folder
|
| 71 |
+
img_path = random.choice(image_paths)
|
| 72 |
|
| 73 |
# BB: Load into PIL - image from folder - image from folder
|
| 74 |
+
image = Image.open(img_path).convert("RGB")
|
| 75 |
|
| 76 |
|
| 77 |
# AA: Image - for DF
|
| 78 |
+
##image = sample["image"]
|
| 79 |
|
| 80 |
# Unconditional image captioning
|
| 81 |
inputs = processor(image, return_tensors="pt")
|
|
|
|
| 84 |
caption_eng = processor.decode(out[0], skip_special_tokens=True)
|
| 85 |
|
| 86 |
# Translate caption from English to Spanish
|
| 87 |
+
trans_inputs = trans_tokenizer.encode(caption_eng, return_tensors="pt")
|
| 88 |
+
trans_out = trans_model.generate(trans_inputs)
|
| 89 |
+
caption_es = trans_tokenizer.decode(trans_out[0], skip_special_tokens=True)
|
| 90 |
|
| 91 |
return image, caption_eng, caption_es
|
| 92 |
|
|
|
|
| 96 |
inputs=None,
|
| 97 |
outputs=[
|
| 98 |
gr.Image(type="pil", label="Random Image"),
|
| 99 |
+
gr.Textbox(label="Caption (English)"),
|
| 100 |
+
gr.Textbox(label="Caption (Spanish)")
|
| 101 |
],
|
| 102 |
title="Image Captioning (with English to Spanish translation)",
|
| 103 |
description="Selects a random image (from either the local folder or henryscheible/coco data subset); generates a BLIP caption; then translates the (English) caption to Spanish."
|