File size: 2,481 Bytes
3f975b2
81bb757
3f975b2
81bb757
3f975b2
81bb757
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a461351
 
81bb757
 
 
3f975b2
a461351
3f975b2
 
81bb757
a461351
 
 
 
 
 
3f975b2
a461351
 
81bb757
a461351
81bb757
 
a461351
3f975b2
 
a461351
1
2
3
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
72
73
74
75
76
import gradio as gr
import requests
import random
import time

# FAL endpoint for Wan2.1 T2V
FAL_API_URL = "https://api.fal.ai/fal-ai/wan2.1-t2v-1.3b"

# Function to call fal.ai API
def generate_video(prompt, fps, num_frames, aspect_ratio, guidance_scale, seed, token):
    headers = {
        "Authorization": f"Bearer {token}"
    }

    payload = {
        "prompt": prompt,
        "num_frames": num_frames,
        "fps": fps,
        "aspect_ratio": aspect_ratio,
        "guidance_scale": guidance_scale,
        "negative_prompt": "",
        "seed": int(seed)
    }

    # Call API
    response = requests.post(FAL_API_URL, json=payload, headers=headers)
    response.raise_for_status()
    result = response.json()

    # Poll for video URL
    video_url = None
    for _ in range(20):
        time.sleep(2)
        poll = requests.get(result["urls"]["get"], headers=headers)
        poll.raise_for_status()
        status = poll.json()
        if status["status"] == "COMPLETED":
            video_url = status["output"]["video_url"]
            break
        elif status["status"] == "FAILED":
            raise Exception("Video generation failed.")

    if not video_url:
        raise Exception("Timeout waiting for video.")

    return video_url

# Gradio UI
with gr.Blocks(fill_height=True) as demo:
    with gr.Sidebar():
        gr.Markdown("# Wan2.1 T2V Generator")
        gr.Markdown("Sign in to use this model.")
        login_button = gr.LoginButton()

    gr.Markdown("## 🎬 Generate Video from Text")
    with gr.Row():
        with gr.Column():
            text_input = gr.Textbox(label="Prompt", placeholder="Enter your prompt...")
            fps_slider = gr.Slider(1, 24, value=8, label="FPS")
            frames_slider = gr.Slider(8, 48, value=24, label="Number of Frames")
            aspect_ratio = gr.Dropdown(choices=["square", "landscape", "portrait"], value="square", label="Aspect Ratio")
            guidance_scale = gr.Slider(1, 20, value=15, label="Guidance Scale")
            seed_input = gr.Number(value=random.randint(1, 99999), label="Seed")
            generate_button = gr.Button("Generate Video")
        with gr.Column():
            video_output = gr.Video(label="Generated Video")

    # Wire button to function
    generate_button.click(
        fn=generate_video,
        inputs=[text_input, fps_slider, frames_slider, aspect_ratio, guidance_scale, seed_input, login_button],
        outputs=video_output
    )

demo.launch()