Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -35,7 +35,7 @@ except Exception as e:
|
|
35 |
tokenizer = None
|
36 |
|
37 |
def generar_guion_largo(tema, custom_script=None):
|
38 |
-
"""Genera un texto largo sobre el tema usando GPT-2"""
|
39 |
if custom_script:
|
40 |
return custom_script
|
41 |
|
@@ -48,15 +48,17 @@ def generar_guion_largo(tema, custom_script=None):
|
|
48 |
|
49 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
50 |
|
51 |
-
# Generar texto
|
52 |
outputs = model.generate(
|
53 |
inputs.input_ids,
|
54 |
max_length=800,
|
|
|
55 |
temperature=0.9,
|
56 |
top_k=50,
|
57 |
top_p=0.95,
|
58 |
num_return_sequences=1,
|
59 |
-
pad_token_id=tokenizer.eos_token_id
|
|
|
60 |
)
|
61 |
|
62 |
guion = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
@@ -64,6 +66,7 @@ def generar_guion_largo(tema, custom_script=None):
|
|
64 |
# Limpiar texto
|
65 |
guion = re.sub(r'<.*?>', '', guion)
|
66 |
guion = re.sub(r'\n+', '\n', guion)
|
|
|
67 |
|
68 |
logger.info(f"Guion generado: {len(guion)} caracteres")
|
69 |
return guion
|
@@ -165,19 +168,37 @@ def crear_video(prompt, custom_script, voz_seleccionada, musica=None):
|
|
165 |
if not clips:
|
166 |
raise Exception("No se pudieron cargar videos válidos")
|
167 |
|
168 |
-
# 6. Combinar videos
|
169 |
-
|
170 |
-
|
171 |
|
172 |
-
# 7.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
173 |
output_path = f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
|
174 |
-
|
175 |
output_path,
|
176 |
fps=24,
|
177 |
codec="libx264",
|
178 |
audio_codec="aac",
|
179 |
threads=4,
|
180 |
-
preset='fast'
|
|
|
181 |
)
|
182 |
|
183 |
logger.info(f"Video generado exitosamente: {output_path}")
|
@@ -195,24 +216,42 @@ def crear_video(prompt, custom_script, voz_seleccionada, musica=None):
|
|
195 |
if os.path.exists(temp_file):
|
196 |
os.remove(temp_file)
|
197 |
|
198 |
-
# Interfaz
|
199 |
-
with gr.Blocks(title="Generador de Videos") as app:
|
200 |
gr.Markdown("# 🎬 GENERADOR AUTOMÁTICO DE VIDEOS")
|
201 |
|
202 |
with gr.Row():
|
203 |
with gr.Column():
|
204 |
-
prompt = gr.Textbox(
|
|
|
|
|
|
|
|
|
205 |
custom_script = gr.TextArea(
|
206 |
label="Guion personalizado (opcional)",
|
207 |
placeholder="Pega tu guion completo aquí...",
|
208 |
-
lines=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
209 |
)
|
210 |
-
voz = gr.Dropdown(label="Voz Narradora", choices=VOICES, value=VOICES[0])
|
211 |
-
musica = gr.File(label="Música de fondo (opcional)", file_types=["audio"])
|
212 |
btn = gr.Button("Generar Video", variant="primary")
|
213 |
|
214 |
with gr.Column():
|
215 |
-
output = gr.Video(
|
|
|
|
|
|
|
|
|
216 |
|
217 |
btn.click(
|
218 |
fn=crear_video,
|
@@ -221,4 +260,8 @@ with gr.Blocks(title="Generador de Videos") as app:
|
|
221 |
)
|
222 |
|
223 |
if __name__ == "__main__":
|
224 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
35 |
tokenizer = None
|
36 |
|
37 |
def generar_guion_largo(tema, custom_script=None):
|
38 |
+
"""Genera un texto largo sobre el tema usando GPT-2 con configuración correcta"""
|
39 |
if custom_script:
|
40 |
return custom_script
|
41 |
|
|
|
48 |
|
49 |
inputs = tokenizer(prompt, return_tensors="pt", max_length=512, truncation=True)
|
50 |
|
51 |
+
# Generar texto con configuración corregida
|
52 |
outputs = model.generate(
|
53 |
inputs.input_ids,
|
54 |
max_length=800,
|
55 |
+
do_sample=True, # Añadido para habilitar temperature y top_p
|
56 |
temperature=0.9,
|
57 |
top_k=50,
|
58 |
top_p=0.95,
|
59 |
num_return_sequences=1,
|
60 |
+
pad_token_id=tokenizer.eos_token_id,
|
61 |
+
early_stopping=True
|
62 |
)
|
63 |
|
64 |
guion = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
66 |
# Limpiar texto
|
67 |
guion = re.sub(r'<.*?>', '', guion)
|
68 |
guion = re.sub(r'\n+', '\n', guion)
|
69 |
+
guion = re.sub(r'\s+', ' ', guion).strip()
|
70 |
|
71 |
logger.info(f"Guion generado: {len(guion)} caracteres")
|
72 |
return guion
|
|
|
168 |
if not clips:
|
169 |
raise Exception("No se pudieron cargar videos válidos")
|
170 |
|
171 |
+
# 6. Combinar videos con transiciones suaves
|
172 |
+
final_clip = concatenate_videoclips(clips, method="compose")
|
173 |
+
final_clip = final_clip.set_audio(audio)
|
174 |
|
175 |
+
# 7. Aplicar música de fondo si existe
|
176 |
+
if musica:
|
177 |
+
try:
|
178 |
+
musica_clip = AudioFileClip(musica.name)
|
179 |
+
if musica_clip.duration < duracion_total:
|
180 |
+
musica_clip = musica_clip.loop(duration=duracion_total)
|
181 |
+
else:
|
182 |
+
musica_clip = musica_clip.subclip(0, duracion_total)
|
183 |
+
|
184 |
+
audio_final = CompositeAudioClip([
|
185 |
+
audio.volumex(1.0),
|
186 |
+
musica_clip.volumex(0.25)
|
187 |
+
])
|
188 |
+
final_clip = final_clip.set_audio(audio_final)
|
189 |
+
except Exception as e:
|
190 |
+
logger.error(f"Error procesando música: {str(e)}")
|
191 |
+
|
192 |
+
# 8. Exportar video final
|
193 |
output_path = f"video_{datetime.now().strftime('%Y%m%d_%H%M%S')}.mp4"
|
194 |
+
final_clip.write_videofile(
|
195 |
output_path,
|
196 |
fps=24,
|
197 |
codec="libx264",
|
198 |
audio_codec="aac",
|
199 |
threads=4,
|
200 |
+
preset='fast',
|
201 |
+
ffmpeg_params=['-crf', '23'] # Calidad balanceada
|
202 |
)
|
203 |
|
204 |
logger.info(f"Video generado exitosamente: {output_path}")
|
|
|
216 |
if os.path.exists(temp_file):
|
217 |
os.remove(temp_file)
|
218 |
|
219 |
+
# Interfaz mejorada
|
220 |
+
with gr.Blocks(title="Generador de Videos", theme=gr.themes.Soft()) as app:
|
221 |
gr.Markdown("# 🎬 GENERADOR AUTOMÁTICO DE VIDEOS")
|
222 |
|
223 |
with gr.Row():
|
224 |
with gr.Column():
|
225 |
+
prompt = gr.Textbox(
|
226 |
+
label="Tema del video",
|
227 |
+
placeholder="Ej: 'La historia de la piratería en el Caribe'",
|
228 |
+
max_lines=1
|
229 |
+
)
|
230 |
custom_script = gr.TextArea(
|
231 |
label="Guion personalizado (opcional)",
|
232 |
placeholder="Pega tu guion completo aquí...",
|
233 |
+
lines=8,
|
234 |
+
max_lines=20
|
235 |
+
)
|
236 |
+
voz = gr.Dropdown(
|
237 |
+
label="Voz Narradora",
|
238 |
+
choices=VOICES,
|
239 |
+
value=VOICES[0],
|
240 |
+
interactive=True
|
241 |
+
)
|
242 |
+
musica = gr.File(
|
243 |
+
label="Música de fondo (opcional)",
|
244 |
+
file_types=["audio"],
|
245 |
+
type="filepath"
|
246 |
)
|
|
|
|
|
247 |
btn = gr.Button("Generar Video", variant="primary")
|
248 |
|
249 |
with gr.Column():
|
250 |
+
output = gr.Video(
|
251 |
+
label="Video Resultado",
|
252 |
+
format="mp4",
|
253 |
+
interactive=False
|
254 |
+
)
|
255 |
|
256 |
btn.click(
|
257 |
fn=crear_video,
|
|
|
260 |
)
|
261 |
|
262 |
if __name__ == "__main__":
|
263 |
+
app.launch(
|
264 |
+
server_name="0.0.0.0",
|
265 |
+
server_port=7860,
|
266 |
+
enable_queue=True
|
267 |
+
)
|