Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,123 @@
|
|
| 1 |
-
|
| 2 |
-
from
|
| 3 |
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from huggingface_hub import hf_hub_download
|
| 3 |
import subprocess
|
| 4 |
+
import tempfile
|
| 5 |
+
import shutil
|
| 6 |
+
import os
|
| 7 |
+
import spaces
|
| 8 |
|
| 9 |
+
from transformers import T5ForConditionalGeneration, T5Tokenizer
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def install_apex():
|
| 16 |
+
# Install Apex in editable mode from the specified GitHub repository
|
| 17 |
+
cmd = [
|
| 18 |
+
'pip', 'install', '--no-cache-dir', '--no-build-isolation',
|
| 19 |
+
'--config-settings', 'build-option=--cpp_ext', '--config-settings',
|
| 20 |
+
'build-option=--cuda_ext', '-e', 'git+https://github.com/NVIDIA/apex.git'
|
| 21 |
+
]
|
| 22 |
+
subprocess.run(cmd, check=True)
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
import apex
|
| 26 |
+
except ModuleNotFoundError:
|
| 27 |
+
print("Apex not found, installing...")
|
| 28 |
+
install_apex()
|
| 29 |
+
# Try to import Apex again after installation
|
| 30 |
+
import apex
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def download_t5_model(model_id, save_directory):
|
| 34 |
+
# Modelin tokenizer'ını ve modeli indir
|
| 35 |
+
model = T5ForConditionalGeneration.from_pretrained(model_id)
|
| 36 |
+
tokenizer = T5Tokenizer.from_pretrained(model_id)
|
| 37 |
+
|
| 38 |
+
# Model ve tokenizer'ı belirtilen dizine kaydet
|
| 39 |
+
if not os.path.exists(save_directory):
|
| 40 |
+
os.makedirs(save_directory)
|
| 41 |
+
model.save_pretrained(save_directory)
|
| 42 |
+
tokenizer.save_pretrained(save_directory)
|
| 43 |
+
|
| 44 |
+
# Model ID ve kaydedilecek dizin
|
| 45 |
+
model_id = "DeepFloyd/t5-v1_1-xxl"
|
| 46 |
+
save_directory = "pretrained_models/t5_ckpts/t5-v1_1-xxl"
|
| 47 |
+
|
| 48 |
+
# Modeli indir
|
| 49 |
+
download_t5_model(model_id, save_directory)
|
| 50 |
+
|
| 51 |
+
def download_model(repo_id, model_name):
|
| 52 |
+
model_path = hf_hub_download(repo_id=repo_id, filename=model_name)
|
| 53 |
+
return model_path
|
| 54 |
+
|
| 55 |
+
import glob
|
| 56 |
+
|
| 57 |
+
@spaces.GPU
|
| 58 |
+
def run_inference(model_name, prompt_text):
|
| 59 |
+
repo_id = "hpcai-tech/Open-Sora"
|
| 60 |
+
|
| 61 |
+
# Map model names to their respective configuration files
|
| 62 |
+
config_mapping = {
|
| 63 |
+
"OpenSora-v1-16x256x256.pth": "configs/opensora/inference/16x256x256.py",
|
| 64 |
+
"OpenSora-v1-HQ-16x256x256.pth": "configs/opensora/inference/16x512x512.py",
|
| 65 |
+
"OpenSora-v1-HQ-16x512x512.pth": "configs/opensora/inference/64x512x512.py"
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
config_path = config_mapping[model_name]
|
| 69 |
+
ckpt_path = download_model(repo_id, model_name)
|
| 70 |
+
|
| 71 |
+
# Save prompt_text to a temporary text file
|
| 72 |
+
prompt_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode='w')
|
| 73 |
+
prompt_file.write(prompt_text)
|
| 74 |
+
prompt_file.close()
|
| 75 |
+
|
| 76 |
+
with open(config_path, 'r') as file:
|
| 77 |
+
config_content = file.read()
|
| 78 |
+
config_content = config_content.replace('prompt_path = "./assets/texts/t2v_samples.txt"', f'prompt_path = "{prompt_file.name}"')
|
| 79 |
+
|
| 80 |
+
with tempfile.NamedTemporaryFile('w', delete=False, suffix='.py') as temp_file:
|
| 81 |
+
temp_file.write(config_content)
|
| 82 |
+
temp_config_path = temp_file.name
|
| 83 |
+
|
| 84 |
+
cmd = [
|
| 85 |
+
"torchrun", "--standalone", "--nproc_per_node", "1",
|
| 86 |
+
"scripts/inference.py", temp_config_path,
|
| 87 |
+
"--ckpt-path", ckpt_path
|
| 88 |
+
]
|
| 89 |
+
subprocess.run(cmd)
|
| 90 |
+
|
| 91 |
+
save_dir = "./outputs/samples/" # Örneğin, inference.py tarafından kullanılan kayıt dizini
|
| 92 |
+
list_of_files = glob.glob(f'{save_dir}/*')
|
| 93 |
+
if list_of_files:
|
| 94 |
+
latest_file = max(list_of_files, key=os.path.getctime)
|
| 95 |
+
return latest_file
|
| 96 |
+
else:
|
| 97 |
+
print("No files found in the output directory.")
|
| 98 |
+
return None
|
| 99 |
+
|
| 100 |
+
# Clean up the temporary files
|
| 101 |
+
os.remove(temp_file.name)
|
| 102 |
+
os.remove(prompt_file.name)
|
| 103 |
+
|
| 104 |
+
def main():
|
| 105 |
+
gr.Interface(
|
| 106 |
+
fn=run_inference,
|
| 107 |
+
inputs=[
|
| 108 |
+
gr.Dropdown(choices=[
|
| 109 |
+
"OpenSora-v1-16x256x256.pth",
|
| 110 |
+
"OpenSora-v1-HQ-16x256x256.pth",
|
| 111 |
+
"OpenSora-v1-HQ-16x512x512.pth"
|
| 112 |
+
],
|
| 113 |
+
value="OpenSora-v1-16x256x256.pth",
|
| 114 |
+
label="Model Selection"),
|
| 115 |
+
gr.Textbox(label="Prompt Text", value="Enter prompt text here")
|
| 116 |
+
],
|
| 117 |
+
outputs=gr.Video(label="Output Video"),
|
| 118 |
+
title="Open-Sora Inference",
|
| 119 |
+
description="Run Open-Sora Inference with Custom Parameters",
|
| 120 |
+
).launch()
|
| 121 |
+
|
| 122 |
+
if __name__ == "__main__":
|
| 123 |
+
main()
|