claudiobxdai commited on
Commit
b2d3f30
·
verified ·
1 Parent(s): a1f7c6b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -47
app.py CHANGED
@@ -1,69 +1,73 @@
1
  import os
2
  import subprocess
3
  import gradio as gr
4
- import torch
5
- import sys
6
 
7
- # Verifica la versione di CUDA
8
- print(f"CUDA disponibile: {torch.cuda.is_available()}")
9
- if torch.cuda.is_available():
10
- print(f"CUDA versione: {torch.version.cuda}")
11
- print(f"Dispositivo: {torch.cuda.get_device_name(0)}")
12
 
13
- # Esegui lo script di setup se necessario
14
- if not os.path.exists("Open-Sora"):
15
- print("Esecuzione setup iniziale...")
 
 
16
  try:
17
- subprocess.run(["bash", "setup.sh"], check=True)
18
- except subprocess.CalledProcessError as e:
19
- print(f"Errore durante l'esecuzione di setup.sh: {e}")
20
- print("Continuando comunque...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
- # Implementazione dell'inferenza seguendo la guida
23
- def generate_video(prompt):
 
 
 
 
 
24
  try:
25
- # Directory per i video generati
26
  if not os.path.exists("outputs"):
27
  os.makedirs("outputs")
28
 
29
- # Comando esattamente come nella guida
30
- cmd = [
31
- "torchrun",
32
- "--nproc_per_node", "1",
33
- "--standalone",
34
- "scripts/diffusion/inference.py",
35
- "configs/diffusion/inference/256px.py",
36
- "--prompt", prompt,
37
- "--save-dir", "outputs"
38
- ]
39
 
40
- print(f"Esecuzione comando: {' '.join(cmd)}")
 
41
 
42
- # Esegui il comando nella directory di Open-Sora
43
- process = subprocess.run(
44
- cmd,
45
- cwd="Open-Sora",
46
- capture_output=True,
47
- text=True,
48
- check=False
49
- )
50
 
51
- if process.returncode != 0:
52
- print(f"Errore: {process.stderr}")
53
- return "Errore durante la generazione", None
54
 
55
- # Cerca il video generato più recente
56
- video_files = [f for f in os.listdir("outputs") if f.endswith(".mp4")]
57
  if not video_files:
58
  return "Nessun video generato trovato", None
59
 
60
- # Ordina per data di creazione e prendi il più recente
61
- video_path = os.path.join("outputs", sorted(video_files, key=lambda x: os.path.getmtime(os.path.join("outputs", x)), reverse=True)[0])
62
 
63
- return "Generazione completata", video_path
64
-
65
  except Exception as e:
66
- print(f"Eccezione: {str(e)}")
67
  return f"Errore: {str(e)}", None
68
 
69
  # Interfaccia Gradio
@@ -72,6 +76,11 @@ with gr.Blocks() as demo:
72
 
73
  with gr.Row():
74
  with gr.Column():
 
 
 
 
 
75
  prompt_input = gr.Textbox(
76
  label="Descrivi il video che vuoi generare",
77
  placeholder="Es. Un panda che mangia bambù in una foresta, stile cinematografico"
@@ -79,9 +88,15 @@ with gr.Blocks() as demo:
79
  generate_btn = gr.Button("Genera Video")
80
 
81
  with gr.Column():
82
- status_output = gr.Textbox(label="Stato")
83
  video_output = gr.Video(label="Video Generato")
84
 
 
 
 
 
 
 
85
  generate_btn.click(
86
  fn=generate_video,
87
  inputs=[prompt_input],
 
1
  import os
2
  import subprocess
3
  import gradio as gr
4
+ import time
 
5
 
6
+ # Variabile di controllo per il setup
7
+ SETUP_COMPLETED = False
 
 
 
8
 
9
+ def perform_setup():
10
+ global SETUP_COMPLETED
11
+ if SETUP_COMPLETED:
12
+ return "Setup già completato", True
13
+
14
  try:
15
+ # Clona il repository
16
+ if not os.path.exists("Open-Sora"):
17
+ os.system("git clone https://github.com/hpcaitech/Open-Sora")
18
+
19
+ # Installa il pacchetto in modalità sviluppatore
20
+ os.system("cd Open-Sora && pip install -e .")
21
+
22
+ # Installa xformers con la versione appropriata
23
+ os.system("pip install xformers==0.0.27.post2 --index-url https://download.pytorch.org/whl/cu118")
24
+
25
+ # Scarica il modello
26
+ if not os.path.exists("ckpts"):
27
+ os.system("mkdir -p ckpts")
28
+ os.system("huggingface-cli download hpcai-tech/Open-Sora-v2 --local-dir ./ckpts")
29
+
30
+ SETUP_COMPLETED = True
31
+ return "Setup completato con successo!", True
32
+ except Exception as e:
33
+ return f"Errore durante il setup: {str(e)}", False
34
 
35
+ def generate_video(prompt, setup_first=True):
36
+ # Esegui il setup se necessario
37
+ if setup_first and not SETUP_COMPLETED:
38
+ status, success = perform_setup()
39
+ if not success:
40
+ return status, None
41
+
42
  try:
43
+ # Crea directory per gli output
44
  if not os.path.exists("outputs"):
45
  os.makedirs("outputs")
46
 
47
+ # ID univoco per l'output
48
+ timestamp = int(time.time())
49
+ output_dir = f"outputs/video_{timestamp}"
50
+ os.makedirs(output_dir, exist_ok=True)
 
 
 
 
 
 
51
 
52
+ # Prepara il comando (modifica la configurazione per disabilitare flash-attn)
53
+ 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"
54
 
55
+ print(f"Esecuzione comando: {cmd}")
56
+ result = os.system(cmd)
 
 
 
 
 
 
57
 
58
+ if result != 0:
59
+ return "Errore durante la generazione del video. Controlla i log.", None
 
60
 
61
+ # Cerca il video generato
62
+ video_files = [os.path.join(output_dir, f) for f in os.listdir(output_dir) if f.endswith(".mp4")]
63
  if not video_files:
64
  return "Nessun video generato trovato", None
65
 
66
+ # Prendi il più recente
67
+ video_path = sorted(video_files, key=lambda x: os.path.getmtime(x), reverse=True)[0]
68
 
69
+ return "Video generato con successo!", video_path
 
70
  except Exception as e:
 
71
  return f"Errore: {str(e)}", None
72
 
73
  # Interfaccia Gradio
 
76
 
77
  with gr.Row():
78
  with gr.Column():
79
+ setup_button = gr.Button("Setup iniziale (esegui una volta)")
80
+ setup_output = gr.Textbox(label="Stato Setup", value="Non eseguito")
81
+
82
+ gr.Markdown("---")
83
+
84
  prompt_input = gr.Textbox(
85
  label="Descrivi il video che vuoi generare",
86
  placeholder="Es. Un panda che mangia bambù in una foresta, stile cinematografico"
 
88
  generate_btn = gr.Button("Genera Video")
89
 
90
  with gr.Column():
91
+ status_output = gr.Textbox(label="Stato Generazione")
92
  video_output = gr.Video(label="Video Generato")
93
 
94
+ # Collega i pulsanti alle funzioni
95
+ setup_button.click(
96
+ fn=perform_setup,
97
+ outputs=[setup_output]
98
+ )
99
+
100
  generate_btn.click(
101
  fn=generate_video,
102
  inputs=[prompt_input],