Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,43 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
import os
|
| 4 |
from pathlib import Path
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 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
|
| 13 |
|
| 14 |
Args:
|
| 15 |
-
|
|
|
|
| 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 |
|
| 20 |
Returns:
|
| 21 |
str: Path to the downloaded video or error message
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
headers = {
|
| 27 |
-
"Authorization": f"Bearer {
|
|
|
|
| 28 |
"Content-Type": "application/json"
|
| 29 |
}
|
| 30 |
|
| 31 |
payload = {
|
|
|
|
| 32 |
"prompt": prompt,
|
| 33 |
"negative_prompt": negative_prompt,
|
| 34 |
"duration": duration,
|
|
@@ -60,12 +69,13 @@ def generate_video(prompt, negative_prompt="", duration=5, aspect_ratio="16:9"):
|
|
| 60 |
except requests.exceptions.RequestException as e:
|
| 61 |
return f"Error: Failed to generate video. Details: {str(e)}"
|
| 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 |
-
|
|
|
|
| 69 |
negative_prompt (str): Negative prompt
|
| 70 |
duration (int): Video duration
|
| 71 |
aspect_ratio (str): Aspect ratio
|
|
@@ -73,23 +83,24 @@ def chatbot_interface(prompt, negative_prompt, duration, aspect_ratio):
|
|
| 73 |
Returns:
|
| 74 |
Video file path or error message
|
| 75 |
"""
|
| 76 |
-
if not
|
| 77 |
-
return "Error: Please
|
| 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.
|
|
|
|
| 86 |
gr.Textbox(lines=2, placeholder="Enter negative prompt (optional)", label="Negative Prompt"),
|
| 87 |
-
gr.Slider(minimum=
|
| 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.
|
| 93 |
)
|
| 94 |
|
| 95 |
# Launch the interface
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
|
|
|
| 3 |
from pathlib import Path
|
| 4 |
+
import base64
|
| 5 |
+
import io
|
| 6 |
|
| 7 |
+
# Kling AI API configuration (keys hardcoded as requested)
|
| 8 |
+
KLING_ACCESS_KEY_ID = "AGBGmadNd9hakFYfahytyQQJtN8CJmDJ"
|
| 9 |
+
KLING_ACCESS_KEY_SECRET = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde"
|
| 10 |
+
KLING_API_URL = "https://api-singapore.klingai.com/v1/image-to-video" # Assumed endpoint for image-to-image video generation
|
| 11 |
|
| 12 |
+
def generate_video(image, prompt="", negative_prompt="", duration=5, aspect_ratio="16:9"):
|
| 13 |
"""
|
| 14 |
+
Call Kling AI API to generate a video from an input image.
|
| 15 |
|
| 16 |
Args:
|
| 17 |
+
image: Uploaded image file (from Gradio)
|
| 18 |
+
prompt (str): Optional text prompt to guide video generation
|
| 19 |
negative_prompt (str): Optional negative prompt
|
| 20 |
+
duration (int): Video duration in seconds (5 or 10)
|
| 21 |
aspect_ratio (str): Aspect ratio (e.g., '16:9')
|
| 22 |
|
| 23 |
Returns:
|
| 24 |
str: Path to the downloaded video or error message
|
| 25 |
"""
|
| 26 |
+
# Convert image to base64 for API submission
|
| 27 |
+
try:
|
| 28 |
+
with open(image, "rb") as img_file:
|
| 29 |
+
image_base64 = base64.b64encode(img_file.read()).decode("utf-8")
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error: Failed to process input image. Details: {str(e)}"
|
| 32 |
|
| 33 |
headers = {
|
| 34 |
+
"Authorization": f"Bearer {KLING_ACCESS_KEY_SECRET}",
|
| 35 |
+
"X-Kling-Access-Key-Id": KLING_ACCESS_KEY_ID,
|
| 36 |
"Content-Type": "application/json"
|
| 37 |
}
|
| 38 |
|
| 39 |
payload = {
|
| 40 |
+
"image": image_base64,
|
| 41 |
"prompt": prompt,
|
| 42 |
"negative_prompt": negative_prompt,
|
| 43 |
"duration": duration,
|
|
|
|
| 69 |
except requests.exceptions.RequestException as e:
|
| 70 |
return f"Error: Failed to generate video. Details: {str(e)}"
|
| 71 |
|
| 72 |
+
def chatbot_interface(image, prompt, negative_prompt, duration, aspect_ratio):
|
| 73 |
"""
|
| 74 |
Gradio interface for user inputs and video output.
|
| 75 |
|
| 76 |
Args:
|
| 77 |
+
image: Uploaded image file
|
| 78 |
+
prompt (str): Optional text prompt
|
| 79 |
negative_prompt (str): Negative prompt
|
| 80 |
duration (int): Video duration
|
| 81 |
aspect_ratio (str): Aspect ratio
|
|
|
|
| 83 |
Returns:
|
| 84 |
Video file path or error message
|
| 85 |
"""
|
| 86 |
+
if not image:
|
| 87 |
+
return "Error: Please upload an image."
|
| 88 |
|
| 89 |
+
return generate_video(image, prompt, negative_prompt, duration, aspect_ratio)
|
| 90 |
|
| 91 |
# Define Gradio interface
|
| 92 |
iface = gr.Interface(
|
| 93 |
fn=chatbot_interface,
|
| 94 |
inputs=[
|
| 95 |
+
gr.Image(type="filepath", label="Input Image"),
|
| 96 |
+
gr.Textbox(lines=2, placeholder="Enter an optional text prompt...", label="Prompt"),
|
| 97 |
gr.Textbox(lines=2, placeholder="Enter negative prompt (optional)", label="Negative Prompt"),
|
| 98 |
+
gr.Slider(minimum=5, maximum=10, step=5, value=5, label="Video Duration (seconds)"),
|
| 99 |
gr.Dropdown(choices=["16:9", "4:3", "1:1"], value="16:9", label="Aspect Ratio")
|
| 100 |
],
|
| 101 |
outputs=gr.Video(label="Generated Video"),
|
| 102 |
+
title="Kling AI Image-to-Video Generator",
|
| 103 |
+
description="Generate videos from an input image using Kling AI API. Upload an image, optionally add a prompt, and set parameters."
|
| 104 |
)
|
| 105 |
|
| 106 |
# Launch the interface
|