File size: 3,595 Bytes
cfcfe98
76f26db
cfcfe98
 
 
b6dcd96
cfcfe98
 
 
883f8b8
cfcfe98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
883f8b8
 
cfcfe98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6dcd96
cfcfe98
045423f
cfcfe98
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
import gradio as gr
import requests
import os
from pathlib import Path
import time

# Kling AI API configuration
KLING_API_URL = "https://api.klingai.com/v1/video"  # Hypothetical endpoint; replace with actual Kling AI API endpoint
KLING_SECRET_KEY = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde"  # Provided secret key

def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
    """
    Function to call Kling AI API for text-to-video generation.
    
    Args:
        prompt (str): Text prompt for video generation
        negative_prompt (str): Optional negative prompt to guide generation
        duration (int): Video duration in seconds
        aspect_ratio (str): Aspect ratio (e.g., '16:9')
    
    Returns:
        str: Path to the downloaded video or error message
    """
    headers = {
        "Authorization": f"Bearer {KLING_SECRET_KEY}",
        "Content-Type": "application/json"
    }
    
    payload = {
        "prompt": prompt,
        "negative_prompt": negative_prompt,
        "duration": duration,
        "aspect_ratio": aspect_ratio
    }
    
    try:
        # Make API request to Kling AI
        response = requests.post(KLING_API_URL, json=payload, headers=headers, timeout=60)
        response.raise_for_status()  # Raise exception for bad status codes
        
        # Assuming the API returns a JSON with a video URL or file
        data = response.json()
        video_url = data.get("video_url")  # Adjust based on actual API response structure
        
        if not video_url:
            return "Error: No video URL returned by the API."
        
        # Download the video
        video_response = requests.get(video_url, timeout=30)
        video_response.raise_for_status()
        
        # Save the video to a temporary file
        output_path = Path("output_video.mp4")
        with open(output_path, "wb") as f:
            f.write(video_response.content)
        
        return str(output_path)
    
    except requests.exceptions.RequestException as e:
        return f"Error: Failed to generate video. Details: {str(e)}"

def chatbot_interface(prompt, negative_prompt, duration, aspect_ratio):
    """
    Gradio interface function to handle user inputs and display results.
    
    Args:
        prompt (str): Text prompt for video generation
        negative_prompt (str): Negative prompt to avoid certain elements
        duration (int): Video duration in seconds
        aspect_ratio (str): Aspect ratio selection
    
    Returns:
        Video or str: Path to the video file or error message
    """
    if not prompt.strip():
        return "Error: Please provide a valid prompt."
    
    result = generate_video(prompt, negative_prompt, duration, aspect_ratio)
    return result

# Define Gradio interface
iface = gr.Interface(
    fn=chatbot_interface,
    inputs=[
        gr.Textbox(lines=2, placeholder="Enter your video generation prompt here...", label="Prompt"),
        gr.Textbox(lines=2, placeholder="Enter negative prompt (optional)", label="Negative Prompt"),
        gr.Slider(minimum=1, maximum=30, step=1, value=5, label="Video Duration (seconds)"),
        gr.Dropdown(choices=["16:9", "4:3", "1:1"], value="16:9", label="Aspect Ratio")
    ],
    outputs=gr.Video(label="Generated Video"),
    title="Kling AI Video Generator",
    description="Generate videos using Kling AI API. Enter a prompt, optionally a negative prompt, select duration and aspect ratio, and get your video!"
)

# Launch the interface
if __name__ == "__main__":
    iface.launch(server_name="0.0.0.0", server_port=7860)