Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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-
|
| 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
|
| 24 |
"""
|
| 25 |
-
Call Kling AI API to generate
|
| 26 |
|
| 27 |
Args:
|
| 28 |
image: Uploaded image file (from Gradio)
|
| 29 |
-
prompt (str): Optional text prompt to guide
|
| 30 |
|
| 31 |
Returns:
|
| 32 |
-
str: Path to the
|
| 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 |
-
"
|
| 53 |
-
"
|
| 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 |
-
|
| 73 |
-
if not
|
| 74 |
-
return "Error: No
|
| 75 |
-
# Download the
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
output_path = Path("
|
| 79 |
with open(output_path, "wb") as f:
|
| 80 |
-
f.write(
|
| 81 |
return str(output_path)
|
| 82 |
elif status_data.get("status") == "failed":
|
| 83 |
-
return "Error:
|
| 84 |
time.sleep(5)
|
| 85 |
|
| 86 |
-
return "Error:
|
| 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-
|
| 96 |
|
| 97 |
Args:
|
| 98 |
image: Uploaded image file
|
| 99 |
prompt (str): Optional text prompt
|
| 100 |
|
| 101 |
Returns:
|
| 102 |
-
|
| 103 |
"""
|
| 104 |
-
return
|
| 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., '
|
| 112 |
],
|
| 113 |
-
outputs=gr.
|
| 114 |
-
title="Kling AI Image-to-
|
| 115 |
-
description="Upload a PNG/JPEG image to generate a
|
| 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
|