Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,97 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
import requests
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
KLING_API_KEY = os.getenv("KLING_API_KEY")
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
if __name__ == "__main__":
|
| 33 |
-
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
| 2 |
import requests
|
| 3 |
+
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
+
# Kling AI API configuration
|
| 8 |
+
KLING_API_URL = "https://api.klingai.com/v1/video" # Hypothetical endpoint; replace with actual Kling AI API endpoint
|
| 9 |
+
KLING_SECRET_KEY = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde" # Provided secret key
|
|
|
|
| 10 |
|
| 11 |
+
def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
| 12 |
+
"""
|
| 13 |
+
Function to call Kling AI API for text-to-video generation.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
prompt (str): Text prompt for video generation
|
| 17 |
+
negative_prompt (str): Optional negative prompt to guide generation
|
| 18 |
+
duration (int): Video duration in seconds
|
| 19 |
+
aspect_ratio (str): Aspect ratio (e.g., '16:9')
|
| 20 |
+
|
| 21 |
+
Returns:
|
| 22 |
+
str: Path to the downloaded video or error message
|
| 23 |
+
"""
|
| 24 |
+
headers = {
|
| 25 |
+
"Authorization": f"Bearer {KLING_SECRET_KEY}",
|
| 26 |
+
"Content-Type": "application/json"
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
payload = {
|
| 30 |
+
"prompt": prompt,
|
| 31 |
+
"negative_prompt": negative_prompt,
|
| 32 |
+
"duration": duration,
|
| 33 |
+
"aspect_ratio": aspect_ratio
|
| 34 |
+
}
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
# Make API request to Kling AI
|
| 38 |
+
response = requests.post(KLING_API_URL, json=payload, headers=headers, timeout=60)
|
| 39 |
+
response.raise_for_status() # Raise exception for bad status codes
|
| 40 |
+
|
| 41 |
+
# Assuming the API returns a JSON with a video URL or file
|
| 42 |
+
data = response.json()
|
| 43 |
+
video_url = data.get("video_url") # Adjust based on actual API response structure
|
| 44 |
+
|
| 45 |
+
if not video_url:
|
| 46 |
+
return "Error: No video URL returned by the API."
|
| 47 |
+
|
| 48 |
+
# Download the video
|
| 49 |
+
video_response = requests.get(video_url, timeout=30)
|
| 50 |
+
video_response.raise_for_status()
|
| 51 |
+
|
| 52 |
+
# Save the video to a temporary file
|
| 53 |
+
output_path = Path("output_video.mp4")
|
| 54 |
+
with open(output_path, "wb") as f:
|
| 55 |
+
f.write(video_response.content)
|
| 56 |
+
|
| 57 |
+
return str(output_path)
|
| 58 |
+
|
| 59 |
+
except requests.exceptions.RequestException as e:
|
| 60 |
+
return f"Error: Failed to generate video. Details: {str(e)}"
|
| 61 |
+
|
| 62 |
+
def chatbot_interface(prompt, negative_prompt, duration, aspect_ratio):
|
| 63 |
+
"""
|
| 64 |
+
Gradio interface function to handle user inputs and display results.
|
| 65 |
+
|
| 66 |
+
Args:
|
| 67 |
+
prompt (str): Text prompt for video generation
|
| 68 |
+
negative_prompt (str): Negative prompt to avoid certain elements
|
| 69 |
+
duration (int): Video duration in seconds
|
| 70 |
+
aspect_ratio (str): Aspect ratio selection
|
| 71 |
+
|
| 72 |
+
Returns:
|
| 73 |
+
Video or str: Path to the video file or error message
|
| 74 |
+
"""
|
| 75 |
+
if not prompt.strip():
|
| 76 |
+
return "Error: Please provide a valid prompt."
|
| 77 |
+
|
| 78 |
+
result = generate_video(prompt, negative_prompt, duration, aspect_ratio)
|
| 79 |
+
return result
|
| 80 |
+
|
| 81 |
+
# Define Gradio interface
|
| 82 |
+
iface = gr.Interface(
|
| 83 |
+
fn=chatbot_interface,
|
| 84 |
+
inputs=[
|
| 85 |
+
gr.Textbox(lines=2, placeholder="Enter your video generation prompt here...", label="Prompt"),
|
| 86 |
+
gr.Textbox(lines=2, placeholder="Enter negative prompt (optional)", label="Negative Prompt"),
|
| 87 |
+
gr.Slider(minimum=1, maximum=30, step=1, value=5, label="Video Duration (seconds)"),
|
| 88 |
+
gr.Dropdown(choices=["16:9", "4:3", "1:1"], value="16:9", label="Aspect Ratio")
|
| 89 |
+
],
|
| 90 |
+
outputs=gr.Video(label="Generated Video"),
|
| 91 |
+
title="Kling AI Video Generator",
|
| 92 |
+
description="Generate videos using Kling AI API. Enter a prompt, optionally a negative prompt, select duration and aspect ratio, and get your video!"
|
| 93 |
+
)
|
| 94 |
|
| 95 |
+
# Launch the interface
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|