Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ blip_model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image
|
|
11 |
translator_tokenizer = NllbTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
|
12 |
translator_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
|
13 |
lang_code = "vie_Latn"
|
|
|
14 |
|
15 |
def caption_translate(image):
|
16 |
# Step 1: Get English caption
|
@@ -19,11 +20,15 @@ def caption_translate(image):
|
|
19 |
eng_caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
20 |
|
21 |
# Step 2: Translate to Vietnamese
|
22 |
-
inputs = translator_tokenizer(eng_caption, return_tensors="pt", src_lang="eng_Latn"
|
23 |
-
translated = translator_model.generate(
|
|
|
|
|
|
|
|
|
24 |
vi_caption = translator_tokenizer.decode(translated[0], skip_special_tokens=True)
|
25 |
|
26 |
-
return
|
27 |
|
28 |
iface = gr.Interface(
|
29 |
fn=caption_translate,
|
|
|
11 |
translator_tokenizer = NllbTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
|
12 |
translator_model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
|
13 |
lang_code = "vie_Latn"
|
14 |
+
lang_token_id = translator_tokenizer.lang_code_to_id[lang_code]
|
15 |
|
16 |
def caption_translate(image):
|
17 |
# Step 1: Get English caption
|
|
|
20 |
eng_caption = blip_processor.decode(out[0], skip_special_tokens=True)
|
21 |
|
22 |
# Step 2: Translate to Vietnamese
|
23 |
+
inputs = translator_tokenizer(eng_caption, return_tensors="pt", src_lang="eng_Latn")
|
24 |
+
translated = translator_model.generate(
|
25 |
+
**inputs,
|
26 |
+
forced_bos_token_id=lang_token_id,
|
27 |
+
max_length=100
|
28 |
+
)
|
29 |
vi_caption = translator_tokenizer.decode(translated[0], skip_special_tokens=True)
|
30 |
|
31 |
+
return vi_caption
|
32 |
|
33 |
iface = gr.Interface(
|
34 |
fn=caption_translate,
|