AkashKumarave commited on
Commit
fab14c7
·
verified ·
1 Parent(s): 7421e11

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -12
app.py CHANGED
@@ -8,16 +8,20 @@ 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/generate/image"
12
 
13
  def generate_jwt_token():
14
  """Generate JWT token for Kling AI API authentication."""
 
 
 
 
15
  payload = {
16
- "iat": int(time.time()),
17
  "exp": int(time.time()) + 1800, # Token expires in 30 minutes
18
- "access_key": ACCESS_KEY_ID
19
  }
20
- token = jwt.encode(payload, ACCESS_KEY_SECRET, algorithm="HS256")
21
  return token
22
 
23
  def generate_image(image, prompt=""):
@@ -89,16 +93,56 @@ def generate_image(image, prompt=""):
89
  return "Error: Image generation timed out. Try during off-peak hours or check account status."
90
 
91
  except requests.exceptions.HTTPError as e:
92
- status_code = e.response.status_code
 
 
 
93
  if status_code == 401:
94
- return "Error: Authentication failed. Verify API keys are correct."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95
  elif status_code == 404:
96
- return "Error: API endpoint not found. Check https://klingai.com/global/dev for correct endpoint (e.g., /v1/predictions)."
97
- elif status_code == 422:
98
- return "Error: Invalid input. Use PNG/JPEG (<10 MB, ≥512x512 pixels) and a simple prompt (e.g., 'Turn this into a sketch')."
99
- elif status_code == 500:
100
- return "Error: Server issue. Try again later or during off-peak hours."
101
- return f"Error: API request failed. Details: {str(e)}"
 
 
 
 
 
 
102
  except requests.exceptions.RequestException as e:
103
  return f"Error: Network issue. Ensure stable internet, disable VPN, and try again. Details: {str(e)}"
104
 
 
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."""
15
+ headers = {
16
+ "alg": "HS256",
17
+ "typ": "JWT"
18
+ }
19
  payload = {
20
+ "iss": ACCESS_KEY_ID,
21
  "exp": int(time.time()) + 1800, # Token expires in 30 minutes
22
+ "nbf": int(time.time()) - 5 # Effective 5 seconds before current time
23
  }
24
+ token = jwt.encode(payload, ACCESS_KEY_SECRET, headers=headers)
25
  return token
26
 
27
  def generate_image(image, prompt=""):
 
93
  return "Error: Image generation timed out. Try during off-peak hours or check account status."
94
 
95
  except requests.exceptions.HTTPError as e:
96
+ status_code = e.response.status_code if e.response else None
97
+ error_data = e.response.json() if e.response and e.response.text else {}
98
+ service_code = error_data.get("code", 0)
99
+
100
  if status_code == 401:
101
+ if service_code == 1000:
102
+ return "Error: Authentication failed (1000). Verify API keys are correct."
103
+ elif service_code == 1001:
104
+ return "Error: Authorization empty (1001). Ensure JWT token is included."
105
+ elif service_code == 1002:
106
+ return "Error: Authorization invalid (1002). Check key format."
107
+ elif service_code == 1003:
108
+ return "Error: Authorization not yet valid (1003). Adjust token start time (nbf)."
109
+ elif service_code == 1004:
110
+ return "Error: Authorization expired (1004). Generate a new token."
111
+ elif status_code == 429:
112
+ if service_code == 1100:
113
+ return "Error: Account exception (1100). Verify account configuration at https://klingai.com."
114
+ elif service_code == 1101:
115
+ return "Error: Account in arrears (1101). Recharge your account."
116
+ elif service_code == 1102:
117
+ return "Error: Resource pack depleted or expired (1102). Purchase additional resources."
118
+ elif service_code in (1302, 1303):
119
+ return "Error: Rate limit exceeded (1302/1303). Reduce request frequency or contact support."
120
+ elif service_code == 1304:
121
+ return "Error: IP whitelisting issue (1304). Contact Kling AI support."
122
+ elif status_code == 403 and service_code == 1103:
123
+ return "Error: Unauthorized access (1103). Verify account permissions."
124
+ elif status_code == 400:
125
+ if service_code == 1200:
126
+ return "Error: Invalid request parameters (1200). Check payload format."
127
+ elif service_code == 1201:
128
+ return "Error: Invalid parameters (1201). Use PNG/JPEG (<10 MB, ≥512x512) and a simple prompt."
129
+ elif service_code == 1300:
130
+ return "Error: Platform policy triggered (1300). Check input content."
131
+ elif service_code == 1301:
132
+ return "Error: Content security policy triggered (1301). Avoid NSFW or sensitive content."
133
  elif status_code == 404:
134
+ if service_code == 1202:
135
+ return "Error: Invalid request method (1202). Check https://klingai.com/global/dev for correct endpoint."
136
+ elif service_code == 1203:
137
+ return "Error: Resource does not exist (1203). Verify model or endpoint."
138
+ elif status_code in (500, 503, 504):
139
+ if service_code == 5000:
140
+ return "Error: Server internal error (5000). Try again later."
141
+ elif service_code == 5001:
142
+ return "Error: Server unavailable due to maintenance (5001). Try again later."
143
+ elif service_code == 5002:
144
+ return "Error: Server timeout (5002). Try during off-peak hours."
145
+ return f"Error: API request failed. HTTP {status_code}, Service Code {service_code}. Details: {str(e)}"
146
  except requests.exceptions.RequestException as e:
147
  return f"Error: Network issue. Ensure stable internet, disable VPN, and try again. Details: {str(e)}"
148