AkashKumarave commited on
Commit
b92d265
·
verified ·
1 Parent(s): 32be9f0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -8,7 +8,7 @@ import time
8
  # Kling AI API configuration (keys hardcoded as requested)
9
  ACCESS_KEY_ID = "AGBGmadNd9hakFYfahytyQQJtN8CJmDJ"
10
  ACCESS_KEY_SECRET = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde"
11
- API_URL = "https://api-singapore.klingai.com/v1/image-to-video"
12
 
13
  def generate_jwt_token():
14
  """Generate JWT token for Kling AI API authentication."""
@@ -20,16 +20,16 @@ def generate_jwt_token():
20
  token = jwt.encode(payload, ACCESS_KEY_SECRET, algorithm="HS256")
21
  return token
22
 
23
- def generate_video(image, prompt=""):
24
  """
25
- Call Kling AI API to generate a video from an input image.
26
 
27
  Args:
28
  image: Uploaded image file (from Gradio)
29
- prompt (str): Optional text prompt to guide video generation
30
 
31
  Returns:
32
- str: Path to the downloaded video or error message
33
  """
34
  if not image:
35
  return "Error: Please upload a valid image (PNG/JPEG)."
@@ -49,8 +49,8 @@ def generate_video(image, prompt=""):
49
  payload = {
50
  "image": image_base64,
51
  "prompt": prompt,
52
- "duration": 5,
53
- "aspect_ratio": "16:9"
54
  }
55
 
56
  try:
@@ -69,21 +69,21 @@ def generate_video(image, prompt=""):
69
  status_response.raise_for_status()
70
  status_data = status_response.json()
71
  if status_data.get("status") == "succeeded":
72
- video_url = status_data.get("video_url") or status_data.get("result", {}).get("video_url")
73
- if not video_url:
74
- return "Error: No video URL in API response."
75
- # Download the video
76
- video_response = requests.get(video_url, timeout=30)
77
- video_response.raise_for_status()
78
- output_path = Path("output_video.mp4")
79
  with open(output_path, "wb") as f:
80
- f.write(video_response.content)
81
  return str(output_path)
82
  elif status_data.get("status") == "failed":
83
- return "Error: Video generation failed."
84
  time.sleep(5)
85
 
86
- return "Error: Video generation timed out."
87
 
88
  except requests.exceptions.HTTPError as e:
89
  return f"Error: API request failed. Details: {str(e)}"
@@ -92,27 +92,27 @@ def generate_video(image, prompt=""):
92
 
93
  def chatbot_interface(image, prompt):
94
  """
95
- Gradio interface for image-to-video generation.
96
 
97
  Args:
98
  image: Uploaded image file
99
  prompt (str): Optional text prompt
100
 
101
  Returns:
102
- Video file path or error message
103
  """
104
- return generate_video(image, prompt)
105
 
106
  # Define Gradio interface
107
  iface = gr.Interface(
108
  fn=chatbot_interface,
109
  inputs=[
110
  gr.Image(type="filepath", label="Upload Image (PNG/JPEG)"),
111
- gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'A cat running')", label="Prompt")
112
  ],
113
- outputs=gr.Video(label="Generated Video"),
114
- title="Kling AI Image-to-Video Generator",
115
- description="Upload a PNG/JPEG image to generate a 5-second video using Kling AI API."
116
  )
117
 
118
  # Launch the interface
 
8
  # Kling AI API configuration (keys hardcoded as requested)
9
  ACCESS_KEY_ID = "AGBGmadNd9hakFYfahytyQQJtN8CJmDJ"
10
  ACCESS_KEY_SECRET = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde"
11
+ API_URL = "https://api-singapore.klingai.com/v1/image-to-image"
12
 
13
  def generate_jwt_token():
14
  """Generate JWT token for Kling AI API authentication."""
 
20
  token = jwt.encode(payload, ACCESS_KEY_SECRET, algorithm="HS256")
21
  return token
22
 
23
+ def generate_image(image, prompt=""):
24
  """
25
+ Call Kling AI API to generate an image from an input image.
26
 
27
  Args:
28
  image: Uploaded image file (from Gradio)
29
+ prompt (str): Optional text prompt to guide image transformation
30
 
31
  Returns:
32
+ str: Path to the generated image or error message
33
  """
34
  if not image:
35
  return "Error: Please upload a valid image (PNG/JPEG)."
 
49
  payload = {
50
  "image": image_base64,
51
  "prompt": prompt,
52
+ "strength": 0.7, # Controls transformation intensity (0.0 to 1.0)
53
+ "output_format": "png"
54
  }
55
 
56
  try:
 
69
  status_response.raise_for_status()
70
  status_data = status_response.json()
71
  if status_data.get("status") == "succeeded":
72
+ image_url = status_data.get("image_url") or status_data.get("result", {}).get("image_url")
73
+ if not image_url:
74
+ return "Error: No image URL in API response."
75
+ # Download the image
76
+ image_response = requests.get(image_url, timeout=30)
77
+ image_response.raise_for_status()
78
+ output_path = Path("output_image.png")
79
  with open(output_path, "wb") as f:
80
+ f.write(image_response.content)
81
  return str(output_path)
82
  elif status_data.get("status") == "failed":
83
+ return "Error: Image generation failed."
84
  time.sleep(5)
85
 
86
+ return "Error: Image generation timed out."
87
 
88
  except requests.exceptions.HTTPError as e:
89
  return f"Error: API request failed. Details: {str(e)}"
 
92
 
93
  def chatbot_interface(image, prompt):
94
  """
95
+ Gradio interface for image-to-image generation.
96
 
97
  Args:
98
  image: Uploaded image file
99
  prompt (str): Optional text prompt
100
 
101
  Returns:
102
+ Image file path or error message
103
  """
104
+ return generate_image(image, prompt)
105
 
106
  # Define Gradio interface
107
  iface = gr.Interface(
108
  fn=chatbot_interface,
109
  inputs=[
110
  gr.Image(type="filepath", label="Upload Image (PNG/JPEG)"),
111
+ gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'Turn this into a painting')", label="Prompt")
112
  ],
113
+ outputs=gr.Image(label="Generated Image"),
114
+ title="Kling AI Image-to-Image Generator",
115
+ description="Upload a PNG/JPEG image to generate a transformed image using Kling AI API."
116
  )
117
 
118
  # Launch the interface