Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +74 -0
- requirements.txt +10 -0
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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("Guspfc/git-base-captioning")
|
38 |
+
model = AutoModelForCausalLM.from_pretrained("Guspfc/git-base-captioning")
|
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 |
+
# Caminhos para as imagens de exemplo (supondo que estejam no mesmo diretório que o script)
|
57 |
+
example_image_paths = [
|
58 |
+
"example1.png",
|
59 |
+
"example2.png",
|
60 |
+
"example3.png"
|
61 |
+
]
|
62 |
+
|
63 |
+
# Interface Gradio
|
64 |
+
iface = gr.Interface(
|
65 |
+
fn=process_image,
|
66 |
+
inputs=gr.Image(type="filepath"),
|
67 |
+
outputs=[gr.Textbox(), gr.Audio(type="filepath")],
|
68 |
+
examples=example_image_paths,
|
69 |
+
title="Image to Voice",
|
70 |
+
description="Gera uma descrição em português e a converte em voz a partir de uma imagem."
|
71 |
+
)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
datasets
|
3 |
+
sentencepiece
|
4 |
+
gtts
|
5 |
+
IPython
|
6 |
+
gradio
|
7 |
+
torch
|
8 |
+
sacremoses
|
9 |
+
Pillow
|
10 |
+
huggingface_hub
|