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/images/image2image" #
|
| 18 |
|
| 19 |
def generate_jwt_token():
|
| 20 |
"""Generate JWT token for Kling AI API authentication."""
|
|
@@ -62,13 +62,12 @@ def generate_image(image, prompt=""):
|
|
| 62 |
|
| 63 |
payload = {
|
| 64 |
"model_name": "kling-v2-1", # V2.1 model for image-to-image
|
| 65 |
-
"
|
| 66 |
-
"
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
}
|
| 72 |
}
|
| 73 |
|
| 74 |
try:
|
|
@@ -88,6 +87,11 @@ def generate_image(image, prompt=""):
|
|
| 88 |
for _ in range(60): # Poll for up to 5 minutes
|
| 89 |
logger.debug(f"Polling status at {status_url}")
|
| 90 |
status_response = requests.get(status_url, headers=headers, timeout=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
status_response.raise_for_status()
|
| 92 |
status_data = status_response.json()
|
| 93 |
logger.debug(f"Status response: {status_data}")
|
|
@@ -127,15 +131,12 @@ def generate_image(image, prompt=""):
|
|
| 127 |
error_message = error_data.get("message", str(e))
|
| 128 |
logger.error(f"HTTP error: {status_code}, Service Code: {service_code}, Details: {error_message}")
|
| 129 |
|
| 130 |
-
if
|
| 131 |
-
logger.error("Create task failed error detected")
|
| 132 |
-
return None, "Error: Create task failed. Try during off-peak hours (e.g., 2 AM IST), verify account status at https://app.klingai.com, or join Kling AI Discord for support."
|
| 133 |
-
elif status_code == 404:
|
| 134 |
if service_code == 1202:
|
| 135 |
return None, "Error: Invalid request method (1202). Check https://app.klingai.com/global/dev for correct endpoint."
|
| 136 |
elif service_code == 1203:
|
| 137 |
-
return None, "Error: Resource does not exist (1203). Verify model or endpoint."
|
| 138 |
-
return None,
|
| 139 |
elif status_code == 401:
|
| 140 |
if service_code == 1000:
|
| 141 |
return None, "Error: Authentication failed (1000). Verify API keys are correct."
|
|
@@ -205,7 +206,7 @@ with gr.Blocks() as iface:
|
|
| 205 |
"""
|
| 206 |
# Kling AI Single-Reference Face Generator
|
| 207 |
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.
|
| 208 |
-
If errors persist,
|
| 209 |
"""
|
| 210 |
)
|
| 211 |
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.klingai.com/v1/images/image2image" # Your specified endpoint
|
| 18 |
|
| 19 |
def generate_jwt_token():
|
| 20 |
"""Generate JWT token for Kling AI API authentication."""
|
|
|
|
| 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
|
| 68 |
+
"output_format": "png",
|
| 69 |
+
"access_key_id": ACCESS_KEY_ID, # Fallback for authentication
|
| 70 |
+
"access_key_secret": ACCESS_KEY_SECRET
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
try:
|
|
|
|
| 87 |
for _ in range(60): # Poll for up to 5 minutes
|
| 88 |
logger.debug(f"Polling status at {status_url}")
|
| 89 |
status_response = requests.get(status_url, headers=headers, timeout=30)
|
| 90 |
+
if status_response.status_code == 404:
|
| 91 |
+
# Try alternative status endpoint
|
| 92 |
+
status_url = f"https://api.klingai.com/api/v1/task/{task_id}"
|
| 93 |
+
logger.debug(f"Fallback: Polling status at {status_url}")
|
| 94 |
+
status_response = requests.get(status_url, headers=headers, timeout=30)
|
| 95 |
status_response.raise_for_status()
|
| 96 |
status_data = status_response.json()
|
| 97 |
logger.debug(f"Status response: {status_data}")
|
|
|
|
| 131 |
error_message = error_data.get("message", str(e))
|
| 132 |
logger.error(f"HTTP error: {status_code}, Service Code: {service_code}, Details: {error_message}")
|
| 133 |
|
| 134 |
+
if status_code == 404:
|
|
|
|
|
|
|
|
|
|
| 135 |
if service_code == 1202:
|
| 136 |
return None, "Error: Invalid request method (1202). Check https://app.klingai.com/global/dev for correct endpoint."
|
| 137 |
elif service_code == 1203:
|
| 138 |
+
return None, "Error: Resource does not exist (1203). Verify model or endpoint at https://app.klingai.com/global/dev."
|
| 139 |
+
return None, "Error: Endpoint not found (404). Verify https://api.klingai.com/v1/images/image2image at https://app.klingai.com/global/dev or try alternative endpoints like /v1/images/generations."
|
| 140 |
elif status_code == 401:
|
| 141 |
if service_code == 1000:
|
| 142 |
return None, "Error: Authentication failed (1000). Verify API keys are correct."
|
|
|
|
| 206 |
"""
|
| 207 |
# Kling AI Single-Reference Face Generator
|
| 208 |
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.
|
| 209 |
+
If errors persist, verify endpoint at https://app.klingai.com/global/dev or contact support via https://app.klingai.com.
|
| 210 |
"""
|
| 211 |
)
|
| 212 |
with gr.Row():
|