Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import os
|
4 |
+
import sys
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
import tempfile
|
8 |
+
import uuid
|
9 |
+
from datetime import datetime
|
10 |
+
|
11 |
+
# Per garantire che le versioni corrispondano
|
12 |
+
import pkg_resources
|
13 |
+
required_packages = {
|
14 |
+
'torch': '>=2.4.0',
|
15 |
+
'transformers': '>=4.35.0',
|
16 |
+
'diffusers': '>=0.25.0'
|
17 |
+
}
|
18 |
+
|
19 |
+
for package, version in required_packages.items():
|
20 |
+
pkg_resources.require(f"{package}{version}")
|
21 |
+
|
22 |
+
# Clona e installa Open-Sora se necessario
|
23 |
+
if not os.path.exists("Open-Sora"):
|
24 |
+
os.system("git clone https://github.com/hpcaitech/Open-Sora")
|
25 |
+
os.system("cd Open-Sora && pip install -e .")
|
26 |
+
os.system("pip install xformers==0.0.27.post2 --index-url https://download.pytorch.org/whl/cu121")
|
27 |
+
os.system("pip install flash-attn --no-build-isolation")
|
28 |
+
|
29 |
+
# Aggiungi il percorso al PYTHONPATH
|
30 |
+
sys.path.append("Open-Sora")
|
31 |
+
|
32 |
+
# Scarica i modelli se necessario
|
33 |
+
if not os.path.exists("./ckpts"):
|
34 |
+
os.system("mkdir -p ./ckpts")
|
35 |
+
os.system("pip install huggingface_hub")
|
36 |
+
os.system("huggingface-cli download hpcai-tech/Open-Sora-v2 --local-dir ./ckpts")
|
37 |
+
|
38 |
+
# Funzione per la generazione del video
|
39 |
+
def generate_video(prompt):
|
40 |
+
try:
|
41 |
+
# Crea un ID unico per il file di output
|
42 |
+
output_id = str(uuid.uuid4())
|
43 |
+
output_path = f"outputs/{output_id}.mp4"
|
44 |
+
if not os.path.exists("outputs"):
|
45 |
+
os.makedirs("outputs")
|
46 |
+
|
47 |
+
# Comando per generare il video (adattato dalla documentazione)
|
48 |
+
cmd = f"cd Open-Sora && python -m scripts.diffusion.inference \
|
49 |
+
configs/diffusion/inference/256px.py \
|
50 |
+
--cond_type t2v_head \
|
51 |
+
--prompt \"{prompt}\" \
|
52 |
+
--save-dir ../{output_path}"
|
53 |
+
|
54 |
+
print(f"Esecuzione comando: {cmd}")
|
55 |
+
result = os.system(cmd)
|
56 |
+
|
57 |
+
if result != 0:
|
58 |
+
return "Errore durante la generazione del video", None
|
59 |
+
|
60 |
+
video_url = output_path
|
61 |
+
return "Generazione completata", video_url
|
62 |
+
except Exception as e:
|
63 |
+
return f"Errore: {str(e)}", None
|
64 |
+
|
65 |
+
# Interfaccia Gradio
|
66 |
+
with gr.Blocks() as demo:
|
67 |
+
gr.Markdown("# Generatore di Video con Open-Sora")
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column():
|
71 |
+
prompt_input = gr.Textbox(
|
72 |
+
label="Descrivi il video che vuoi generare",
|
73 |
+
placeholder="Es. Un panda che mangia bambù in una foresta, stile cinematografico"
|
74 |
+
)
|
75 |
+
generate_btn = gr.Button("Genera Video")
|
76 |
+
|
77 |
+
with gr.Column():
|
78 |
+
status_output = gr.Textbox(label="Stato")
|
79 |
+
video_output = gr.Video(label="Video Generato")
|
80 |
+
|
81 |
+
generate_btn.click(
|
82 |
+
fn=generate_video,
|
83 |
+
inputs=[prompt_input],
|
84 |
+
outputs=[status_output, video_output]
|
85 |
+
)
|
86 |
+
|
87 |
+
demo.launch()
|