Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,67 +1,66 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
from transformers import AutoProcessor, AutoModelForCausalLM, MarianMTModel, MarianTokenizer
|
3 |
-
from PIL import Image
|
4 |
-
import torch
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
tts
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
demo.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoProcessor, AutoModelForCausalLM, MarianMTModel, MarianTokenizer
|
3 |
+
from PIL import Image
|
4 |
+
import torch
|
5 |
+
from gtts import gTTS
|
6 |
+
import os
|
7 |
+
|
8 |
+
# Funções auxiliares
|
9 |
+
def prepare_image(image_path):
|
10 |
+
image = Image.open(image_path).convert("RGB")
|
11 |
+
inputs = processor(images=image, return_tensors="pt").to(device)
|
12 |
+
return image, inputs.pixel_values
|
13 |
+
|
14 |
+
def generate_caption(pixel_values):
|
15 |
+
model.eval()
|
16 |
+
with torch.no_grad():
|
17 |
+
generated_ids = model.generate(
|
18 |
+
pixel_values=pixel_values,
|
19 |
+
max_length=50,
|
20 |
+
num_beams=4,
|
21 |
+
early_stopping=True,
|
22 |
+
no_repeat_ngram_size=2
|
23 |
+
)
|
24 |
+
return processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
25 |
+
|
26 |
+
def translate_to_portuguese(text):
|
27 |
+
inputs = translation_tokenizer(text, return_tensors="pt", truncation=True).to(device)
|
28 |
+
translated_ids = translation_model.generate(inputs["input_ids"], max_length=50, num_beams=4, early_stopping=True)
|
29 |
+
return translation_tokenizer.batch_decode(translated_ids, skip_special_tokens=True)[0]
|
30 |
+
|
31 |
+
def text_to_speech_gtts(text, lang='pt'):
|
32 |
+
tts = gTTS(text=text, lang=lang)
|
33 |
+
tts.save("output.mp3")
|
34 |
+
return "output.mp3"
|
35 |
+
|
36 |
+
# Carregar os modelos
|
37 |
+
processor = AutoProcessor.from_pretrained("microsoft/git-base")
|
38 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/git-base")
|
39 |
+
translation_model_name = 'Helsinki-NLP/opus-mt-tc-big-en-pt'
|
40 |
+
translation_tokenizer = MarianTokenizer.from_pretrained(translation_model_name)
|
41 |
+
translation_model = MarianMTModel.from_pretrained(translation_model_name)
|
42 |
+
|
43 |
+
# Configurar o dispositivo (GPU ou CPU)
|
44 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
45 |
+
model.to(device)
|
46 |
+
translation_model.to(device)
|
47 |
+
|
48 |
+
# Função principal para processar a imagem e gerar a voz
|
49 |
+
def process_image(image):
|
50 |
+
_, pixel_values = prepare_image(image)
|
51 |
+
caption_en = generate_caption(pixel_values)
|
52 |
+
caption_pt = translate_to_portuguese(caption_en)
|
53 |
+
audio_file = text_to_speech_gtts(caption_pt)
|
54 |
+
return caption_pt, audio_file
|
55 |
+
|
56 |
+
# Interface Gradio
|
57 |
+
iface = gr.Interface(
|
58 |
+
fn=process_image,
|
59 |
+
inputs=gr.Image(type="filepath"),
|
60 |
+
outputs=[gr.Textbox(), gr.Audio(type="filepath")],
|
61 |
+
title="Image to Voice",
|
62 |
+
description="Gera uma descrição em português e a converte em voz a partir de uma imagem."
|
63 |
+
)
|
64 |
+
|
65 |
+
if __name__ == "__main__":
|
66 |
+
iface.launch()
|
|