histlearn commited on
Commit
0037215
1 Parent(s): 0018436

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -67
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
- 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()
 
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()