Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import google.generativeai as genai
|
| 3 |
from gtts import gTTS
|
| 4 |
import os
|
| 5 |
import cv2
|
|
@@ -8,21 +7,7 @@ from pydub import AudioSegment
|
|
| 8 |
import subprocess
|
| 9 |
import shutil
|
| 10 |
|
| 11 |
-
GEMINI_API_KEY
|
| 12 |
-
if not GEMINI_API_KEY:
|
| 13 |
-
print("Advertencia: La GEMINI_API_KEY no est谩 configurada como secreto en Hugging Face. La generaci贸n de texto con Gemini no estar谩 disponible.")
|
| 14 |
-
else:
|
| 15 |
-
genai.configure(api_key=GEMINI_API_KEY)
|
| 16 |
-
|
| 17 |
-
def generate_news_with_gemini(prompt):
|
| 18 |
-
if not GEMINI_API_KEY:
|
| 19 |
-
return "Error: GEMINI_API_KEY no est谩 configurada. No se puede generar texto."
|
| 20 |
-
try:
|
| 21 |
-
model = genai.GenerativeModel('gemini-pro')
|
| 22 |
-
response = model.generate_content(prompt)
|
| 23 |
-
return response.text
|
| 24 |
-
except Exception as e:
|
| 25 |
-
return f"Error al generar la noticia con Gemini: {e}"
|
| 26 |
|
| 27 |
def text_to_speech(text, output_filename="audio.mp3"):
|
| 28 |
try:
|
|
@@ -139,21 +124,12 @@ def combine_video_and_audio_ffmpeg(video_path, audio_path, output_path):
|
|
| 139 |
print("DEBUG: FFmpeg not found during combine.")
|
| 140 |
raise Exception("Error: FFmpeg no encontrado. Aseg煤rate de que est茅 instalado en el entorno.")
|
| 141 |
|
| 142 |
-
def generate_tts_only(news_text_input
|
| 143 |
-
news_content =
|
| 144 |
temp_audio_file = "audio_temp_preview.mp3"
|
| 145 |
|
| 146 |
-
if news_text_input:
|
| 147 |
-
news_content = news_text_input
|
| 148 |
-
elif gemini_prompt:
|
| 149 |
-
news_content = generate_news_with_gemini(gemini_prompt)
|
| 150 |
-
if "Error" in news_content:
|
| 151 |
-
return news_content, None
|
| 152 |
-
else:
|
| 153 |
-
return "Por favor, escribe una noticia o proporciona un prompt para Gemini.", None
|
| 154 |
-
|
| 155 |
if not news_content:
|
| 156 |
-
return "
|
| 157 |
|
| 158 |
try:
|
| 159 |
text_to_speech(news_content, output_filename=temp_audio_file)
|
|
@@ -161,8 +137,8 @@ def generate_tts_only(news_text_input, gemini_prompt):
|
|
| 161 |
except Exception as e:
|
| 162 |
return f"Ocurri贸 un error al generar solo el audio: {e}", None
|
| 163 |
|
| 164 |
-
def create_news_video_app(news_text_input,
|
| 165 |
-
news_content =
|
| 166 |
output_video_path = None
|
| 167 |
error_message = ""
|
| 168 |
|
|
@@ -176,19 +152,10 @@ def create_news_video_app(news_text_input, gemini_prompt, image_files, video_rat
|
|
| 176 |
os.makedirs(processed_image_folder, exist_ok=True)
|
| 177 |
|
| 178 |
try:
|
|
|
|
|
|
|
|
|
|
| 179 |
if not input_audio_file or not os.path.exists(input_audio_file):
|
| 180 |
-
if news_text_input:
|
| 181 |
-
news_content = news_text_input
|
| 182 |
-
elif gemini_prompt:
|
| 183 |
-
news_content = generate_news_with_gemini(gemini_prompt)
|
| 184 |
-
if "Error" in news_content:
|
| 185 |
-
return news_content, None
|
| 186 |
-
else:
|
| 187 |
-
return "Por favor, escribe una noticia, proporciona un prompt para Gemini, o genera el audio primero.", None
|
| 188 |
-
|
| 189 |
-
if not news_content:
|
| 190 |
-
return "No se pudo obtener el contenido de la noticia. Intenta con un prompt diferente o escribe directamente.", None
|
| 191 |
-
|
| 192 |
text_to_speech(news_content, output_filename=temp_audio_file)
|
| 193 |
|
| 194 |
audio_duration = get_audio_duration(temp_audio_file)
|
|
@@ -224,11 +191,10 @@ def create_news_video_app(news_text_input, gemini_prompt, image_files, video_rat
|
|
| 224 |
print(f"DEBUG: Error caught in main app function: {e}")
|
| 225 |
return error_message, None
|
| 226 |
finally:
|
| 227 |
-
# Clean up all temporary files
|
| 228 |
if os.path.exists(temp_audio_file) and temp_audio_file != input_audio_file:
|
| 229 |
os.remove(temp_audio_file)
|
| 230 |
if os.path.exists("audio_temp_preview.mp3"):
|
| 231 |
-
os.remove("audio_temp_preview.mp3")
|
| 232 |
if os.path.exists(temp_video_no_audio):
|
| 233 |
os.remove(temp_video_no_audio)
|
| 234 |
if os.path.exists(processed_image_folder):
|
|
@@ -239,9 +205,8 @@ def create_news_video_app(news_text_input, gemini_prompt, image_files, video_rat
|
|
| 239 |
with gr.Blocks() as demo:
|
| 240 |
gr.Markdown(
|
| 241 |
"""
|
| 242 |
-
# 馃帴 Creador de Videos de Noticias con
|
| 243 |
-
Escribe una noticia
|
| 244 |
-
Puedes escribir la noticia directamente o generarla con tu **GEMINI_API_KEY**.
|
| 245 |
"""
|
| 246 |
)
|
| 247 |
|
|
@@ -253,13 +218,8 @@ with gr.Blocks() as demo:
|
|
| 253 |
lines=5,
|
| 254 |
value=""
|
| 255 |
)
|
| 256 |
-
|
| 257 |
-
label="O genera la noticia con Gemini (escribe un prompt)",
|
| 258 |
-
placeholder="Ej: Escribe una noticia sobre un nuevo r茅cord mundial de ajedrez.",
|
| 259 |
-
value=""
|
| 260 |
-
)
|
| 261 |
|
| 262 |
-
# Bot贸n intermedio para generar solo audio
|
| 263 |
generate_audio_button = gr.Button("1. Generar Solo Audio")
|
| 264 |
audio_output_preview = gr.Audio(label="Audio de Noticia (Vista Previa)", interactive=False)
|
| 265 |
audio_status_message = gr.Textbox(label="Estado del Audio", interactive=False)
|
|
@@ -284,13 +244,13 @@ with gr.Blocks() as demo:
|
|
| 284 |
|
| 285 |
generate_audio_button.click(
|
| 286 |
fn=generate_tts_only,
|
| 287 |
-
inputs=[news_input,
|
| 288 |
outputs=[audio_status_message, audio_output_preview]
|
| 289 |
)
|
| 290 |
|
| 291 |
generate_video_button.click(
|
| 292 |
fn=create_news_video_app,
|
| 293 |
-
inputs=[news_input,
|
| 294 |
outputs=[output_message, video_output]
|
| 295 |
)
|
| 296 |
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from gtts import gTTS
|
| 3 |
import os
|
| 4 |
import cv2
|
|
|
|
| 7 |
import subprocess
|
| 8 |
import shutil
|
| 9 |
|
| 10 |
+
# La configuraci贸n de GEMINI_API_KEY y las funciones relacionadas con Gemini han sido eliminadas.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def text_to_speech(text, output_filename="audio.mp3"):
|
| 13 |
try:
|
|
|
|
| 124 |
print("DEBUG: FFmpeg not found during combine.")
|
| 125 |
raise Exception("Error: FFmpeg no encontrado. Aseg煤rate de que est茅 instalado en el entorno.")
|
| 126 |
|
| 127 |
+
def generate_tts_only(news_text_input): # Se elimina gemini_prompt
|
| 128 |
+
news_content = news_text_input
|
| 129 |
temp_audio_file = "audio_temp_preview.mp3"
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
if not news_content:
|
| 132 |
+
return "Por favor, escribe una noticia para generar el audio.", None
|
| 133 |
|
| 134 |
try:
|
| 135 |
text_to_speech(news_content, output_filename=temp_audio_file)
|
|
|
|
| 137 |
except Exception as e:
|
| 138 |
return f"Ocurri贸 un error al generar solo el audio: {e}", None
|
| 139 |
|
| 140 |
+
def create_news_video_app(news_text_input, image_files, video_ratio, input_audio_file=None): # Se elimina gemini_prompt
|
| 141 |
+
news_content = news_text_input # Ya no hay opci贸n de Gemini
|
| 142 |
output_video_path = None
|
| 143 |
error_message = ""
|
| 144 |
|
|
|
|
| 152 |
os.makedirs(processed_image_folder, exist_ok=True)
|
| 153 |
|
| 154 |
try:
|
| 155 |
+
if not news_content and (not input_audio_file or not os.path.exists(input_audio_file)):
|
| 156 |
+
return "Por favor, escribe una noticia o genera el audio primero.", None
|
| 157 |
+
|
| 158 |
if not input_audio_file or not os.path.exists(input_audio_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 159 |
text_to_speech(news_content, output_filename=temp_audio_file)
|
| 160 |
|
| 161 |
audio_duration = get_audio_duration(temp_audio_file)
|
|
|
|
| 191 |
print(f"DEBUG: Error caught in main app function: {e}")
|
| 192 |
return error_message, None
|
| 193 |
finally:
|
|
|
|
| 194 |
if os.path.exists(temp_audio_file) and temp_audio_file != input_audio_file:
|
| 195 |
os.remove(temp_audio_file)
|
| 196 |
if os.path.exists("audio_temp_preview.mp3"):
|
| 197 |
+
os.remove("audio_temp_preview.mp3")
|
| 198 |
if os.path.exists(temp_video_no_audio):
|
| 199 |
os.remove(temp_video_no_audio)
|
| 200 |
if os.path.exists(processed_image_folder):
|
|
|
|
| 205 |
with gr.Blocks() as demo:
|
| 206 |
gr.Markdown(
|
| 207 |
"""
|
| 208 |
+
# 馃帴 Creador de Videos de Noticias con Gradio 馃帴
|
| 209 |
+
Escribe una noticia y sube tus im谩genes para generar un video.
|
|
|
|
| 210 |
"""
|
| 211 |
)
|
| 212 |
|
|
|
|
| 218 |
lines=5,
|
| 219 |
value=""
|
| 220 |
)
|
| 221 |
+
# El campo para el prompt de Gemini ha sido eliminado.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
|
|
|
| 223 |
generate_audio_button = gr.Button("1. Generar Solo Audio")
|
| 224 |
audio_output_preview = gr.Audio(label="Audio de Noticia (Vista Previa)", interactive=False)
|
| 225 |
audio_status_message = gr.Textbox(label="Estado del Audio", interactive=False)
|
|
|
|
| 244 |
|
| 245 |
generate_audio_button.click(
|
| 246 |
fn=generate_tts_only,
|
| 247 |
+
inputs=[news_input], # Se elimina gemini_prompt de los inputs
|
| 248 |
outputs=[audio_status_message, audio_output_preview]
|
| 249 |
)
|
| 250 |
|
| 251 |
generate_video_button.click(
|
| 252 |
fn=create_news_video_app,
|
| 253 |
+
inputs=[news_input, image_upload, video_ratio_dropdown, audio_output_preview], # Se elimina gemini_prompt de los inputs
|
| 254 |
outputs=[output_message, video_output]
|
| 255 |
)
|
| 256 |
|