Spaces:
Sleeping
Sleeping
Re-entered working code (Select photo from folder, generate caption, then translate.)
Browse files
app.py
CHANGED
|
@@ -2,12 +2,21 @@
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
# Import pandas datasets, transformers, torch
|
| 6 |
import pandas as pd
|
| 7 |
-
#import torch
|
| 8 |
-
#import tensorflow as tf
|
| 9 |
-
from datasets import load_dataset
|
| 10 |
|
|
|
|
| 11 |
|
| 12 |
from transformers import (
|
| 13 |
BlipProcessor,
|
|
@@ -18,13 +27,11 @@ from transformers import (
|
|
| 18 |
MarianTokenizer
|
| 19 |
)
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
from PIL import Image
|
| 25 |
import torch
|
| 26 |
|
| 27 |
|
|
|
|
| 28 |
# Get merve/coco dataset
|
| 29 |
from datasets import load_dataset
|
| 30 |
|
|
@@ -41,9 +48,15 @@ samples = dataset.select(range(20))
|
|
| 41 |
df = pd.DataFrame(samples)
|
| 42 |
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
|
| 49 |
#Load the image captioning model (Salesforce/blip-image-captioning-large)
|
|
@@ -57,20 +70,27 @@ trans_tokenizer = MarianTokenizer.from_pretrained(model_name)
|
|
| 57 |
trans_model = MarianMTModel.from_pretrained(model_name)
|
| 58 |
|
| 59 |
|
|
|
|
| 60 |
|
|
|
|
| 61 |
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
#Configure captioning function
|
| 65 |
-
def caption_random_image():
|
| 66 |
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
# pick random row
|
| 69 |
-
sample = df.sample(1).iloc[0]
|
| 70 |
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
|
| 76 |
# Unconditional image captioning
|
|
@@ -101,12 +121,8 @@ demo = gr.Interface(
|
|
| 101 |
gr.Textbox(label="Caption (Spanish)")
|
| 102 |
],
|
| 103 |
title="Image Captioning (with English to Spanish translation)",
|
| 104 |
-
description="Selects a random
|
| 105 |
)
|
| 106 |
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
demo.launch()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
|
| 4 |
|
| 5 |
+
# Two image datasources are available.
|
| 6 |
+
# Minor adjustments (add/remove # to deactivate/activate) to switch between datasources.
|
| 7 |
+
# AA comments refer to images in the DataFrame / from Coco database
|
| 8 |
+
# BB comments refer to images stored in local Gradio app folder
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Import os and random to support random selection of image (from folder)
|
| 12 |
+
import os
|
| 13 |
+
import random
|
| 14 |
+
|
| 15 |
+
|
| 16 |
# Import pandas datasets, transformers, torch
|
| 17 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
from datasets import load_dataset
|
| 20 |
|
| 21 |
from transformers import (
|
| 22 |
BlipProcessor,
|
|
|
|
| 27 |
MarianTokenizer
|
| 28 |
)
|
| 29 |
|
|
|
|
|
|
|
|
|
|
| 30 |
from PIL import Image
|
| 31 |
import torch
|
| 32 |
|
| 33 |
|
| 34 |
+
# AA: Load dataset. Initial image source.
|
| 35 |
# Get merve/coco dataset
|
| 36 |
from datasets import load_dataset
|
| 37 |
|
|
|
|
| 48 |
df = pd.DataFrame(samples)
|
| 49 |
|
| 50 |
|
| 51 |
+
# BB: Direct to Photos folder
|
| 52 |
+
IMAGE_FOLDER = "Photos"
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
image_paths = [
|
| 56 |
+
os.path.join(IMAGE_FOLDER, f)
|
| 57 |
+
for f in os.listdir(IMAGE_FOLDER)
|
| 58 |
+
if f.lower().endswith((".jpg", ".jpeg", ".png"))
|
| 59 |
+
]
|
| 60 |
|
| 61 |
|
| 62 |
#Load the image captioning model (Salesforce/blip-image-captioning-large)
|
|
|
|
| 70 |
trans_model = MarianMTModel.from_pretrained(model_name)
|
| 71 |
|
| 72 |
|
| 73 |
+
#Configure captioning function
|
| 74 |
|
| 75 |
+
def caption_random_image():
|
| 76 |
|
| 77 |
|
| 78 |
+
# AA: pick random row - from DF
|
| 79 |
+
##sample = df.sample(1).iloc[0]
|
| 80 |
+
|
| 81 |
+
|
| 82 |
+
# BB: Pick a random image path - image from folder
|
| 83 |
+
img_path = random.choice(image_paths)
|
| 84 |
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# BB: Load into PIL - image from folder - image from folder
|
| 87 |
+
image = Image.open(img_path).convert("RGB")
|
| 88 |
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
+
|
| 92 |
+
# AA: Image - for DF
|
| 93 |
+
##image = sample["image"]
|
| 94 |
|
| 95 |
|
| 96 |
# Unconditional image captioning
|
|
|
|
| 121 |
gr.Textbox(label="Caption (Spanish)")
|
| 122 |
],
|
| 123 |
title="Image Captioning (with English to Spanish translation)",
|
| 124 |
+
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."
|
| 125 |
)
|
| 126 |
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
demo.launch()
|