Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,17 +3,17 @@ import requests
|
|
| 3 |
import os
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
-
# Retrieve Kling AI API key from environment variable (set as a
|
| 7 |
KLING_SECRET_KEY = os.environ.get("KLING_SECRET_KEY")
|
| 8 |
KLING_API_URL = "https://api.klingai.com/v1/video" # Replace with actual Kling AI API endpoint
|
| 9 |
|
| 10 |
def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
| 11 |
"""
|
| 12 |
-
Call Kling AI API to generate a video
|
| 13 |
|
| 14 |
Args:
|
| 15 |
prompt (str): Text prompt for video generation
|
| 16 |
-
negative_prompt (str): Optional negative prompt
|
| 17 |
duration (int): Video duration in seconds
|
| 18 |
aspect_ratio (str): Aspect ratio (e.g., '16:9')
|
| 19 |
|
|
@@ -21,7 +21,7 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
|
| 21 |
str: Path to the downloaded video or error message
|
| 22 |
"""
|
| 23 |
if not KLING_SECRET_KEY:
|
| 24 |
-
return "Error: KLING_SECRET_KEY
|
| 25 |
|
| 26 |
headers = {
|
| 27 |
"Authorization": f"Bearer {KLING_SECRET_KEY}",
|
|
@@ -36,13 +36,12 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
|
| 36 |
}
|
| 37 |
|
| 38 |
try:
|
| 39 |
-
# Make API request to Kling AI
|
| 40 |
response = requests.post(KLING_API_URL, json=payload, headers=headers, timeout=60)
|
| 41 |
response.raise_for_status()
|
| 42 |
|
| 43 |
# Assuming API returns a JSON with a video URL
|
| 44 |
data = response.json()
|
| 45 |
-
video_url = data.get("video_url") # Adjust based on actual API response
|
| 46 |
|
| 47 |
if not video_url:
|
| 48 |
return "Error: No video URL returned by the API."
|
|
@@ -51,7 +50,7 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
|
| 51 |
video_response = requests.get(video_url, timeout=30)
|
| 52 |
video_response.raise_for_status()
|
| 53 |
|
| 54 |
-
# Save video to
|
| 55 |
output_path = Path("output_video.mp4")
|
| 56 |
with open(output_path, "wb") as f:
|
| 57 |
f.write(video_response.content)
|
|
@@ -63,37 +62,36 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
|
| 63 |
|
| 64 |
def chatbot_interface(prompt, negative_prompt, duration, aspect_ratio):
|
| 65 |
"""
|
| 66 |
-
Gradio interface
|
| 67 |
|
| 68 |
Args:
|
| 69 |
-
prompt (str): Text prompt
|
| 70 |
-
negative_prompt (str): Negative prompt
|
| 71 |
-
duration (int): Video duration
|
| 72 |
-
aspect_ratio (str): Aspect ratio
|
| 73 |
|
| 74 |
Returns:
|
| 75 |
-
Video
|
| 76 |
"""
|
| 77 |
if not prompt.strip():
|
| 78 |
return "Error: Please provide a valid prompt."
|
| 79 |
|
| 80 |
-
|
| 81 |
-
return result
|
| 82 |
|
| 83 |
# Define Gradio interface
|
| 84 |
iface = gr.Interface(
|
| 85 |
fn=chatbot_interface,
|
| 86 |
inputs=[
|
| 87 |
-
gr.Textbox(lines=2, placeholder="Enter your video generation prompt
|
| 88 |
gr.Textbox(lines=2, placeholder="Enter negative prompt (optional)", label="Negative Prompt"),
|
| 89 |
gr.Slider(minimum=1, maximum=30, step=1, value=5, label="Video Duration (seconds)"),
|
| 90 |
gr.Dropdown(choices=["16:9", "4:3", "1:1"], value="16:9", label="Aspect Ratio")
|
| 91 |
],
|
| 92 |
outputs=gr.Video(label="Generated Video"),
|
| 93 |
title="Kling AI Video Generator",
|
| 94 |
-
description="Generate videos using Kling AI API. Enter a prompt
|
| 95 |
)
|
| 96 |
|
| 97 |
-
# Launch the interface
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
iface.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
from pathlib import Path
|
| 5 |
|
| 6 |
+
# Retrieve Kling AI API key from environment variable (set as a secret in Hugging Face Spaces)
|
| 7 |
KLING_SECRET_KEY = os.environ.get("KLING_SECRET_KEY")
|
| 8 |
KLING_API_URL = "https://api.klingai.com/v1/video" # Replace with actual Kling AI API endpoint
|
| 9 |
|
| 10 |
def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
| 11 |
"""
|
| 12 |
+
Call Kling AI API to generate a video from a text prompt.
|
| 13 |
|
| 14 |
Args:
|
| 15 |
prompt (str): Text prompt for video generation
|
| 16 |
+
negative_prompt (str): Optional negative prompt
|
| 17 |
duration (int): Video duration in seconds
|
| 18 |
aspect_ratio (str): Aspect ratio (e.g., '16:9')
|
| 19 |
|
|
|
|
| 21 |
str: Path to the downloaded video or error message
|
| 22 |
"""
|
| 23 |
if not KLING_SECRET_KEY:
|
| 24 |
+
return "Error: KLING_SECRET_KEY not set in environment variables."
|
| 25 |
|
| 26 |
headers = {
|
| 27 |
"Authorization": f"Bearer {KLING_SECRET_KEY}",
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
try:
|
|
|
|
| 39 |
response = requests.post(KLING_API_URL, json=payload, headers=headers, timeout=60)
|
| 40 |
response.raise_for_status()
|
| 41 |
|
| 42 |
# Assuming API returns a JSON with a video URL
|
| 43 |
data = response.json()
|
| 44 |
+
video_url = data.get("video_url") # Adjust based on actual API response
|
| 45 |
|
| 46 |
if not video_url:
|
| 47 |
return "Error: No video URL returned by the API."
|
|
|
|
| 50 |
video_response = requests.get(video_url, timeout=30)
|
| 51 |
video_response.raise_for_status()
|
| 52 |
|
| 53 |
+
# Save video to temporary file
|
| 54 |
output_path = Path("output_video.mp4")
|
| 55 |
with open(output_path, "wb") as f:
|
| 56 |
f.write(video_response.content)
|
|
|
|
| 62 |
|
| 63 |
def chatbot_interface(prompt, negative_prompt, duration, aspect_ratio):
|
| 64 |
"""
|
| 65 |
+
Gradio interface for user inputs and video output.
|
| 66 |
|
| 67 |
Args:
|
| 68 |
+
prompt (str): Text prompt
|
| 69 |
+
negative_prompt (str): Negative prompt
|
| 70 |
+
duration (int): Video duration
|
| 71 |
+
aspect_ratio (str): Aspect ratio
|
| 72 |
|
| 73 |
Returns:
|
| 74 |
+
Video file path or error message
|
| 75 |
"""
|
| 76 |
if not prompt.strip():
|
| 77 |
return "Error: Please provide a valid prompt."
|
| 78 |
|
| 79 |
+
return generate_video(prompt, negative_prompt, duration, aspect_ratio)
|
|
|
|
| 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...", 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 and settings to create your video!"
|
| 93 |
)
|
| 94 |
|
| 95 |
+
# Launch the interface
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|