Spaces:
Sleeping
Sleeping
| 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) |