Spaces:
Sleeping
Sleeping
mileski-dev
commited on
Commit
·
a214e53
1
Parent(s):
7309205
ajustar para pt-br com helsinki 3
Browse files
app.py
CHANGED
@@ -7,8 +7,9 @@ from transformers import BlipProcessor, BlipForConditionalGeneration, pipeline
|
|
7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
|
10 |
-
# 2) Carrega pipeline de tradução Inglês → Português
|
11 |
-
|
|
|
12 |
|
13 |
def infer_caption(image):
|
14 |
# 1) Redimensiona mantendo proporção para max 1024×1024
|
@@ -24,20 +25,20 @@ def infer_caption(image):
|
|
24 |
outputs = model.generate(**inputs)
|
25 |
english_caption = processor.decode(outputs[0], skip_special_tokens=True)
|
26 |
|
27 |
-
# 3) Traduz para pt
|
28 |
translated = translator(english_caption, max_length=128)
|
29 |
portuguese_caption = translated[0]["translation_text"]
|
30 |
|
31 |
return portuguese_caption
|
32 |
|
33 |
-
#
|
34 |
interface = gr.Interface(
|
35 |
fn=infer_caption,
|
36 |
inputs=gr.Image(type="pil"),
|
37 |
outputs="text",
|
38 |
title="Ouvir Para Ver",
|
39 |
-
description="Envie uma imagem e receba a descrição."
|
40 |
)
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
-
interface.launch(server_name="0.0.0.0", share=False)
|
|
|
7 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-base")
|
8 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
9 |
|
10 |
+
# 2) Carrega pipeline de tradução Inglês → Português
|
11 |
+
# usando o modelo Helsinki-NLP/opus-mt-tc-big-en-pt
|
12 |
+
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-big-en-pt")
|
13 |
|
14 |
def infer_caption(image):
|
15 |
# 1) Redimensiona mantendo proporção para max 1024×1024
|
|
|
25 |
outputs = model.generate(**inputs)
|
26 |
english_caption = processor.decode(outputs[0], skip_special_tokens=True)
|
27 |
|
28 |
+
# 3) Traduz para português (pt)
|
29 |
translated = translator(english_caption, max_length=128)
|
30 |
portuguese_caption = translated[0]["translation_text"]
|
31 |
|
32 |
return portuguese_caption
|
33 |
|
34 |
+
# 4) Cria interface Gradio
|
35 |
interface = gr.Interface(
|
36 |
fn=infer_caption,
|
37 |
inputs=gr.Image(type="pil"),
|
38 |
outputs="text",
|
39 |
title="Ouvir Para Ver",
|
40 |
+
description="Envie uma imagem e receba a descrição em português brasileiro."
|
41 |
)
|
42 |
|
43 |
if __name__ == "__main__":
|
44 |
+
interface.launch(server_name="0.0.0.0", share=False)
|