Spaces:
Runtime error
Runtime error
AkashKumaravel commited on
Update app.py
Browse files25 unmodified lines26272829303132333435363738394041429 unmodified lines525354555657585925 unmodified lines8586878889909133 unmodified lines12512612712812913013113213313413512 unmodified lines1481491501511521531541551561575 unmodified lines16316416516616716816917017117217317425 unmodified linesdef generate_image(image, prompt=""):"""Call Kling AI API to generate an image from an input image.Args:image: Uploaded image file (from Gradio)prompt (str): Optional text prompt to guide image transformationReturns:str: Path to the generated image or error message"""if not image:return "Error: Please upload a valid image (PNG/JPEG, <10 MB, ≥512x512 pixels)."# Convert image to base64try:9 unmodified linespayload = {"image": image_base64,"prompt": prompt or "Transform the image into a cartoon style","strength": 0.7, # Controls transformation intensity (0.0 to 1.0)"output_format": "png"}25 unmodified linesf.write(image_response.content)return str(output_path)elif status == "failed":return "Error: Image generation failed. Avoid NSFW, celebrities, or sensitive content (e.g., politics, violence)."elif status == "processing" and status_data.get("progress", 0) >= 0.99:return "Error: Generation stuck at 99%. Check account credits or upgrade to a paid plan at https://klingai.com."time.sleep(5)33 unmodified linesif service_code == 1200:return "Error: Invalid request parameters (1200). Check payload format."elif service_code == 1201:return "Error: Invalid parameters (1201). Use PNG/JPEG (<10 MB, ≥512x512) and a simple prompt."elif service_code == 1300:return "Error: Platform policy triggered (1300). Check input content."elif service_code == 1301:return "Error: Content security policy triggered (1301). Avoid NSFW or sensitive content."elif status_code == 404:if service_code == 1202:return "Error: Invalid request method (1202). Check https://klingai.com/global/dev for correct endpoint."12 unmodified linesdef chatbot_interface(image, prompt):"""Gradio interface for image-to-image generation.Args:image: Uploaded image fileprompt (str): Optional text promptReturns:5 unmodified linesiface = gr.Interface(fn=chatbot_interface,inputs=[gr.Image(type="filepath", label="Upload Image (PNG/JPEG, <10 MB, ≥512x512)"),gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'Turn this into a cartoon')", label="Prompt")],outputs=gr.Image(label="Generated Image"),title="Kling AI Image-to-Image Generator",description="Upload a PNG/JPEG image (<10 MB, ≥512x512 pixels) to generate a transformed image using Kling AI API. Avoid NSFW, celebrities, or sensitive content.")# Launch the interface25 unmodified lines26272829303132333435363738394041429 unmodified lines525354555657585925 unmodified lines8586878889909133 unmodified lines12512612712812913013113213313413512 unmodified lines1481491501511521531541551561575 unmodified lines16316416516616716816917017117217317425 unmodified linesdef generate_image(image, prompt=""):"""Call Kling AI API for single-reference face generation.Args:image: Uploaded image file (from Gradio, face image)prompt (str): Optional text prompt to guide transformationReturns:str: Path to the generated image or error message"""if not image:return "Error: Please upload a valid face image (PNG/JPEG, <10 MB, ≥512x512 pixels)."# Convert image to base64try:9 unmodified linespayload = {"image": image_base64,"prompt": prompt or "Transform the face into a cartoon style while preserving identity","strength": 0.97, # High reference strength for face preservation"output_format": "png"}25 unmodified linesf.write(image_response.content)return str(output_path)elif status == "failed":return "Error: Image generation failed. Ensure the image contains a clear face and avoid NSFW or sensitive content."elif status == "processing" and status_data.get("progress", 0) >= 0.99:return "Error: Generation stuck at 99%. Check account credits or upgrade to a paid plan at https://klingai.com."time.sleep(5)33 unmodified linesif service_code == 1200:return "Error: Invalid request parameters (1200). Check payload format."elif service_code == 1201:return "Error: Invalid parameters (1201). Use PNG/JPEG (<10 MB, ≥512x512) with a clear face."elif service_code == 1300:return "Error: Platform policy triggered (1300). Check input content."elif service_code == 1301:return "Error: Content security policy triggered (1301). Ensure the image contains a clear face and avoid NSFW content."elif status_code == 404:if service_code == 1202:return "Error: Invalid request method (1202). Check https://klingai.com/global/dev for correct endpoint."12 unmodified linesdef chatbot_interface(image, prompt):"""Gradio interface for single-reference face generation.Args:image: Uploaded image file (containing a face)prompt (str): Optional text promptReturns:5 unmodified linesiface = gr.Interface(fn=chatbot_interface,inputs=[gr.Image(type="filepath", label="Upload Face Image (PNG/JPEG, <10 MB, ≥512x512)"),gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'Turn this face into a cartoon')", label="Prompt")],outputs=gr.Image(label="Generated Face Image"),title="Kling AI Single-Reference Face Generator",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.")# Launch the interface
app.py+16-16