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