File size: 3,707 Bytes
bdb3536
a3c1a92
65b3767
b2d3f30
709cc8d
b2d3f30
 
a3c1a92
b2d3f30
 
 
 
 
65b3767
b2d3f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bdb3536
b2d3f30
 
 
 
 
 
 
bdb3536
b2d3f30
65b3767
 
bdb3536
b2d3f30
 
 
 
bdb3536
b2d3f30
 
bdb3536
b2d3f30
 
bdb3536
b2d3f30
 
65b3767
b2d3f30
 
65b3767
 
a3c1a92
b2d3f30
 
a3c1a92
b2d3f30
bdb3536
 
 
 
 
 
 
 
 
b2d3f30
 
 
 
 
bdb3536
 
 
 
 
 
 
b2d3f30
bdb3536
 
b2d3f30
 
 
 
 
 
bdb3536
 
65b3767
bdb3536
 
 
a3c1a92
bdb3536
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
import subprocess
import gradio as gr
import time

# Variabile di controllo per il setup
SETUP_COMPLETED = False

def perform_setup():
    global SETUP_COMPLETED
    if SETUP_COMPLETED:
        return "Setup già completato", True
    
    try:
        # Clona il repository
        if not os.path.exists("Open-Sora"):
            os.system("git clone https://github.com/hpcaitech/Open-Sora")
        
        # Installa il pacchetto in modalità sviluppatore
        os.system("cd Open-Sora && pip install -e .")
        
        # Installa xformers con la versione appropriata
        os.system("pip install xformers==0.0.27.post2 --index-url https://download.pytorch.org/whl/cu118")
        
        # Scarica il modello
        if not os.path.exists("ckpts"):
            os.system("mkdir -p ckpts")
            os.system("huggingface-cli download hpcai-tech/Open-Sora-v2 --local-dir ./ckpts")
        
        SETUP_COMPLETED = True
        return "Setup completato con successo!", True
    except Exception as e:
        return f"Errore durante il setup: {str(e)}", False

def generate_video(prompt, setup_first=True):
    # Esegui il setup se necessario
    if setup_first and not SETUP_COMPLETED:
        status, success = perform_setup()
        if not success:
            return status, None
    
    try:
        # Crea directory per gli output
        if not os.path.exists("outputs"):
            os.makedirs("outputs")
        
        # ID univoco per l'output
        timestamp = int(time.time())
        output_dir = f"outputs/video_{timestamp}"
        os.makedirs(output_dir, exist_ok=True)
        
        # Prepara il comando (modifica la configurazione per disabilitare flash-attn)
        cmd = f"cd Open-Sora && python -m scripts.diffusion.inference configs/diffusion/inference/256px.py --prompt \"{prompt}\" --save-dir ../{output_dir} --model.use-flash-attn=False"
        
        print(f"Esecuzione comando: {cmd}")
        result = os.system(cmd)
        
        if result != 0:
            return "Errore durante la generazione del video. Controlla i log.", None
        
        # Cerca il video generato
        video_files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.endswith(".mp4")]
        if not video_files:
            return "Nessun video generato trovato", None
        
        # Prendi il più recente
        video_path = sorted(video_files, key=lambda x: os.path.getmtime(x), reverse=True)[0]
        
        return "Video generato con successo!", video_path
    except Exception as e:
        return f"Errore: {str(e)}", None

# Interfaccia Gradio
with gr.Blocks() as demo:
    gr.Markdown("# Generatore di Video con Open-Sora")
    
    with gr.Row():
        with gr.Column():
            setup_button = gr.Button("Setup iniziale (esegui una volta)")
            setup_output = gr.Textbox(label="Stato Setup", value="Non eseguito")
            
            gr.Markdown("---")
            
            prompt_input = gr.Textbox(
                label="Descrivi il video che vuoi generare",
                placeholder="Es. Un panda che mangia bambù in una foresta, stile cinematografico"
            )
            generate_btn = gr.Button("Genera Video")
        
        with gr.Column():
            status_output = gr.Textbox(label="Stato Generazione")
            video_output = gr.Video(label="Video Generato")
    
    # Collega i pulsanti alle funzioni
    setup_button.click(
        fn=perform_setup,
        outputs=[setup_output]
    )
    
    generate_btn.click(
        fn=generate_video,
        inputs=[prompt_input],
        outputs=[status_output, video_output]
    )

# Avvia l'interfaccia
demo.launch()