import os
import subprocess
from PIL import Image
import gradio as gr
import cv2
# Codigo creado por IA(Sistema de Interes) https://www.youtube.com/@IA.Sistema.de.Interes
# Función para ejecutar comandos y devolver el estado
def run_commands(description, aspect_ratio, enhance_check, model, style, upscale_check, task_type, img=None, is_extend=False, is_recreate=False):
try:
commands = []
if True:
# Comandos generales según el tipo de tarea
if upscale_check:
commands.append('python /content/Vidu_Studio/vidu_credit.py --credit 4')
else:
commands.append('python /content/Vidu_Studio/vidu_credit.py --credit 0')
if task_type == "video_to_video":
commands.append('python /content/Vidu_Studio/utils/video_fragment.py')
print("File Uploader...")
commands.append('python /content/Vidu_Studio/upload/file_uploader.py --file_info "file_info" --image "img_fragmento"')
if task_type == "extend":
print("Last frame...")
commands.append('python /content/Vidu_Studio/utils/fragment.py')
if task_type == "reference_to_video" or task_type == "image_to_video" or task_type == "extend":
print("File Uploader...")
commands.append('python /content/Vidu_Studio/upload/file_uploader.py --file_info "file_info" --image "img_fragmento"')
if task_type == "reference_to_video":
print("Generating video with reference...")
commands.append(
f'python /content/Vidu_Studio/create_video_reference.py --prompt "{description}" --enhance {str(enhance_check).lower()} --model "{model}"'
)
elif task_type == "text_to_video":
print("Generating video: Text to Video...")
commands.append(
f'python /content/Vidu_Studio/create_text_to_video.py --prompt "{description}" --enhance {str(enhance_check).lower()} --model "{model}" --style "{style}" --aspect_ratio "{aspect_ratio}"'
)
elif task_type == "image_to_video" or task_type == "extend":
print("Generating Video: Image to Video...")
commands.append(
f'python /content/Vidu_Studio/create_task.py --prompt "{description}" --enhance {str(enhance_check).lower()} --model "{model}"'
)
elif task_type == "video_to_video":
print("Generating Video: Video to Video...")
commands.append(
f'python /content/Vidu_Studio/create_task.py --prompt "{description}" --enhance {str(enhance_check).lower()} --model "{model}"'
)
else:
raise ValueError(f"Tipo de tarea no reconocido: {task_type}")
print("Video in progress...")
commands.append('python /content/Vidu_Studio/utils/process_task.py')
if upscale_check:
print("Generate Upscale...")
commands.append('python /content/Vidu_Studio/utils/upscale_task.py')
print("Upscale in progress...")
commands.append('python /content/Vidu_Studio/utils/process_task_upscale.py')
commands.append('python /content/Vidu_Studio/utils/delete_task.py')
commands.append('python /content/Vidu_Studio/utils/delete_task_upscale.py')
else:
commands.append('python /content/Vidu_Studio/utils/delete_task.py')
output = ""
for command in commands:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output += f"Exit:\n{result.stdout}\n"
print(result.stdout)
video_path = "/content/Vidu_Studio/video.mp4"
return output, video_path
except Exception as e:
return f"Excepción: {str(e)}", None
def run_commands_dual(description, aspect_ratio, enhance_check, model, style, upscale_check, task_type, add_end_frame, img=None, is_extend=False, is_recreate=False):
try:
commands = []
if add_end_frame:
# Comandos generales según el tipo de tarea
if upscale_check:
commands.append('python /content/Vidu_Studio/vidu_credit.py --credit 4')
else:
commands.append('python /content/Vidu_Studio/vidu_credit.py --credit 0')
print("File Uploader...")
commands.append('python /content/Vidu_Studio/upload/file_uploader.py --file_info "file_info" --image "img_fragmento"')
commands.append('python /content/Vidu_Studio/upload/file_uploader.py --file_info "file_info2" --image "img_fragmento2"')
if task_type == "headtailimg2video":
print("Generating video with headtailimg2video...")
commands.append(
f'python /content/Vidu_Studio/headtailimg2video.py --prompt "{description}" --enhance {str(enhance_check).lower()} --model "{model}"'
)
print("Video in progress...")
commands.append('python /content/Vidu_Studio/utils/process_task.py')
if upscale_check:
print("Generate Upscale...")
commands.append('python /content/Vidu_Studio/utils/upscale_task.py')
print("Upscale in progress...")
commands.append('python /content/Vidu_Studio/utils/process_task_upscale.py')
commands.append('python /content/Vidu_Studio/utils/delete_task.py')
commands.append('python /content/Vidu_Studio/utils/delete_task_upscale.py')
else:
commands.append('python /content/Vidu_Studio/utils/delete_task.py')
else:
# Comandos generales según el tipo de tarea
if upscale_check:
commands.append('python /content/Vidu_Studio/vidu_credit.py --credit 4')
else:
commands.append('python /content/Vidu_Studio/vidu_credit.py --credit 0')
print("File Uploader...")
commands.append('python /content/Vidu_Studio/upload/file_uploader.py --file_info "file_info" --image "img_fragmento"')
if task_type == "headtailimg2video":
print("Generating video with Image to Video...")
commands.append(
f'python /content/Vidu_Studio/create_task.py --prompt "{description}" --enhance {str(enhance_check).lower()} --model "{model}"'
)
print("Video in progress...")
commands.append('python /content/Vidu_Studio/utils/process_task.py')
if upscale_check:
print("Generate Upscale...")
commands.append('python /content/Vidu_Studio/utils/upscale_task.py')
print("Upscale in progress...")
commands.append('python /content/Vidu_Studio/utils/process_task_upscale.py')
commands.append('python /content/Vidu_Studio/utils/delete_task.py')
commands.append('python /content/Vidu_Studio/utils/delete_task_upscale.py')
else:
commands.append('python /content/Vidu_Studio/utils/delete_task.py')
output = ""
for command in commands:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output += f"Exit:\n{result.stdout}\n"
print(result.stdout)
video_path = "/content/Vidu_Studio/video.mp4"
return output, video_path
except Exception as e:
return f"Excepción: {str(e)}", None
# Función para procesar y guardar la imagen automáticamente en formato JPG
def process_and_save_image(image, coordinates_file_path="/tmp/coordinates.txt"):
jpg_path = "/tmp/img_fragmento.jpg"
try:
if image.format != "JPEG":
image = image.convert("RGB")
image.save(jpg_path, "JPEG", quality=100)
if os.path.exists(jpg_path):
print(f"Imagen guardada correctamente")
# Obtener las dimensiones de la imagen
width, height = image.size
# Guardar las coordenadas en un archivo de texto
with open(coordinates_file_path, "w") as file:
file.write(f"X: {width}\n")
file.write(f"Y: {height}\n")
print(f"Coordenadas guardadas correctamente")
return None
except Exception as e:
print(f"Error al procesar la imagen: {str(e)}")
return None
# Función para procesar y guardar la imagen automáticamente en formato JPG
def process_and_save_image1(image, coordinates_file_path="/tmp/coordinates.txt"):
jpg_path = "/tmp/img_fragmento.jpg"
try:
if image.format != "JPEG":
image = image.convert("RGB")
image.save(jpg_path, "JPEG", quality=100)
if os.path.exists(jpg_path):
print(f"Imagen guardada correctamente")
# Obtener las dimensiones de la imagen
width, height = image.size
# Guardar las coordenadas en un archivo de texto
with open(coordinates_file_path, "w") as file:
file.write(f"X: {width}\n")
file.write(f"Y: {height}\n")
print(f"Coordenadas guardadas correctamente")
return None
except Exception as e:
print(f"Error al procesar la imagen: {str(e)}")
return None
# Función para procesar y guardar la imagen automáticamente en formato JPG
def process_and_save_image2(image, coordinates_file_path="/tmp/coordinates2.txt"):
jpg_path = "/tmp/img_fragmento2.jpg"
try:
if image.format != "JPEG":
image = image.convert("RGB")
image.save(jpg_path, "JPEG", quality=100)
if os.path.exists(jpg_path):
print(f"Imagen guardada correctamente")
# Obtener las dimensiones de la imagen
width, height = image.size
# Guardar las coordenadas en un archivo de texto
with open(coordinates_file_path, "w") as file:
file.write(f"X: {width}\n")
file.write(f"Y: {height}\n")
print(f"Coordenadas guardadas correctamente")
except Exception as e:
print(f"Error al procesar la imagen: {str(e)}")
# Función para obtener el último frame de un video
def get_last_frame(video_path):
try:
cap = cv2.VideoCapture(video_path)
frame_count = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count - 1)
ret, frame = cap.read()
cap.release()
if ret:
return Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
else:
print(f"Error al leer el último frame del video: {video_path}")
return None
except Exception as e:
print(f"Excepción al obtener el último frame: {str(e)}")
return None
# Función para ejecutar los comandos de guardado
def run_save_commands():
try:
print("Rendering video...")
commands = [
"python /content/Vidu_Studio/utils/fragment.py",
"python /content/Vidu_Studio/utils/union.py"
]
output = ""
for command in commands:
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output += f"Exit:\n{result.stdout}\n"
print(result.stdout)
video_path = "/content/Vidu_Studio/video.mp4"
return output, video_path
except Exception as e:
return f"Excepción al guardar: {str(e)}", None
# Función para mostrar o ocultar el segundo cuadro de imagen
def activar_end_frame(activar):
return gr.update(visible=activar)
# Definir la interfaz
with gr.Blocks() as demo:
# Agregar un título en grande al inicio de la interfaz
gr.HTML(f"
VIDU STUDIO 3.0 - AUTOMATICCreated by:IA(Sistema de Interés)")
with gr.Tabs():
# Primera pestaña: Image to Video
with gr.Tab("Image to Video"):
with gr.Row():
with gr.Column():
add_end_frame = gr.Checkbox(label="Add End Frame", value=False)
# Fila para mostrar ambas imágenes cuando se activa
with gr.Row():
# Imagen de inicio (carga de archivo)
img1 = gr.Image(type="pil", label="Drag image here or select image", interactive=True,
elem_id="img1", width=200, height=200) # Ajustar tamaño de la imagen
# Imagen de fin (se oculta inicialmente)
img2 = gr.Image(type="pil", label="Upload the last frame image", interactive=True,
elem_id="img2", visible=False, width=200, height=200) # Ajustar tamaño de la imagen
description_input = gr.Textbox(label="Prompt",
placeholder="Aquí va una caja de texto editable para el prompt",
elem_id="description_input")
enhance_checkbox = gr.Checkbox(label="Enhance prompt", value=False, elem_id="enhance_checkbox")
model_dropdown = gr.Dropdown(
choices=["vidu-1", "vidu-high-performance"],
label="Model",
value="vidu-1",
elem_id="model_dropdown"
)
upscale_checkbox = gr.Checkbox(label="Upscale", value=False, elem_id="upscale_checkbox")
create_button = gr.Button("Create", elem_id="create_button")
extend_button = gr.Button("Extend", elem_id="extend_button")
recreate_button = gr.Button("Re-create", elem_id="recreate_button") # Nuevo botón "Re-create"
save_button = gr.Button("Save", elem_id="save_button") # Botón de guardado
with gr.Column():
video_output = gr.Video(label="Video Output Image to Video", height=400, elem_id="video_output_image_to_video")
output_textbox = gr.Textbox(label="Output", interactive=False, elem_id="output_textbox") # Cambia el nombre del textbox
# Lógica para mostrar el segundo cuadro de imagen al activar el checkbox
add_end_frame.change(activar_end_frame, add_end_frame, img2)
# Procesar la imagen automáticamente cuando se carga
img1.change(
fn=process_and_save_image1,
inputs=img1,
outputs=[]
)
img2.change(
fn=process_and_save_image2,
inputs=img2,
outputs=[]
)
# Botón "Create" y su salida
create_button.click(
fn=lambda desc, enhance, model, upscale, img, add_end: run_commands_dual(desc, "16:9", enhance, model, "general", upscale, task_type="headtailimg2video", img=img, add_end_frame=add_end),
inputs=[description_input, enhance_checkbox, model_dropdown, upscale_checkbox, img1, add_end_frame], # Añade el Checkbox aquí
outputs=[output_textbox, video_output] # Asegúrate de tener definidos estos componentes de salida
)
# Botón "Extend" y su salida
extend_button.click(
fn=lambda desc, enhance, model, upscale, img: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="extend", img=img, is_extend=True), # Agrega img como argumento
inputs=[description_input, enhance_checkbox, model_dropdown, upscale_checkbox, img1], # Agrega image_input a la lista de inputs
outputs=[output_textbox, video_output]
)
# Botón "Re-create" y su salida
recreate_button.click(
fn=lambda desc, enhance, model, upscale, img: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="image_to_video", img=img, is_recreate=True), # Agrega img como argumento
inputs=[description_input, enhance_checkbox, model_dropdown, upscale_checkbox, img1], # Agrega image_input a la lista de inputs
outputs=[output_textbox, video_output]
)
# Botón "Save" y su salida
save_button.click(
fn=run_save_commands,
inputs=[],
outputs=[output_textbox, video_output]
)
# Segunda pestaña: Reference to Video
with gr.Tab("Reference to Video"):
with gr.Row():
with gr.Column():
image_input2 = gr.Image(type="pil", label="Drag image here or select image", interactive=True,
elem_id="image_input2")
description_input2 = gr.Textbox(label="Prompt",
placeholder="Enter text to begin creating a video that aligns with the subject of the image",
elem_id="description_input2")
enhance_checkbox2 = gr.Checkbox(label="Enhance prompt", value=False, elem_id="enhance_checkbox2")
model_dropdown2 = gr.Dropdown(
choices=["vidu-1", "vidu-high-performance"],
label="Model",
value="vidu-1",
elem_id="model_dropdown2"
)
upscale_checkbox2 = gr.Checkbox(label="Upscale", value=False, elem_id="upscale_checkbox2")
create_button2 = gr.Button("Create", elem_id="create_button2")
extend_button2 = gr.Button("Extend", elem_id="extend_button2")
recreate_button2 = gr.Button("Re-create", elem_id="recreate_button2") # Nuevo botón "Re-create"
save_button2 = gr.Button("Save", elem_id="save_button2") # Botón de guardado
with gr.Column():
video_output2 = gr.Video(label="Video Output Reference to Video", height=400, elem_id="video_output_reference_to_video")
output_textbox2 = gr.Textbox(label="Output", interactive=False, elem_id="output_textbox2") # Cambia el nombre del textbox
# Procesar la imagen automáticamente cuando se carga
image_input2.change(
fn=process_and_save_image,
inputs=image_input2,
outputs=[]
)
# Botón "Create" y su salida
create_button2.click(
fn=lambda desc, enhance, model, upscale, img: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="reference_to_video", img=img),
inputs=[description_input2, enhance_checkbox2, model_dropdown2, upscale_checkbox2, image_input2],
outputs=[output_textbox2, video_output2]
)
# Botón "Extend" y su salida
extend_button2.click(
fn=lambda desc, enhance, model, upscale, img: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="extend", img=img, is_extend=True),
inputs=[description_input2, enhance_checkbox2, model_dropdown2, upscale_checkbox2, image_input2],
outputs=[output_textbox2, video_output2]
)
# Botón "Re-create" y su salida
recreate_button2.click(
fn=lambda desc, enhance, model, upscale, img: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="reference_to_video", img=img, is_recreate=True),
inputs=[description_input2, enhance_checkbox2, model_dropdown2, upscale_checkbox2, image_input2],
outputs=[output_textbox2, video_output2]
)
# Botón "Save" y su salida
save_button2.click(
fn=run_save_commands,
inputs=[],
outputs=[output_textbox2, video_output2]
)
# Tercera pestaña: Text to Video
with gr.Tab("Text to Video"):
with gr.Row():
with gr.Column():
description_input3 = gr.Textbox(label="Prompt",
placeholder="Enter text to begin creating a video",
elem_id="description_input3")
aspect_ratio_dropdown = gr.Dropdown(
choices=["16:9", "9:16", "4:3", "1:1"],
label="Aspect Ratio",
value="16:9",
elem_id="aspect_ratio_dropdown"
)
enhance_checkbox3 = gr.Checkbox(label="Enhance prompt", value=False, elem_id="enhance_checkbox3")
model_dropdown3 = gr.Dropdown(
choices=["vidu-1", "vidu-high-performance"],
label="Model",
value="vidu-1",
elem_id="model_dropdown3"
)
style_dropdown3 = gr.Dropdown(
choices=["general", "anime"],
label="Style",
value="general",
elem_id="style_dropdown3"
)
upscale_checkbox3 = gr.Checkbox(label="Upscale", value=False, elem_id="upscale_checkbox3")
create_button3 = gr.Button("Create", elem_id="create_button3")
extend_button3 = gr.Button("Extend", elem_id="extend_button3")
recreate_button3 = gr.Button("Re-create", elem_id="recreate_button3") # Nuevo botón "Re-create"
save_button3 = gr.Button("Save", elem_id="save_button3") # Botón de guardado
with gr.Column():
video_output3 = gr.Video(label="Video Output Text to Video", height=400, elem_id="video_output_text_to_video")
output_textbox3 = gr.Textbox(label="Output", interactive=False, elem_id="output_textbox3") # Cambia el nombre del textbox
# Botón "Create" y su salida
create_button3.click(
fn=lambda desc, ratio, enhance, model, style, upscale: run_commands(desc, ratio, enhance, model, style,
upscale, task_type="text_to_video"),
inputs=[description_input3, aspect_ratio_dropdown, enhance_checkbox3, model_dropdown3, style_dropdown3,
upscale_checkbox3],
outputs=[output_textbox3, video_output3]
)
# Botón "Extend" y su salida
extend_button3.click(
fn=lambda desc, enhance, model, upscale: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="extend", is_extend=True), # "general" como argumento
inputs=[description_input3, enhance_checkbox3, model_dropdown3, upscale_checkbox3], # Eliminado "general"
outputs=[output_textbox3, video_output3]
)
# Botón "Re-create" y su salida
recreate_button3.click(
fn=lambda desc, ratio, enhance, model, style, upscale: run_commands(desc, ratio, enhance, model, style,
upscale, task_type="text_to_video", is_recreate=True),
inputs=[description_input3, aspect_ratio_dropdown, enhance_checkbox3, model_dropdown3, style_dropdown3,
upscale_checkbox3],
outputs=[output_textbox3, video_output3]
)
# Botón "Save" y su salida
save_button3.click(
fn=run_save_commands,
inputs=[],
outputs=[output_textbox3, video_output3]
)
# Cuarta pestaña: Video to Video
with gr.Tab("Video to Video"):
with gr.Row():
with gr.Column():
video_input4 = gr.Video(label="Input Video", height=400, interactive=True) # Agregar entrada de video
description_input4 = gr.Textbox(label="Prompt",
placeholder="Enter text to begin creating a video",
elem_id="description_input4")
enhance_checkbox4 = gr.Checkbox(label="Enhance prompt", value=False, elem_id="enhance_checkbox4")
model_dropdown4 = gr.Dropdown(
choices=["vidu-1", "vidu-high-performance"],
label="Model",
value="vidu-1",
elem_id="model_dropdown4"
)
upscale_checkbox4 = gr.Checkbox(label="Upscale", value=False, elem_id="upscale_checkbox4")
create_button4 = gr.Button("Create", elem_id="create_button4")
extend_button4 = gr.Button("Extend", elem_id="extend_button4")
recreate_button4 = gr.Button("Re-create", elem_id="recreate_button4") # Nuevo botón "Re-create"
save_button4 = gr.Button("Save", elem_id="save_button4") # Botón de guardado
with gr.Column():
video_output4 = gr.Video(label="Video Output Video to Video", height=400, elem_id="video_output_video_to_video")
output_textbox4 = gr.Textbox(label="Output", interactive=False, elem_id="output_textbox4") # Cambia el nombre del textbox
# Guardar el video cargado en /content/Vidu_Studio/video.mp4
def save_uploaded_video(video):
if video is not None:
video_path = video
result = subprocess.run(f"cp '{video_path}' /content/Vidu_Studio/videotovideo.mp4", shell=True)
print(f"Video guardado en /content/Vidu_Studio/videotovideo.mp4")
print(result.stdout)
# Ejecutar save_uploaded_video cuando se carga un video
video_input4.upload(
fn=save_uploaded_video,
inputs=video_input4,
outputs=None
)
# Botón "Create" y su salida
create_button4.click(
fn=lambda desc, enhance, model, upscale: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="video_to_video"), # "general" como argumento
inputs=[description_input4, enhance_checkbox4, model_dropdown4, upscale_checkbox4], # Eliminado "general"
outputs=[output_textbox4, video_output4]
)
# Botón "Extend" y su salida
extend_button4.click(
fn=lambda desc, enhance, model, upscale: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="extend", is_extend=True), # "general" como argumento
inputs=[description_input4, enhance_checkbox4, model_dropdown4, upscale_checkbox4], # Eliminado "general"
outputs=[output_textbox4, video_output4]
)
# Botón "Re-create" y su salida
recreate_button4.click(
fn=lambda desc, enhance, model, upscale: run_commands(desc, "16:9", enhance, model, "general", upscale, task_type="video_to_video", is_recreate=True), # "general" como argumento
inputs=[description_input4, enhance_checkbox4, model_dropdown4, upscale_checkbox4], # Eliminado "general"
outputs=[output_textbox4, video_output4]
)
# Botón "Save" y su salida
save_button4.click(
fn=run_save_commands,
inputs=[],
outputs=[output_textbox4, video_output4]
)
# Ejecutar la interfaz
demo.launch(inline=False, debug=True, share=True)