Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -1,128 +1,21 @@
|
|
1 |
import gradio as gr
|
2 |
-
import
|
3 |
-
import torch
|
4 |
-
from transformers import MarianMTModel, MarianTokenizer
|
5 |
-
import ffmpeg
|
6 |
-
import requests
|
7 |
-
import os
|
8 |
-
import tempfile
|
9 |
-
import shutil
|
10 |
|
11 |
-
#
|
12 |
-
|
13 |
-
ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY") # Defina essa variável no Hugging Face Spaces
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
|
20 |
-
#
|
21 |
-
def transcribe_audio(video_path: str) -> str:
|
22 |
-
try:
|
23 |
-
result = whisper_model.transcribe(video_path)
|
24 |
-
return result["text"]
|
25 |
-
except Exception as e:
|
26 |
-
return f"Erro na transcrição: {str(e)}"
|
27 |
-
|
28 |
-
# Função para traduzir texto
|
29 |
-
def translate_text(text: str, target_lang="pt") -> str:
|
30 |
-
try:
|
31 |
-
inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
|
32 |
-
translated_tokens = translator.generate(**inputs)
|
33 |
-
return tokenizer.decode(translated_tokens[0], skip_special_tokens=True)
|
34 |
-
except Exception as e:
|
35 |
-
return f"Erro na tradução: {str(e)}"
|
36 |
-
|
37 |
-
# Função para gerar áudio em português (usando ElevenLabs)
|
38 |
-
def synthesize_speech(text: str, voice="Antônio") -> str:
|
39 |
-
try:
|
40 |
-
url = "https://api.elevenlabs.io/v1/text-to-speech"
|
41 |
-
headers = {"Authorization": f"Bearer {ELEVENLABS_API_KEY}"}
|
42 |
-
response = requests.post(url, json={"text": text, "voice": voice}, headers=headers)
|
43 |
-
|
44 |
-
if response.status_code != 200:
|
45 |
-
return f"Erro na geração de voz: {response.text}"
|
46 |
-
|
47 |
-
temp_audio = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
|
48 |
-
with open(temp_audio.name, "wb") as f:
|
49 |
-
f.write(response.content)
|
50 |
-
return temp_audio.name
|
51 |
-
except Exception as e:
|
52 |
-
return f"Erro na síntese de voz: {str(e)}"
|
53 |
-
|
54 |
-
# Função para substituir o áudio no vídeo
|
55 |
-
def replace_audio(video_path: str, new_audio_path: str) -> str:
|
56 |
-
try:
|
57 |
-
output_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
58 |
-
ffmpeg.input(video_path).output(
|
59 |
-
output_path,
|
60 |
-
audio=new_audio_path,
|
61 |
-
codec="copy"
|
62 |
-
).run(overwrite_output=True)
|
63 |
-
|
64 |
-
return output_path
|
65 |
-
except Exception as e:
|
66 |
-
return f"Erro na substituição do áudio: {str(e)}"
|
67 |
-
|
68 |
-
# Função para mover o arquivo gerado para um local público
|
69 |
-
def move_video_to_public(output_video_path: str) -> str:
|
70 |
-
try:
|
71 |
-
public_path = f"/app/public/{os.path.basename(output_video_path)}" # Ajuste para o ambiente de produção
|
72 |
-
shutil.move(output_video_path, public_path)
|
73 |
-
return public_path
|
74 |
-
except Exception as e:
|
75 |
-
return f"Erro ao mover o vídeo para o diretório público: {str(e)}"
|
76 |
-
|
77 |
-
# Pipeline completo
|
78 |
-
def process_video(video_file):
|
79 |
-
try:
|
80 |
-
# Verifique se o arquivo tem o método 'read' e é do tipo esperado
|
81 |
-
if not hasattr(video_file, 'read'):
|
82 |
-
return "Erro: O arquivo fornecido não é válido."
|
83 |
-
|
84 |
-
# Salve o arquivo temporariamente
|
85 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp4") as temp_video:
|
86 |
-
temp_video.write(video_file.read()) # Lê o conteúdo do arquivo de entrada
|
87 |
-
video_path = temp_video.name
|
88 |
-
|
89 |
-
# Passo 1: Transcrição do áudio
|
90 |
-
transcript = transcribe_audio(video_path)
|
91 |
-
if "Erro" in transcript:
|
92 |
-
return transcript
|
93 |
-
|
94 |
-
# Passo 2: Tradução do texto
|
95 |
-
translated_text = translate_text(transcript)
|
96 |
-
if "Erro" in translated_text:
|
97 |
-
return translated_text
|
98 |
-
|
99 |
-
# Passo 3: Síntese de fala em português
|
100 |
-
new_audio_path = synthesize_speech(translated_text)
|
101 |
-
if "Erro" in new_audio_path:
|
102 |
-
return new_audio_path
|
103 |
-
|
104 |
-
# Passo 4: Substituição do áudio no vídeo
|
105 |
-
output_video_path = replace_audio(video_path, new_audio_path)
|
106 |
-
if "Erro" in output_video_path:
|
107 |
-
return output_video_path
|
108 |
-
|
109 |
-
# Passo 5: Mover o vídeo para um local público (link acessível)
|
110 |
-
public_video_path = move_video_to_public(output_video_path)
|
111 |
-
if "Erro" in public_video_path:
|
112 |
-
return public_video_path
|
113 |
-
|
114 |
-
# Retorna o link público para o vídeo
|
115 |
-
return f"Vídeo processado com sucesso! Você pode visualizar o vídeo no seguinte link: [Clique aqui para ver o vídeo](/{os.path.basename(public_video_path)})"
|
116 |
-
except Exception as e:
|
117 |
-
return f"Erro inesperado: {str(e)}"
|
118 |
-
|
119 |
-
# Interface Gradio
|
120 |
iface = gr.Interface(
|
121 |
-
fn=
|
122 |
-
inputs=gr.
|
123 |
-
outputs=gr.
|
124 |
-
title="Tradutor de Vídeos para Português",
|
125 |
-
description="Faz a transcrição, tradução e substituição de áudio em vídeos automaticamente."
|
126 |
)
|
127 |
|
128 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
+
# Carrega o modelo (modifique para o modelo que você usa)
|
5 |
+
modelo = pipeline("text-generation", model="gpt2")
|
|
|
6 |
|
7 |
+
# Função para processar a entrada do usuário
|
8 |
+
def gerar_texto(entrada):
|
9 |
+
resultado = modelo(entrada, max_length=50)
|
10 |
+
return resultado[0]["generated_text"]
|
11 |
|
12 |
+
# Interface do Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
iface = gr.Interface(
|
14 |
+
fn=gerar_texto,
|
15 |
+
inputs=gr.Textbox(label="Digite seu texto"),
|
16 |
+
outputs=gr.Textbox(label="Texto Gerado"),
|
|
|
|
|
17 |
)
|
18 |
|
19 |
+
# Lançar app (sem `share=True`)
|
20 |
+
iface.launch()
|
21 |
+
|