AnthonyOlatunji commited on
Commit
29b20e5
Β·
verified Β·
1 Parent(s): d764024

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -27
app.py CHANGED
@@ -1,35 +1,40 @@
1
  import os
2
- from huggingface_hub import InferenceClient
3
  import gradio as gr
 
4
 
5
- # β€”β€”β€”β€”β€”β€”β€”β€” CONFIG β€”β€”β€”β€”β€”β€”β€”β€”
6
- # (You’ve already set HF_HUB_TOKEN in your Space’s Secrets UI)
7
- hf_token = os.environ["HF_HUB_TOKEN"]
 
8
 
9
- # Initialize the client against the text-to-video model
10
- client = InferenceClient(
11
- model="ali-vilab/text-to-video-ms-1.7b",
12
- token=hf_token,
13
- )
14
 
15
- # β€”β€”β€”β€”β€”β€”β€”β€” LOGIC β€”β€”β€”β€”β€”β€”β€”β€”
16
  def generate_video(prompt: str) -> str:
17
- # call the HF text-to-video endpoint
18
- res = client.text_to_video(prompt) # ➞ {'video': b'...'}
19
- video_bytes = res["video"]
20
- out_path = "out.mp4"
21
- with open(out_path, "wb") as f:
22
- f.write(video_bytes)
23
- return out_path
 
 
 
 
 
 
 
24
 
25
- # β€”β€”β€”β€”β€”β€”β€”β€” UI β€”β€”β€”β€”β€”β€”β€”β€”
26
- demo = gr.Interface(
27
- fn=generate_video,
28
- inputs=gr.Text(label="Enter your prompt here"),
29
- outputs=gr.Video(label="Generated Video"),
30
- title="🎬 Video Generator",
31
- description="Generate a short MP4 from your text prompt via the HF Inference API"
32
- )
33
 
34
- if __name__ == "__main__":
35
- demo.launch()
 
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()