AnthonyOlatunji commited on
Commit
99cb38f
Β·
verified Β·
1 Parent(s): e3e897d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -38
app.py CHANGED
@@ -1,48 +1,34 @@
1
- # app.py
2
  import os
3
- import gradio as gr
4
  from huggingface_hub import InferenceClient
 
5
 
6
- # ── CONFIG ─────────────────────────────────────────────────────────────────────
7
-
8
- # (1) Your HF token must be set in Secrets as HF_HUB_TOKEN
9
- HF_TOKEN = os.getenv("HF_HUB_TOKEN", None)
10
- if not HF_TOKEN:
11
- raise RuntimeError("Please set a HF_HUB_TOKEN in your Space secrets!")
12
-
13
- # (2) Model to call
14
- MODEL_ID = "damo-vilab/text-to-video-ms-1.7b"
15
-
16
- # Initialize the HF Inference client
17
- client = InferenceClient(token=HF_TOKEN)
18
 
19
- # ── GENERATION FUNCTION ────────────────────────────────────────────────────────
 
 
 
 
 
20
 
 
 
21
  def generate_video(prompt: str) -> str:
22
- """
23
- Calls HF's text-to-video API, writes out an MP4, and returns its path.
24
- """
25
- out_path = "/tmp/out.mp4"
26
-
27
- # 1) Fire off the inference request
28
- # The `text_to_video` method returns raw bytes for the first generated video.
29
- result = client.text_to_video(model=MODEL_ID, inputs=prompt)
30
-
31
- # 2) Save to disk
32
- video_bytes = result["generated_video"] # bytes
33
  with open(out_path, "wb") as f:
34
  f.write(video_bytes)
35
-
36
  return out_path
37
 
38
- # ── GRADIO UI ─────────────────────────────────────────────────────────────────
39
-
40
- with gr.Blocks() as demo:
41
- gr.Markdown("## 🎬 Text-to-Video Generator")
42
- prompt = gr.Textbox(label="Enter your prompt here", placeholder="a chicken crosses the road")
43
- video_out = gr.Video(label="Generated Video")
44
- btn = gr.Button("Generate Video")
45
-
46
- btn.click(fn=generate_video, inputs=prompt, outputs=video_out)
47
-
48
- demo.launch(share=True)
 
 
1
  import os
 
2
  from huggingface_hub import InferenceClient
3
+ import gradio as gr
4
 
5
+ # ─── STEP 1 ───
6
+ # Paste your HF token here (bypasses the need for the Spaces secrets UI)
7
+ os.environ["HF_HUB_TOKEN"] = "YOUR_HF_TOKEN_HERE"
 
 
 
 
 
 
 
 
 
8
 
9
+ # ─── STEP 2 ───
10
+ # Initialize the InferenceClient
11
+ client = InferenceClient(
12
+ repo_id="ali-vilab/text-to-video-ms-1.7b",
13
+ token=os.environ["HF_HUB_TOKEN"],
14
+ )
15
 
16
+ # ─── STEP 3 ───
17
+ # Define the function that takes a prompt, calls the API, and writes out an MP4
18
  def generate_video(prompt: str) -> str:
19
+ result = client.text_to_video(prompt) # {'video': b'...'}
20
+ video_bytes = result["video"]
21
+ out_path = "out.mp4"
 
 
 
 
 
 
 
 
22
  with open(out_path, "wb") as f:
23
  f.write(video_bytes)
 
24
  return out_path
25
 
26
+ # ─── STEP 4 ───
27
+ # Wire it up in Gradio
28
+ demo = gr.Interface(
29
+ fn=generate_video,
30
+ inputs=gr.Textbox(label="Enter your prompt here"),
31
+ outputs=gr.Video(label="Generated Video"),
32
+ title="Video Generator",
33
+ )
34
+ demo.launch(server_name="0.0.0.0", server_port=7860)