Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from tts_module import get_voices, text_to_speech
|
| 3 |
-
from
|
| 4 |
from moviepy.editor import (
|
| 5 |
AudioFileClip, VideoFileClip, CompositeAudioClip,
|
| 6 |
concatenate_audioclips, concatenate_videoclips, vfx, CompositeVideoClip
|
|
@@ -15,29 +15,22 @@ import tempfile
|
|
| 15 |
import re
|
| 16 |
import random
|
| 17 |
|
| 18 |
-
# Define la carpeta de salida temporal
|
| 19 |
output_folder = "outputs"
|
| 20 |
os.makedirs(output_folder, exist_ok=True)
|
| 21 |
|
| 22 |
def clean_text_for_search(text):
|
| 23 |
-
"""Limpia el texto para hacer b煤squedas v谩lidas en Pexels"""
|
| 24 |
-
# Eliminar caracteres especiales y limitar longitud
|
| 25 |
text = re.sub(r'[^\w\s]', '', text).strip()
|
| 26 |
return text
|
| 27 |
|
| 28 |
def resize_and_blur_video(clip, target_aspect_ratio=16/9):
|
| 29 |
-
"""
|
| 30 |
-
Redimensiona y aplica desenfoque al fondo del video para mantener el aspecto 16:9.
|
| 31 |
-
"""
|
| 32 |
try:
|
| 33 |
w, h = clip.size
|
| 34 |
current_aspect_ratio = w / h
|
| 35 |
|
| 36 |
-
print(f"Procesando video: {w}x{h}, ratio: {current_aspect_ratio}")
|
| 37 |
if abs(current_aspect_ratio - target_aspect_ratio) < 0.1:
|
| 38 |
return clip
|
| 39 |
|
| 40 |
-
if current_aspect_ratio < target_aspect_ratio:
|
| 41 |
target_w = int(h * target_aspect_ratio)
|
| 42 |
target_h = h
|
| 43 |
|
|
@@ -53,18 +46,14 @@ def resize_and_blur_video(clip, target_aspect_ratio=16/9):
|
|
| 53 |
[background, foreground.set_position((x_center, 0))],
|
| 54 |
size=(target_w, target_h)
|
| 55 |
)
|
| 56 |
-
else:
|
| 57 |
return clip.resize(width=int(h * target_aspect_ratio), height=h)
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
print(f"Error en resize_and_blur_video: {e}")
|
| 61 |
return clip
|
| 62 |
|
| 63 |
-
def
|
| 64 |
-
"""
|
| 65 |
-
Concatena videos de Pexels basados en palabras clave proporcionadas por el usuario.
|
| 66 |
-
:param keywords: Palabras clave separadas por comas (ejemplo: "universo, galaxia, bosque, gato").
|
| 67 |
-
"""
|
| 68 |
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
|
| 69 |
if not keyword_list:
|
| 70 |
raise Exception("No se proporcionaron palabras clave v谩lidas.")
|
|
@@ -73,13 +62,12 @@ def concatenate_pexels_videos(keywords, num_videos_per_keyword=1):
|
|
| 73 |
|
| 74 |
for keyword in keyword_list:
|
| 75 |
try:
|
| 76 |
-
|
| 77 |
-
links = search_pexels(keyword, num_results=num_videos_per_keyword)
|
| 78 |
if not links:
|
| 79 |
print(f"No se encontraron videos para la palabra clave '{keyword}'.")
|
| 80 |
continue
|
| 81 |
|
| 82 |
-
link = links[0]
|
| 83 |
video_response = requests.get(link)
|
| 84 |
if video_response.status_code != 200:
|
| 85 |
print(f"Error al descargar video desde {link}: C贸digo de estado {video_response.status_code}")
|
|
@@ -98,9 +86,7 @@ def concatenate_pexels_videos(keywords, num_videos_per_keyword=1):
|
|
| 98 |
if not video_clips:
|
| 99 |
raise Exception("No se pudieron obtener videos v谩lidos.")
|
| 100 |
|
| 101 |
-
# Aleatorizar el orden de los clips si es necesario
|
| 102 |
random.shuffle(video_clips)
|
| 103 |
-
|
| 104 |
return concatenate_videoclips(video_clips, method="compose")
|
| 105 |
|
| 106 |
def adjust_background_music(video_duration, music_file):
|
|
@@ -165,7 +151,7 @@ def process_input(text, txt_file, mp3_file, selected_voice, rate, pitch, keyword
|
|
| 165 |
return f"Error generando audio: {e}"
|
| 166 |
|
| 167 |
try:
|
| 168 |
-
video_clip =
|
| 169 |
except Exception as e:
|
| 170 |
return f"Error concatenando videos: {e}"
|
| 171 |
|
|
@@ -198,7 +184,6 @@ def upload_to_google_drive(file_path):
|
|
| 198 |
print(f"Error subiendo a Google Drive: {e}")
|
| 199 |
return None
|
| 200 |
|
| 201 |
-
# Interfaz Gradio
|
| 202 |
with gr.Blocks() as demo:
|
| 203 |
gr.Markdown("# Text-to-Video Generator")
|
| 204 |
with gr.Row():
|
|
@@ -221,8 +206,5 @@ with gr.Blocks() as demo:
|
|
| 221 |
outputs=output_video
|
| 222 |
)
|
| 223 |
|
| 224 |
-
# Leer el puerto asignado por Hugging Face
|
| 225 |
port = int(os.getenv("PORT", 7860))
|
| 226 |
-
|
| 227 |
-
# Lanzar la aplicaci贸n
|
| 228 |
demo.launch(server_name="0.0.0.0", server_port=port, share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from tts_module import get_voices, text_to_speech
|
| 3 |
+
from pixabay_api import search_pixabay
|
| 4 |
from moviepy.editor import (
|
| 5 |
AudioFileClip, VideoFileClip, CompositeAudioClip,
|
| 6 |
concatenate_audioclips, concatenate_videoclips, vfx, CompositeVideoClip
|
|
|
|
| 15 |
import re
|
| 16 |
import random
|
| 17 |
|
|
|
|
| 18 |
output_folder = "outputs"
|
| 19 |
os.makedirs(output_folder, exist_ok=True)
|
| 20 |
|
| 21 |
def clean_text_for_search(text):
|
|
|
|
|
|
|
| 22 |
text = re.sub(r'[^\w\s]', '', text).strip()
|
| 23 |
return text
|
| 24 |
|
| 25 |
def resize_and_blur_video(clip, target_aspect_ratio=16/9):
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
w, h = clip.size
|
| 28 |
current_aspect_ratio = w / h
|
| 29 |
|
|
|
|
| 30 |
if abs(current_aspect_ratio - target_aspect_ratio) < 0.1:
|
| 31 |
return clip
|
| 32 |
|
| 33 |
+
if current_aspect_ratio < target_aspect_ratio:
|
| 34 |
target_w = int(h * target_aspect_ratio)
|
| 35 |
target_h = h
|
| 36 |
|
|
|
|
| 46 |
[background, foreground.set_position((x_center, 0))],
|
| 47 |
size=(target_w, target_h)
|
| 48 |
)
|
| 49 |
+
else:
|
| 50 |
return clip.resize(width=int(h * target_aspect_ratio), height=h)
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
print(f"Error en resize_and_blur_video: {e}")
|
| 54 |
return clip
|
| 55 |
|
| 56 |
+
def concatenate_pixabay_videos(keywords, num_videos_per_keyword=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
keyword_list = [keyword.strip() for keyword in keywords.split(",") if keyword.strip()]
|
| 58 |
if not keyword_list:
|
| 59 |
raise Exception("No se proporcionaron palabras clave v谩lidas.")
|
|
|
|
| 62 |
|
| 63 |
for keyword in keyword_list:
|
| 64 |
try:
|
| 65 |
+
links = search_pixabay(keyword, num_results=num_videos_per_keyword)
|
|
|
|
| 66 |
if not links:
|
| 67 |
print(f"No se encontraron videos para la palabra clave '{keyword}'.")
|
| 68 |
continue
|
| 69 |
|
| 70 |
+
link = links[0]
|
| 71 |
video_response = requests.get(link)
|
| 72 |
if video_response.status_code != 200:
|
| 73 |
print(f"Error al descargar video desde {link}: C贸digo de estado {video_response.status_code}")
|
|
|
|
| 86 |
if not video_clips:
|
| 87 |
raise Exception("No se pudieron obtener videos v谩lidos.")
|
| 88 |
|
|
|
|
| 89 |
random.shuffle(video_clips)
|
|
|
|
| 90 |
return concatenate_videoclips(video_clips, method="compose")
|
| 91 |
|
| 92 |
def adjust_background_music(video_duration, music_file):
|
|
|
|
| 151 |
return f"Error generando audio: {e}"
|
| 152 |
|
| 153 |
try:
|
| 154 |
+
video_clip = concatenate_pixabay_videos(keywords, num_videos_per_keyword=1)
|
| 155 |
except Exception as e:
|
| 156 |
return f"Error concatenando videos: {e}"
|
| 157 |
|
|
|
|
| 184 |
print(f"Error subiendo a Google Drive: {e}")
|
| 185 |
return None
|
| 186 |
|
|
|
|
| 187 |
with gr.Blocks() as demo:
|
| 188 |
gr.Markdown("# Text-to-Video Generator")
|
| 189 |
with gr.Row():
|
|
|
|
| 206 |
outputs=output_video
|
| 207 |
)
|
| 208 |
|
|
|
|
| 209 |
port = int(os.getenv("PORT", 7860))
|
|
|
|
|
|
|
| 210 |
demo.launch(server_name="0.0.0.0", server_port=port, share=True)
|