AkashKumarave commited on
Commit
1a8f2c7
Β·
verified Β·
1 Parent(s): 29c1a23

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +200 -257
app.py CHANGED
@@ -1,273 +1,216 @@
1
- import os
2
- import time
3
- import base64
4
- import json
5
- from pathlib import Path
6
-
7
  import gradio as gr
8
  import requests
 
 
9
  import jwt
10
- from PIL import Image
11
-
12
- # ──────────────────────────────────────────────────────────────────────────────
13
- # CONFIG β€” set your keys as HF Space secrets or env vars for safety.
14
- # (Falls back to the keys you shared.)
15
- # ──────────────────────────────────────────────────────────────────────────────
16
- ACCESS_KEY_ID = os.getenv("KLING_ACCESS_KEY_ID", "AGBGmadNd9hakFYfahytyQQJtN8CJmDJ")
17
- ACCESS_KEY_SECRET = os.getenv("KLING_ACCESS_KEY_SECRET", "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde")
18
-
19
- API_BASE = "https://api.klingai.com"
20
- ENDPOINT_KOLORS = f"{API_BASE}/v1/images/kolors" # face/subject reference modes (image-to-image)
21
- ENDPOINT_GENERATIONS = f"{API_BASE}/v1/images/generations" # listing (used as a fallback poller)
22
- ENDPOINT_TASK = lambda tid: f"{API_BASE}/v1/tasks/{tid}" # primary poller
23
 
24
- # ──────────────────────────────────────────────────────────────────────────────
25
- # AUTH β€” Kling uses JWT: iss / exp / nbf with HS256 (no "access_key" field)
26
- # ──────────────────────────────────────────────────────────────────────────────
27
- def make_jwt() -> str:
 
 
 
 
 
 
 
 
 
 
 
28
  headers = {"alg": "HS256", "typ": "JWT"}
29
- now = int(time.time())
30
  payload = {
31
  "iss": ACCESS_KEY_ID,
32
- "exp": now + 1800, # 30 minutes
33
- "nbf": now - 5, # start now (minus small skew)
34
  }
35
- return jwt.encode(payload, ACCESS_KEY_SECRET, algorithm="HS256", headers=headers)
36
-
37
- # ──────────────────────────────────────────────────────────────────────────────
38
- # HELPERS
39
- # ──────────────────────────────────────────────────────────────────────────────
40
- def ensure_image_ok(img_path: str):
41
- with Image.open(img_path) as im:
42
- im.verify() # quick integrity check
43
 
44
- def b64_data_uri(img_path: str) -> str:
45
- mime = "image/png" if img_path.lower().endswith(".png") else "image/jpeg"
46
- with open(img_path, "rb") as f:
47
- b = base64.b64encode(f.read()).decode("utf-8")
48
- return f"data:{mime};base64,{b}"
49
-
50
- def extract_task_id(resp_json: dict) -> str | None:
51
- # Common shapes seen in the wild
52
- if not resp_json:
53
- return None
54
- for key in ("task_id", "taskId", "id"):
55
- if key in resp_json:
56
- return str(resp_json[key])
57
- data = resp_json.get("data") or {}
58
- for key in ("task_id", "taskId", "id"):
59
- if key in data:
60
- return str(data[key])
61
- # Sometimes nested deeper (e.g., {"task": {"id": ...}})
62
- task = resp_json.get("task") or data.get("task") or {}
63
- if "id" in task:
64
- return str(task["id"])
65
- return None
66
-
67
- def extract_image_urls(resp_json: dict) -> list[str]:
68
- if not resp_json:
69
- return []
70
- data = resp_json.get("data") or {}
71
- # Typical: data.task_result.images = [{url: "..."}]
72
- task_result = data.get("task_result") or {}
73
- images = task_result.get("images") or []
74
- urls = [img.get("url") for img in images if isinstance(img, dict) and img.get("url")]
75
- if urls:
76
- return urls
77
- # Some variants: output, image_url, result.image_url
78
- for k in ("output", "image_url"):
79
- if k in resp_json and isinstance(resp_json[k], str):
80
- return [resp_json[k]]
81
- result = resp_json.get("result") or {}
82
- if isinstance(result, dict) and result.get("image_url"):
83
- return [result["image_url"]]
84
- # Works array pattern
85
- works = resp_json.get("works") or data.get("works") or []
86
- urls = []
87
- for w in works:
88
- if isinstance(w, dict):
89
- u = w.get("url") or w.get("imageUrl")
90
- if u:
91
- urls.append(u)
92
- return urls
93
-
94
- def download_to_file(url: str, out_path: Path) -> Path:
95
- r = requests.get(url, timeout=60)
96
- r.raise_for_status()
97
- out_path.parent.mkdir(parents=True, exist_ok=True)
98
- with open(out_path, "wb") as f:
99
- f.write(r.content)
100
- return out_path
101
-
102
- def poll_for_result(task_id: str, headers: dict, timeout_s: int = 300, interval_s: float = 3.0) -> dict:
103
- """Poll task endpoint first; fallback to listing."""
104
- deadline = time.time() + timeout_s
105
- last_error = None
106
-
107
- while time.time() < deadline:
108
- try:
109
- # Preferred: direct task status
110
- r = requests.get(ENDPOINT_TASK(task_id), headers=headers, timeout=30)
111
- if r.status_code == 200:
112
- j = r.json()
113
- # Either "status_name":"succeed" or "data.task_status":"succeed"
114
- status_name = (j.get("status_name")
115
- or (j.get("data") or {}).get("task_status")
116
- or (j.get("task") or {}).get("status_name"))
117
- if isinstance(status_name, dict):
118
- # Some SDKs wrap status as enum-like
119
- status_name = status_name.get("value")
120
- if status_name in ("succeed", "succeeded", "success", "SUCCEED"):
121
- return j
122
- if status_name in ("failed", "FAIL", "failed_with_error"):
123
- return j
124
- elif r.status_code in (401, 403, 404):
125
- last_error = r.text
126
- # Fallback: scan generations list
127
- r2 = requests.get(ENDPOINT_GENERATIONS, headers=headers, params={"pageSize": 200}, timeout=30)
128
- if r2.status_code == 200:
129
- j2 = r2.json()
130
- for item in (j2.get("data") or []):
131
- if str(item.get("task_id")) == str(task_id):
132
- status = item.get("task_status")
133
- if status in ("succeed", "succeeded", "success"):
134
- return item
135
- if status in ("failed",):
136
- return item
137
- except requests.RequestException as e:
138
- last_error = str(e)
139
- time.sleep(interval_s)
140
- raise TimeoutError(f"Polling timed out for task_id {task_id}. Last error: {last_error or 'n/a'}")
141
-
142
- # ──────────────────────────────────────────────────────────────────────────────
143
- # CORE CALL β€” Kolors face reference (single reference, faceStrength=97)
144
- # ──────────────────────────────────────────────────────────────────────────────
145
- def kling_face_reference(image_path: str, prompt: str, face_strength: int = 97, aspect_ratio: str = "1:1") -> tuple[str, str]:
146
  """
147
- Returns (display_image_path, download_file_path)
 
 
 
 
 
 
 
148
  """
149
- if not image_path:
150
- raise gr.Error("Please upload a face/reference image.")
151
- ensure_image_ok(image_path)
152
-
153
- token = make_jwt()
154
- headers_json = {
155
- "Authorization": f"Bearer {token}",
156
- "Content-Type": "application/json",
157
- }
158
- headers_multipart = {
159
- "Authorization": f"Bearer {token}",
160
- }
161
-
162
- # First try: multipart/form-data (send file as `imageReference`)
163
- data_multipart = {
164
- "prompt": (None, prompt),
165
- "reference": (None, "face"),
166
- "faceStrength": (None, str(max(1, min(100, int(face_strength))))),
167
- "faceNo": (None, "1"), # single face reference
168
- "imageCount": (None, "1"),
169
- "aspect_ratio": (None, aspect_ratio),
170
  }
171
- files = {
172
- "imageReference": (os.path.basename(image_path), open(image_path, "rb"),
173
- "image/png" if image_path.lower().endswith(".png") else "image/jpeg")
 
 
 
 
 
 
174
  }
175
-
176
- # Attempt 1 β€” multipart
177
- try:
178
- resp = requests.post(ENDPOINT_KOLORS, headers=headers_multipart, files=files, data=data_multipart, timeout=60)
179
- if resp.status_code == 200:
180
- j = resp.json()
181
- else:
182
- # Read JSON anyway if possible
183
- try:
184
- j = resp.json()
185
- except Exception:
186
- j = {"code": resp.status_code, "message": resp.text}
187
- finally:
188
- # Close file handle if opened
189
- try:
190
- files["imageReference"][1].close()
191
- except Exception:
192
- pass
193
-
194
- task_id = extract_task_id(j)
195
-
196
- # If Kolors rejected multipart or no task_id, try JSON with data URI
197
- if not task_id:
198
- payload = {
199
- "prompt": prompt,
200
- "reference": "face",
201
- "faceStrength": max(1, min(100, int(face_strength))),
202
- "faceNo": 1,
203
- "imageCount": 1,
204
- "aspect_ratio": aspect_ratio,
205
- "imageReference": b64_data_uri(image_path),
206
- }
207
- resp2 = requests.post(ENDPOINT_KOLORS, headers=headers_json, json=payload, timeout=60)
208
- try:
209
- j = resp2.json()
210
- except Exception:
211
- j = {"code": resp2.status_code, "message": resp2.text}
212
- task_id = extract_task_id(j)
213
-
214
- if not task_id:
215
- code = j.get("code") or j.get("service_code") or "?"
216
- msg = j.get("message") or j.get("error") or f"HTTP {resp.status_code if 'resp' in locals() else '?'}"
217
- raise gr.Error(f"Create task failed. Code: {code}. Message: {msg}")
218
-
219
- # Poll
220
- result_json = poll_for_result(task_id, headers=headers_json, timeout_s=420, interval_s=3.0)
221
-
222
- # Gather image URLs
223
- urls = extract_image_urls(result_json)
224
- if not urls:
225
- # Some APIs return the latest object on /v1/images/generations with same task_id
226
  try:
227
- listing = requests.get(ENDPOINT_GENERATIONS, headers=headers_json, params={"pageSize": 200}, timeout=30).json()
228
- for item in (listing.get("data") or []):
229
- if str(item.get("task_id")) == str(task_id):
230
- urls = extract_image_urls(item)
231
- if urls:
232
- break
233
- except Exception:
234
- pass
235
-
236
- if not urls:
237
- raise gr.Error(f"Task {task_id} succeeded but no image URL found in response.")
238
-
239
- # Download first image
240
- out_dir = Path("outputs")
241
- out_dir.mkdir(parents=True, exist_ok=True)
242
- out_path = out_dir / f"kling_face_{task_id}.png"
243
- download_to_file(urls[0], out_path)
244
-
245
- # Return same path for preview and download
246
- return str(out_path), str(out_path)
247
-
248
- # ──────────────────────────────────────────────────────────────────────────────
249
- # GRADIO UI
250
- # ──────────────────────────────────────────────────────────────────────────────
251
- with gr.Blocks(title="Kling AI β€” Image to Image (Face Reference)") as demo:
252
- gr.Markdown("### Kling AI β€” Image-to-Image (Single Face Reference)\nUpload a face image and a prompt. Strength defaults to 97.")
253
- with gr.Row():
254
- in_image = gr.Image(type="filepath", label="Reference Face Image (PNG/JPG)")
255
- in_prompt = gr.Textbox(label="Prompt", placeholder="e.g., Ultra-detailed portrait, soft light, studio background", lines=2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  with gr.Row():
257
- in_strength = gr.Slider(1, 100, value=97, step=1, label="Face Reference Strength")
258
- in_aspect = gr.Dropdown(choices=["1:1", "3:4", "4:3", "2:3", "3:2", "16:9", "9:16", "21:9"], value="1:1", label="Aspect Ratio")
259
- btn = gr.Button("Generate", variant="primary")
260
-
261
- out_img = gr.Image(label="Generated Image", show_download_button=False)
262
- out_file = gr.File(label="Download Image")
263
-
264
- def run(image, prompt, strength, aspect):
265
- if not prompt or not prompt.strip():
266
- raise gr.Error("Please enter a prompt.")
267
- return kling_face_reference(image, prompt.strip(), int(strength), aspect)
268
-
269
- btn.click(fn=run, inputs=[in_image, in_prompt, in_strength, in_aspect], outputs=[out_img, out_file])
270
-
 
 
271
  if __name__ == "__main__":
272
- # On HF Spaces, just `python app.py` is enough β€” no need to set host/port.
273
- demo.launch()
 
 
 
 
 
 
 
1
  import gradio as gr
2
  import requests
3
+ import base64
4
+ from pathlib import Path
5
  import jwt
6
+ import time
7
+ import logging
8
+ import os
 
 
 
 
 
 
 
 
 
 
9
 
10
+ # Set up logging for debugging
11
+ logging.basicConfig(level=logging.DEBUG)
12
+ logger = logging.getLogger(__name__)
13
+
14
+ # Kling AI API configuration
15
+ ACCESS_KEY_ID = "AGBGmadNd9hakFYfahytyQQJtN8CJmDJ"
16
+ ACCESS_KEY_SECRET = "dp3pAe4PpdmnAHCAPgEd3PyLmBQrkMde"
17
+ API_URLS = [
18
+ "https://api.klingai.com/v1/images/kolors", # Primary endpoint for Kolors 2.1
19
+ "https://api.klingai.com/v1/images/image2image", # Your original endpoint
20
+ "https://api.klingai.com/v1/images/generations" # Fallback from
21
+ ]
22
+
23
+ def generate_jwt_token():
24
+ """Generate JWT token for Kling AI API authentication."""
25
  headers = {"alg": "HS256", "typ": "JWT"}
 
26
  payload = {
27
  "iss": ACCESS_KEY_ID,
28
+ "exp": int(time.time()) + 1800,
29
+ "nbf": int(time.time()) - 5
30
  }
31
+ token = jwt.encode(payload, ACCESS_KEY_SECRET, headers=headers)
32
+ logger.debug(f"Generated JWT token: {token}")
33
+ return token
 
 
 
 
 
34
 
35
+ def generate_image(image, prompt=""):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  """
37
+ Call Kling AI API for single-reference face generation.
38
+
39
+ Args:
40
+ image: Uploaded image file (face image)
41
+ prompt (str): Optional text prompt
42
+
43
+ Returns:
44
+ tuple: (Path to generated image or None, error message or None)
45
  """
46
+ if not image:
47
+ logger.error("No image uploaded")
48
+ return None, "Error: Please upload a valid face image (PNG/JPEG, <10 MB, β‰₯512x512 pixels)."
49
+
50
+ try:
51
+ with open(image, "rb") as img_file:
52
+ image_base64 = base64.b64encode(img_file.read()).decode("utf-8")
53
+ logger.debug("Image converted to base64 successfully")
54
+ except Exception as e:
55
+ logger.error(f"Failed to process image: {str(e)}")
56
+ return None, f"Error: Failed to process image. Ensure it’s a valid PNG/JPEG. Details: {str(e)}"
57
+
58
+ headers = {
59
+ "Authorization": f"Bearer {generate_jwt_token()}",
60
+ "Content-Type": "application/json"
 
 
 
 
 
 
61
  }
62
+
63
+ payload = {
64
+ "model_name": "kolors-v2.1", # Kolors 2.1 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,
68
+ "output_format": "png",
69
+ "n": 1,
70
+ "aspect_ratio": "1:1"
71
  }
72
+
73
+ for api_url in API_URLS:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  try:
75
+ logger.debug(f"Sending POST request to {api_url} with payload: {payload}")
76
+ response = requests.post(api_url, json=payload, headers=headers, timeout=30)
77
+ response.raise_for_status()
78
+ data = response.json()
79
+ logger.debug(f"API response: {data}")
80
+
81
+ task_id = data.get("task_id") or data.get("taskId") or data.get("id") or data.get("data", {}).get("task_id")
82
+ if not task_id:
83
+ logger.error("No task ID returned in API response")
84
+ return None, "Error: No task ID returned. Check API endpoint at https://klingai.com/global/dev/model/image."
85
+
86
+ status_urls = [
87
+ f"https://api.klingai.com/v1/tasks/{task_id}",
88
+ f"https://api.klingai.com/api/v1/task/{task_id}"
89
+ ]
90
+ for status_url in status_urls:
91
+ for _ in range(60):
92
+ logger.debug(f"Polling status at {status_url}")
93
+ status_response = requests.get(status_url, headers=headers, timeout=30)
94
+ if status_response.status_code == 404 and status_url != status_urls[-1]:
95
+ logger.debug(f"Status endpoint {status_url} failed, trying next")
96
+ continue
97
+ status_response.raise_for_status()
98
+ status_data = status_response.json()
99
+ logger.debug(f"Status response: {status_data}")
100
+ status = status_data.get("task_status") or status_data.get("status")
101
+ if status in ("succeeded", "completed"):
102
+ image_url = (status_data.get("image_url") or
103
+ status_data.get("result", {}).get("image_url") or
104
+ status_data.get("output") or
105
+ status_data.get("data", {}).get("image") or
106
+ status_data.get("data", {}).get("images", [{}])[0].get("url"))
107
+ if not image_url:
108
+ logger.error("No image URL in API response")
109
+ return None, "Error: No image URL in API response. Check https://klingai.com/global/dev/model/image."
110
+ logger.debug(f"Downloading image from {image_url}")
111
+ image_response = requests.get(image_url, timeout=30)
112
+ image_response.raise_for_status()
113
+ output_path = Path("/tmp/output_image.png")
114
+ with open(output_path, "wb") as f:
115
+ f.write(image_response.content)
116
+ if not output_path.exists():
117
+ logger.error("Output image file not created")
118
+ return None, "Error: Failed to save output image."
119
+ logger.debug(f"Image saved to {output_path}")
120
+ return str(output_path), None
121
+ elif status == "failed":
122
+ logger.error("Image generation failed")
123
+ return None, "Error: Image generation failed. Ensure the image contains a clear face and avoid NSFW content."
124
+ elif status == "processing" and status_data.get("progress", 0) >= 0.99:
125
+ logger.error("Generation stuck at 99%")
126
+ return None, "Error: Generation stuck at 99%. Check account credits at https://app.klingai.com."
127
+ time.sleep(5)
128
+
129
+ logger.error("Image generation timed out")
130
+ return None, "Error: Image generation timed out. Try at 2 AM IST or check account status."
131
+
132
+ return None, f"Error: All status endpoints failed. Check https://klingai.com/global/dev/model/image."
133
+
134
+ except requests.exceptions.HTTPError as e:
135
+ status_code = e.response.status_code if e.response else None
136
+ error_data = e.response.json() if e.response and e.response.text else {}
137
+ service_code = error_data.get("code", 0)
138
+ error_message = error_data.get("message", str(e))
139
+ logger.error(f"HTTP error: {status_code}, Service Code: {service_code}, Details: {error_message}")
140
+
141
+ if error_message.startswith("Create task failed"):
142
+ logger.error("Create task failed error detected")
143
+ return None, "Error: Create task failed. Try at 2 AM IST, verify account at https://app.klingai.com, or join Kling AI Discord."
144
+ elif status_code == 404 and api_url != API_URLS[-1]:
145
+ logger.debug(f"Endpoint {api_url} failed with 404, trying next")
146
+ continue
147
+ elif status_code == 404:
148
+ return None, f"Error: Endpoint not found (404). Tried {', '.join(API_URLS)}. Verify endpoint at https://klingai.com/global/dev/model/image."
149
+ elif status_code == 401:
150
+ if service_code in (1000, 1001, 1002, 1003, 1004):
151
+ return None, f"Error: Authentication failed ({service_code}). Verify API keys or token settings."
152
+ elif status_code == 429:
153
+ if service_code in (1100, 1101, 1102, 1302, 1303, 1304):
154
+ return None, f"Error: Rate limit or account issue ({service_code}). Try at 2 AM IST or check https://app.klingai.com."
155
+ elif status_code == 403 and service_code == 1103:
156
+ return None, "Error: Unauthorized access (1103). Verify account permissions at https://app.klingai.com."
157
+ elif status_code == 400:
158
+ if service_code in (1200, 1201, 1300, 1301):
159
+ return None, f"Error: Invalid parameters ({service_code}). Use PNG/JPEG (<10 MB, β‰₯512x512) with a clear face."
160
+ elif status_code in (500, 503, 504):
161
+ if service_code in (5000, 5001, 5002):
162
+ return None, f"Error: Server issue ({service_code}). Try at 2 AM IST or contact support."
163
+ return None, f"Error: API request failed. HTTP {status_code}, Service Code {service_code}. Details: {error_message}"
164
+ except requests.exceptions.RequestException as e:
165
+ logger.error(f"Network error: {str(e)}")
166
+ return None, f"Error: Network issue. Ensure stable internet, disable VPN. Details: {str(e)}"
167
+
168
+ return None, f"Error: All API endpoints failed. Verify endpoint at https://klingai.com/global/dev/model/image."
169
+
170
+ def chatbot_interface(image, prompt):
171
+ """
172
+ Gradio interface for single-reference face generation.
173
+
174
+ Args:
175
+ image: Uploaded image file (containing a face)
176
+ prompt (str): Optional text prompt
177
+
178
+ Returns:
179
+ tuple: (Output image path for display, output image path for download, error message)
180
+ """
181
+ output_path, error = generate_image(image, prompt)
182
+ if error:
183
+ logger.debug(f"Error returned: {error}")
184
+ return None, None, error
185
+ logger.debug(f"Returning image path for display and download: {output_path}")
186
+ return output_path, output_path, None
187
+
188
+ # Define Gradio interface
189
+ with gr.Blocks() as iface:
190
+ gr.Markdown(
191
+ """
192
+ # Kling AI Single-Reference Face Generator
193
+ Upload a PNG/JPEG image (<10 MB, β‰₯512x512 pixels) with a clear face to generate a transformed image using Kling AI Kolors 2.1 (strength 0.97). Avoid NSFW content.
194
+ If errors persist, verify endpoint at https://klingai.com/global/dev/model/image or contact support at https://app.klingai.com or Kling AI Discord.
195
+ """
196
+ )
197
  with gr.Row():
198
+ with gr.Column():
199
+ image_input = gr.Image(type="filepath", label="Upload Face Image (PNG/JPEG, <10 MB, β‰₯512x512)")
200
+ prompt_input = gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'Turn this face into a cartoon')", label="Prompt")
201
+ generate_button = gr.Button("Generate")
202
+ with gr.Column():
203
+ output_image = gr.Image(label="Generated Face Image")
204
+ output_file = gr.File(label="Download Generated Image")
205
+ error_message = gr.Textbox(label="Status/Error Message", interactive=False)
206
+
207
+ generate_button.click(
208
+ fn=chatbot_interface,
209
+ inputs=[image_input, prompt_input],
210
+ outputs=[output_image, output_file, error_message]
211
+ )
212
+
213
+ # Launch the interface
214
  if __name__ == "__main__":
215
+ logger.debug("Launching Gradio interface")
216
+ iface.launch(server_name="0.0.0.0", server_port=7860)