Spaces:
Running
Running
Added the transformer to translate text from English to Spanish
Browse files
app.py
CHANGED
|
@@ -5,7 +5,13 @@ import gradio as gr
|
|
| 5 |
import pandas as pd
|
| 6 |
from datasets import load_dataset
|
| 7 |
|
| 8 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
from PIL import Image
|
| 10 |
import torch
|
| 11 |
|
|
@@ -29,6 +35,10 @@ df = pd.DataFrame(samples)
|
|
| 29 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 30 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
#Configure captioning function
|
| 33 |
def caption_random_image():
|
| 34 |
|
|
@@ -42,17 +52,26 @@ def caption_random_image():
|
|
| 42 |
inputs = processor(image, return_tensors="pt")
|
| 43 |
|
| 44 |
out = model.generate(**inputs)
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
return image,
|
| 48 |
|
| 49 |
|
| 50 |
demo = gr.Interface(
|
| 51 |
fn=caption_random_image,
|
| 52 |
inputs=None,
|
| 53 |
-
outputs=[
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
)
|
| 57 |
|
| 58 |
|
|
|
|
| 5 |
import pandas as pd
|
| 6 |
from datasets import load_dataset
|
| 7 |
|
| 8 |
+
from transformers import (
|
| 9 |
+
BlipProcessor,
|
| 10 |
+
BlipForConditionalGeneration,
|
| 11 |
+
AutoTokenizer,
|
| 12 |
+
AutoModelForSeq2SeqLM
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
from PIL import Image
|
| 16 |
import torch
|
| 17 |
|
|
|
|
| 35 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 36 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 37 |
|
| 38 |
+
#Load transformer for translating captions from English to Spanish
|
| 39 |
+
trans_tokenizer = AutoTokenizer.from_pretrained("Abhra-loony/english-to-spanish-lang-translation-model")
|
| 40 |
+
trans_model = AutoModelForSeq2SeqLM.from_pretrained("Abhra-loony/english-to-spanish-lang-translation-model")
|
| 41 |
+
|
| 42 |
#Configure captioning function
|
| 43 |
def caption_random_image():
|
| 44 |
|
|
|
|
| 52 |
inputs = processor(image, return_tensors="pt")
|
| 53 |
|
| 54 |
out = model.generate(**inputs)
|
| 55 |
+
caption_eng = processor.decode(out[0], skip_special_tokens=True)
|
| 56 |
+
|
| 57 |
+
# Translate caption from English to Spanish
|
| 58 |
+
trans_inputs = trans_tokenizer.encode(caption_en, return_tensors="pt")
|
| 59 |
+
trans_out = trans_model.generate(trans_inputs)
|
| 60 |
+
caption_es = trans_tokenizer.decode(trans_out[0], skip_special_tokens=True)
|
| 61 |
|
| 62 |
+
return image, caption_eng, caption_es
|
| 63 |
|
| 64 |
|
| 65 |
demo = gr.Interface(
|
| 66 |
fn=caption_random_image,
|
| 67 |
inputs=None,
|
| 68 |
+
outputs=[
|
| 69 |
+
gr.Image(type="pil", label="Random Image"),
|
| 70 |
+
gr.Textbox(label="Caption (English)"),
|
| 71 |
+
gr.Textbox(label="Caption (Spanish)")
|
| 72 |
+
],
|
| 73 |
+
title="Image Captioning (with English to Spanish translation)",
|
| 74 |
+
description="Selects a random COCO image from 20 samples; generates a BLIP caption; then translates the (English) caption to Spanish."
|
| 75 |
)
|
| 76 |
|
| 77 |
|