Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,7 @@ logger = logging.getLogger(__name__)
|
|
| 14 |
# Kling AI API configuration (keys hardcoded as requested)
|
| 15 |
ACCESS_KEY_ID = "AGBGmadNd9hakFYfahytyQQJtN8CJmDJ"
|
| 16 |
ACCESS_KEY_SECRET = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde"
|
| 17 |
-
API_URL = "https://api.klingai.com/v1/
|
| 18 |
|
| 19 |
def generate_jwt_token():
|
| 20 |
"""Generate JWT token for Kling AI API authentication."""
|
|
@@ -61,6 +61,7 @@ def generate_image(image, prompt=""):
|
|
| 61 |
}
|
| 62 |
|
| 63 |
payload = {
|
|
|
|
| 64 |
"image": image_base64,
|
| 65 |
"prompt": prompt or "Transform the face into a cartoon style while preserving identity",
|
| 66 |
"strength": 0.97, # High reference strength for face preservation
|
|
@@ -77,10 +78,10 @@ def generate_image(image, prompt=""):
|
|
| 77 |
task_id = data.get("task_id") or data.get("taskId") or data.get("id")
|
| 78 |
if not task_id:
|
| 79 |
logger.error("No task ID returned in API response")
|
| 80 |
-
return None, "Error: No task ID returned. Check API endpoint or keys at https://klingai.com/global/dev."
|
| 81 |
|
| 82 |
# Poll for task completion
|
| 83 |
-
status_url = f"
|
| 84 |
for _ in range(60): # Poll for up to 5 minutes
|
| 85 |
logger.debug(f"Polling status at {status_url}")
|
| 86 |
status_response = requests.get(status_url, headers=headers, timeout=30)
|
|
@@ -92,7 +93,7 @@ def generate_image(image, prompt=""):
|
|
| 92 |
image_url = status_data.get("image_url") or status_data.get("result", {}).get("image_url") or status_data.get("output") or status_data.get("data", {}).get("image")
|
| 93 |
if not image_url:
|
| 94 |
logger.error("No image URL in API response")
|
| 95 |
-
return None, "Error: No image URL in API response. Check API documentation at https://klingai.com/global/dev."
|
| 96 |
# Download the image
|
| 97 |
logger.debug(f"Downloading image from {image_url}")
|
| 98 |
image_response = requests.get(image_url, timeout=30)
|
|
@@ -110,7 +111,7 @@ def generate_image(image, prompt=""):
|
|
| 110 |
return None, "Error: Image generation failed. Ensure the image contains a clear face and avoid NSFW or sensitive content."
|
| 111 |
elif status == "processing" and status_data.get("progress", 0) >= 0.99:
|
| 112 |
logger.error("Generation stuck at 99%")
|
| 113 |
-
return None, "Error: Generation stuck at 99%. Check account credits or upgrade to a paid plan at https://klingai.com."
|
| 114 |
time.sleep(5)
|
| 115 |
|
| 116 |
logger.error("Image generation timed out")
|
|
@@ -124,10 +125,10 @@ def generate_image(image, prompt=""):
|
|
| 124 |
|
| 125 |
if status_code == 404:
|
| 126 |
if service_code == 1202:
|
| 127 |
-
return None, "Error: Invalid request method (1202). Check https://klingai.com/global/dev for correct endpoint."
|
| 128 |
elif service_code == 1203:
|
| 129 |
return None, "Error: Resource does not exist (1203). Verify model or endpoint."
|
| 130 |
-
return None, f"Error: Endpoint not found (404).
|
| 131 |
elif status_code == 401:
|
| 132 |
if service_code == 1000:
|
| 133 |
return None, "Error: Authentication failed (1000). Verify API keys are correct."
|
|
@@ -141,7 +142,7 @@ def generate_image(image, prompt=""):
|
|
| 141 |
return None, "Error: Authorization expired (1004). Generate a new token."
|
| 142 |
elif status_code == 429:
|
| 143 |
if service_code == 1100:
|
| 144 |
-
return None, "Error: Account exception (1100). Verify account configuration at https://klingai.com."
|
| 145 |
elif service_code == 1101:
|
| 146 |
return None, "Error: Account in arrears (1101). Recharge your account."
|
| 147 |
elif service_code == 1102:
|
|
@@ -196,8 +197,8 @@ with gr.Blocks() as iface:
|
|
| 196 |
gr.Markdown(
|
| 197 |
"""
|
| 198 |
# Kling AI Single-Reference Face Generator
|
| 199 |
-
Upload a PNG/JPEG image (<10 MB, ≥512x512 pixels) with a clear face to generate a transformed image using Kling AI API (reference strength 0.97). Avoid NSFW or sensitive content.
|
| 200 |
-
If
|
| 201 |
"""
|
| 202 |
)
|
| 203 |
with gr.Row():
|
|
|
|
| 14 |
# Kling AI API configuration (keys hardcoded as requested)
|
| 15 |
ACCESS_KEY_ID = "AGBGmadNd9hakFYfahytyQQJtN8CJmDJ"
|
| 16 |
ACCESS_KEY_SECRET = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde"
|
| 17 |
+
API_URL = "https://api-singapore.klingai.com/v1/images/image2image" # Updated endpoint
|
| 18 |
|
| 19 |
def generate_jwt_token():
|
| 20 |
"""Generate JWT token for Kling AI API authentication."""
|
|
|
|
| 61 |
}
|
| 62 |
|
| 63 |
payload = {
|
| 64 |
+
"model_name": "kling-v2-1", # V2.1 model for image-to-image
|
| 65 |
"image": image_base64,
|
| 66 |
"prompt": prompt or "Transform the face into a cartoon style while preserving identity",
|
| 67 |
"strength": 0.97, # High reference strength for face preservation
|
|
|
|
| 78 |
task_id = data.get("task_id") or data.get("taskId") or data.get("id")
|
| 79 |
if not task_id:
|
| 80 |
logger.error("No task ID returned in API response")
|
| 81 |
+
return None, "Error: No task ID returned. Check API endpoint or keys at https://app.klingai.com/global/dev."
|
| 82 |
|
| 83 |
# Poll for task completion
|
| 84 |
+
status_url = f"https://api-singapore.klingai.com/v1/tasks/{task_id}"
|
| 85 |
for _ in range(60): # Poll for up to 5 minutes
|
| 86 |
logger.debug(f"Polling status at {status_url}")
|
| 87 |
status_response = requests.get(status_url, headers=headers, timeout=30)
|
|
|
|
| 93 |
image_url = status_data.get("image_url") or status_data.get("result", {}).get("image_url") or status_data.get("output") or status_data.get("data", {}).get("image")
|
| 94 |
if not image_url:
|
| 95 |
logger.error("No image URL in API response")
|
| 96 |
+
return None, "Error: No image URL in API response. Check API documentation at https://app.klingai.com/global/dev."
|
| 97 |
# Download the image
|
| 98 |
logger.debug(f"Downloading image from {image_url}")
|
| 99 |
image_response = requests.get(image_url, timeout=30)
|
|
|
|
| 111 |
return None, "Error: Image generation failed. Ensure the image contains a clear face and avoid NSFW or sensitive content."
|
| 112 |
elif status == "processing" and status_data.get("progress", 0) >= 0.99:
|
| 113 |
logger.error("Generation stuck at 99%")
|
| 114 |
+
return None, "Error: Generation stuck at 99%. Check account credits or upgrade to a paid plan at https://app.klingai.com."
|
| 115 |
time.sleep(5)
|
| 116 |
|
| 117 |
logger.error("Image generation timed out")
|
|
|
|
| 125 |
|
| 126 |
if status_code == 404:
|
| 127 |
if service_code == 1202:
|
| 128 |
+
return None, "Error: Invalid request method (1202). Check https://app.klingai.com/global/dev for correct endpoint."
|
| 129 |
elif service_code == 1203:
|
| 130 |
return None, "Error: Resource does not exist (1203). Verify model or endpoint."
|
| 131 |
+
return None, f"Error: Endpoint not found (404). Check https://app.klingai.com/global/dev for the correct endpoint."
|
| 132 |
elif status_code == 401:
|
| 133 |
if service_code == 1000:
|
| 134 |
return None, "Error: Authentication failed (1000). Verify API keys are correct."
|
|
|
|
| 142 |
return None, "Error: Authorization expired (1004). Generate a new token."
|
| 143 |
elif status_code == 429:
|
| 144 |
if service_code == 1100:
|
| 145 |
+
return None, "Error: Account exception (1100). Verify account configuration at https://app.klingai.com."
|
| 146 |
elif service_code == 1101:
|
| 147 |
return None, "Error: Account in arrears (1101). Recharge your account."
|
| 148 |
elif service_code == 1102:
|
|
|
|
| 197 |
gr.Markdown(
|
| 198 |
"""
|
| 199 |
# Kling AI Single-Reference Face Generator
|
| 200 |
+
Upload a PNG/JPEG image (<10 MB, ≥512x512 pixels) with a clear face to generate a transformed image using Kling AI V2.1 API (reference strength 0.97). Avoid NSFW or sensitive content.
|
| 201 |
+
If errors persist, check the correct API endpoint at https://app.klingai.com/global/dev.
|
| 202 |
"""
|
| 203 |
)
|
| 204 |
with gr.Row():
|