Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,40 @@
|
|
| 1 |
import os
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
client = InferenceClient(
|
| 11 |
-
model="ali-vilab/text-to-video-ms-1.7b",
|
| 12 |
-
token=hf_token,
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def generate_video(prompt: str) -> str:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
)
|
| 33 |
|
| 34 |
-
|
| 35 |
-
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
|
| 5 |
+
# βββ STEP 1: read your token from the Spaces secrets
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_HUB_TOKEN")
|
| 7 |
+
if HF_TOKEN is None:
|
| 8 |
+
raise RuntimeError("β Please add your HF_HUB_TOKEN under Settings β Variables and secrets")
|
| 9 |
|
| 10 |
+
# βββ STEP 2: initialize the client (no repo_id arg any more)
|
| 11 |
+
client = InferenceClient(token=HF_TOKEN)
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# βββ STEP 3: the generation function
|
| 14 |
def generate_video(prompt: str) -> str:
|
| 15 |
+
try:
|
| 16 |
+
# pass your model ID as the first arg, and the text as `inputs=β¦`
|
| 17 |
+
result = client.text_to_video(
|
| 18 |
+
"ali-vilab/text-to-video-ms-1.7b",
|
| 19 |
+
inputs=prompt
|
| 20 |
+
)
|
| 21 |
+
video_bytes = result["video"]
|
| 22 |
+
out_path = "output.mp4"
|
| 23 |
+
with open(out_path, "wb") as f:
|
| 24 |
+
f.write(video_bytes)
|
| 25 |
+
return out_path
|
| 26 |
+
except Exception as e:
|
| 27 |
+
# re-raise so Gradio logs it for you
|
| 28 |
+
raise RuntimeError(f"Video generation failed: {e}")
|
| 29 |
|
| 30 |
+
# βββ STEP 4: wire it up in Gradio
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("## π¬ Video Generator")
|
| 33 |
+
with gr.Row():
|
| 34 |
+
inp = gr.Textbox(placeholder="Type your prompt here", label="Enter your prompt")
|
| 35 |
+
vid = gr.Video(label="Generated Video")
|
| 36 |
+
btn = gr.Button("Generate")
|
| 37 |
+
btn.click(fn=generate_video, inputs=inp, outputs=vid)
|
| 38 |
|
| 39 |
+
# βββ STEP 5: launch
|
| 40 |
+
demo.launch()
|