AkashKumarave commited on
Commit
81068b9
·
verified ·
1 Parent(s): 90b5ca5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
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
- # 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
 
20
  Returns:
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}",
 
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
- prompt (str): Text prompt
 
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 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
 
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