Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,17 +26,17 @@ def generate_jwt_token():
|
|
| 26 |
|
| 27 |
def generate_image(image, prompt=""):
|
| 28 |
"""
|
| 29 |
-
Call Kling AI API
|
| 30 |
|
| 31 |
Args:
|
| 32 |
-
image: Uploaded image file (from Gradio)
|
| 33 |
-
prompt (str): Optional text prompt to guide
|
| 34 |
|
| 35 |
Returns:
|
| 36 |
str: Path to the generated image or error message
|
| 37 |
"""
|
| 38 |
if not image:
|
| 39 |
-
return "Error: Please upload a valid image (PNG/JPEG, <10 MB, ≥512x512 pixels)."
|
| 40 |
|
| 41 |
# Convert image to base64
|
| 42 |
try:
|
|
@@ -52,8 +52,8 @@ def generate_image(image, prompt=""):
|
|
| 52 |
|
| 53 |
payload = {
|
| 54 |
"image": image_base64,
|
| 55 |
-
"prompt": prompt or "Transform the
|
| 56 |
-
"strength": 0.
|
| 57 |
"output_format": "png"
|
| 58 |
}
|
| 59 |
|
|
@@ -85,7 +85,7 @@ def generate_image(image, prompt=""):
|
|
| 85 |
f.write(image_response.content)
|
| 86 |
return str(output_path)
|
| 87 |
elif status == "failed":
|
| 88 |
-
return "Error: Image generation failed.
|
| 89 |
elif status == "processing" and status_data.get("progress", 0) >= 0.99:
|
| 90 |
return "Error: Generation stuck at 99%. Check account credits or upgrade to a paid plan at https://klingai.com."
|
| 91 |
time.sleep(5)
|
|
@@ -125,11 +125,11 @@ def generate_image(image, prompt=""):
|
|
| 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)
|
| 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).
|
| 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."
|
|
@@ -148,10 +148,10 @@ def generate_image(image, prompt=""):
|
|
| 148 |
|
| 149 |
def chatbot_interface(image, prompt):
|
| 150 |
"""
|
| 151 |
-
Gradio interface for
|
| 152 |
|
| 153 |
Args:
|
| 154 |
-
image: Uploaded image file
|
| 155 |
prompt (str): Optional text prompt
|
| 156 |
|
| 157 |
Returns:
|
|
@@ -163,12 +163,12 @@ def chatbot_interface(image, prompt):
|
|
| 163 |
iface = gr.Interface(
|
| 164 |
fn=chatbot_interface,
|
| 165 |
inputs=[
|
| 166 |
-
gr.Image(type="filepath", label="Upload Image (PNG/JPEG, <10 MB, ≥512x512)"),
|
| 167 |
-
gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'Turn this into a cartoon')", label="Prompt")
|
| 168 |
],
|
| 169 |
-
outputs=gr.Image(label="Generated Image"),
|
| 170 |
-
title="Kling AI
|
| 171 |
-
description="Upload a PNG/JPEG image (<10 MB, ≥512x512 pixels) to generate a transformed image using Kling AI API. Avoid NSFW
|
| 172 |
)
|
| 173 |
|
| 174 |
# Launch the interface
|
|
|
|
| 26 |
|
| 27 |
def generate_image(image, prompt=""):
|
| 28 |
"""
|
| 29 |
+
Call Kling AI API for single-reference face generation.
|
| 30 |
|
| 31 |
Args:
|
| 32 |
+
image: Uploaded image file (from Gradio, face image)
|
| 33 |
+
prompt (str): Optional text prompt to guide transformation
|
| 34 |
|
| 35 |
Returns:
|
| 36 |
str: Path to the generated image or error message
|
| 37 |
"""
|
| 38 |
if not image:
|
| 39 |
+
return "Error: Please upload a valid face image (PNG/JPEG, <10 MB, ≥512x512 pixels)."
|
| 40 |
|
| 41 |
# Convert image to base64
|
| 42 |
try:
|
|
|
|
| 52 |
|
| 53 |
payload = {
|
| 54 |
"image": image_base64,
|
| 55 |
+
"prompt": prompt or "Transform the face into a cartoon style while preserving identity",
|
| 56 |
+
"strength": 0.97, # High reference strength for face preservation
|
| 57 |
"output_format": "png"
|
| 58 |
}
|
| 59 |
|
|
|
|
| 85 |
f.write(image_response.content)
|
| 86 |
return str(output_path)
|
| 87 |
elif status == "failed":
|
| 88 |
+
return "Error: Image generation failed. Ensure the image contains a clear face and avoid NSFW or sensitive content."
|
| 89 |
elif status == "processing" and status_data.get("progress", 0) >= 0.99:
|
| 90 |
return "Error: Generation stuck at 99%. Check account credits or upgrade to a paid plan at https://klingai.com."
|
| 91 |
time.sleep(5)
|
|
|
|
| 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) with a clear face."
|
| 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). Ensure the image contains a clear face and avoid NSFW 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."
|
|
|
|
| 148 |
|
| 149 |
def chatbot_interface(image, prompt):
|
| 150 |
"""
|
| 151 |
+
Gradio interface for single-reference face generation.
|
| 152 |
|
| 153 |
Args:
|
| 154 |
+
image: Uploaded image file (containing a face)
|
| 155 |
prompt (str): Optional text prompt
|
| 156 |
|
| 157 |
Returns:
|
|
|
|
| 163 |
iface = gr.Interface(
|
| 164 |
fn=chatbot_interface,
|
| 165 |
inputs=[
|
| 166 |
+
gr.Image(type="filepath", label="Upload Face Image (PNG/JPEG, <10 MB, ≥512x512)"),
|
| 167 |
+
gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'Turn this face into a cartoon')", label="Prompt")
|
| 168 |
],
|
| 169 |
+
outputs=gr.Image(label="Generated Face Image"),
|
| 170 |
+
title="Kling AI Single-Reference Face Generator",
|
| 171 |
+
description="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."
|
| 172 |
)
|
| 173 |
|
| 174 |
# Launch the interface
|