File size: 3,281 Bytes
3203372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import gradio as gr
import requests

def generate_video(prompt, duration, progress=gr.Progress()):
    """Generate video using akhaliq-Sora-2 API"""
    if not prompt.strip():
        raise gr.Error("Please enter a text prompt")
    
    progress(0.1, desc="Starting video generation...")
    
    try:
        # API endpoint for akhaliq-Sora-2
        api_url = "https://akhaliq-sora-2.hf.space/api/predict"
        
        # Prepare the payload
        payload = {
            "data": [
                prompt,  # text prompt
                duration,  # duration in seconds
                30,  # fps (fixed)
                512,  # width (fixed)
                512,  # height (fixed)
            ]
        }
        
        progress(0.5, desc="Generating video...")
        
        # Make the API request
        response = requests.post(api_url, json=payload)
        
        if response.status_code == 200:
            progress(0.9, desc="Processing...")
            result = response.json()
            
            # Check if the generation is complete
            if "data" in result and len(result["data"]) > 0:
                video_url = result["data"][0]
                if video_url:
                    progress(1.0, desc="Complete!")
                    return video_url
                else:
                    raise gr.Error("Video generation failed")
            else:
                raise gr.Error("Invalid response from API")
        else:
            raise gr.Error(f"API request failed with status code: {response.status_code}")
            
    except requests.exceptions.RequestException as e:
        raise gr.Error(f"Network error: {str(e)}")
    except Exception as e:
        raise gr.Error(f"Error generating video: {str(e)}")

# Create the Gradio interface
with gr.Blocks(title="Text-to-Video AI Generator") as demo:
    
    gr.Markdown("# 🎬 Text-to-Video AI Generator")
    gr.Markdown("Transform your text into AI-generated videos using Sora-2 model")
    gr.Markdown('<p><a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">Built with anycoder</a></p>')
    
    with gr.Row():
        with gr.Column():
            prompt_input = gr.Textbox(
                label="Video Description",
                placeholder="Describe the video you want to generate...",
                lines=3
            )
            
            duration_slider = gr.Slider(
                minimum=1,
                maximum=10,
                value=4,
                step=1,
                label="Video Duration (seconds)"
            )
            
            generate_btn = gr.Button("Generate Video", variant="primary")
        
        with gr.Column():
            video_output = gr.Video(
                label="Generated Video",
                height=400
            )
    
    gr.Examples(
        examples=[
            ["A cat playing piano", 4],
            ["A robot dancing", 6],
            ["A sunset over the ocean", 3],
        ],
        inputs=[prompt_input, duration_slider]
    )
    
    generate_btn.click(
        fn=generate_video,
        inputs=[prompt_input, duration_slider],
        outputs=[video_output],
        show_progress=True
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()