PatoFlamejanteTV commited on
Commit
f7ee6e0
·
verified ·
1 Parent(s): 59febd7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +113 -0
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ import os
3
+ import tempfile
4
+ import gradio as gr
5
+ from pathlib import Path
6
+ import shutil
7
+ import subprocess
8
+ import json
9
+
10
+ HF_TOKEN = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_TOKEN")
11
+
12
+ # Utility: save uploaded file and return path
13
+ def save_temp_file(uploaded):
14
+ tmp = tempfile.mkdtemp()
15
+ if isinstance(uploaded, tuple):
16
+ # gradio file gives (name, fileobj)
17
+ filename = uploaded[0]
18
+ fileobj = uploaded[1]
19
+ path = Path(tmp) / filename
20
+ with open(path, "wb") as f:
21
+ f.write(fileobj.read())
22
+ return str(path)
23
+ else:
24
+ # path-like (when running locally in dev)
25
+ return uploaded
26
+
27
+
28
+ def try_import_sp4d():
29
+ """Try importing a local SP4D pipeline from Stability-AI repo.
30
+ If successful, return pipeline runner function. Otherwise return None.
31
+ """
32
+ try:
33
+ # Users will typically install from the generative-models repo
34
+ # Example: pip install git+https://github.com/Stability-AI/generative-models.git@sp4d
35
+ from sp4d import SP4DPipeline # type: ignore
36
+
37
+ def run_sp4d_local(input_path, guidance_scale=7.5, seed=None):
38
+ # Example stub. Adapt to actual pipeline signature from stabilityai repo.
39
+ pipe = SP4DPipeline.from_pretrained("stabilityai/sp4d")
40
+ # The real pipeline requires video frames tensor input; implement conversions here.
41
+ results = pipe(input_path, guidance_scale=guidance_scale, generator_seed=seed)
42
+ # save outputs to a folder and return path
43
+ out_dir = tempfile.mkdtemp()
44
+ # `results` handling depends on the real pipeline's return format
45
+ # Placeholder: write metadata json
46
+ with open(Path(out_dir)/"metadata.json", "w") as f:
47
+ json.dump({"note": "Replace this stub with actual results handling."}, f)
48
+ return out_dir
49
+
50
+ return run_sp4d_local
51
+ except Exception as e:
52
+ print("SP4D local import failed:", e)
53
+ return None
54
+
55
+
56
+ # If local import not available, create fallback runner that instructs the user
57
+ LOCAL_RUNNER = try_import_sp4d()
58
+
59
+
60
+ def run_inference(file, guidance, seed):
61
+ path = save_temp_file(file)
62
+
63
+ if LOCAL_RUNNER is not None:
64
+ # Run local SP4D pipeline
65
+ try:
66
+ out_dir = LOCAL_RUNNER(path, guidance_scale=guidance, seed=seed)
67
+ # Zip outputs for download
68
+ zip_path = shutil.make_archive(out_dir, "zip", out_dir)
69
+ return (f"Ran local SP4D and produced outputs in {out_dir}", zip_path)
70
+ except Exception as e:
71
+ return (f"Local SP4D run failed: {e}", None)
72
+ else:
73
+ # Provide helpful message + instructions
74
+ instructions = (
75
+ "SP4D is not installed in this Space environment.\n\n"
76
+ "You can either: \n"
77
+ "1) Deploy SP4D by installing the Stability-AI generative-models repo in this Space:\n"
78
+ " pip install git+https://github.com/Stability-AI/generative-models.git@sp4d\n"
79
+ " Then ensure the Space has a GPU (Gradio 'hardware accelerator' = GPU) and restart.\n\n"
80
+ "2) Or run inference via a custom backend (if/when Stability deploys an inference endpoint).\n\n"
81
+ "See README in this Space for full setup steps."
82
+ )
83
+ return (instructions, None)
84
+
85
+
86
+ with gr.Blocks(title="SP4D — Stable Part Diffusion 4D") as demo:
87
+ gr.Markdown("# SP4D — Stable Part Diffusion 4D (UI template)")
88
+ with gr.Row():
89
+ with gr.Column(scale=2):
90
+ inp_file = gr.File(label="Upload 4-frame input (video file or zip of 4 frames)")
91
+ guidance = gr.Slider(0.0, 20.0, value=7.5, label="Guidance scale")
92
+ seed = gr.Number(value=0, label="Seed (0 = random)")
93
+ run_btn = gr.Button("Run SP4D")
94
+ output_text = gr.Textbox(label="Status / Instructions", interactive=False)
95
+ download_output = gr.File(label="Outputs (zip)")
96
+
97
+ with gr.Column(scale=1):
98
+ gr.Markdown(
99
+ """
100
+ ## Notes
101
+ - This Space is a **template** UI. SP4D is large and may require custom installation.
102
+ - See README for installation instructions and licensing notes.
103
+ """
104
+ )
105
+
106
+ def on_run(file, guidance, seed):
107
+ status, out_zip = run_inference(file, guidance, seed if seed != 0 else None)
108
+ return status, out_zip
109
+
110
+ run_btn.click(on_run, inputs=[inp_file, guidance, seed], outputs=[output_text, download_output])
111
+
112
+ if __name__ == "__main__":
113
+ demo.launch()