AkashKumarave commited on
Commit
3a257f2
Β·
verified Β·
1 Parent(s): 8fe78cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -189
app.py CHANGED
@@ -1,198 +1,120 @@
1
- import os
2
- import io
3
- import time
4
- import base64
5
- import requests
6
- from PIL import Image
7
  import gradio as gr
 
 
 
 
 
8
 
9
- # -------------------------------------------------------------------
10
- # CONFIG (via Secrets in your HF Space)
11
- # -------------------------------------------------------------------
12
- # Required: your Kling secret key. In HF: Settings β†’ Secrets β†’ add KLING_SECRET_KEY
13
- KLING_SECRET_KEY = os.getenv("KLING_SECRET_KEY", "").strip()
14
-
15
- # One of these should be set:
16
- # 1) If you have a proxy (e.g., your Supabase Edge Function):
17
- # e.g. https://rzbyjtariqsnhgwfxdxa.supabase.co/functions/v1/klingai-proxy
18
- KLING_PROXY_URL = os.getenv("KLING_PROXY_URL", "").strip()
19
-
20
- # 2) Or, if you want to call Kling API directly, set this to the base URL they provide.
21
- # Example placeholder (replace with the *real* one if you have it):
22
- KLING_API_URL = os.getenv("KLING_API_URL", "").strip()
23
-
24
- # Optional tuning defaults
25
- DEFAULT_SIZE = "1024x1024"
26
- TIMEOUT_SEC = 120 # request timeout
27
- POLL_DELAY = 2 # for any status polling, if your proxy supports it
28
-
29
- # -------------------------------------------------------------------
30
- # Helpers
31
- # -------------------------------------------------------------------
32
- def _b64_image_from_pil(img: Image.Image) -> str:
33
- buf = io.BytesIO()
34
- img.save(buf, format="PNG")
35
- return base64.b64encode(buf.getvalue()).decode("utf-8")
36
-
37
- def _pil_from_b64(b64_str: str) -> Image.Image:
38
- raw = base64.b64decode(b64_str)
39
- return Image.open(io.BytesIO(raw)).convert("RGBA").convert("RGB")
40
-
41
- def _ensure_ready():
42
- if not KLING_SECRET_KEY:
43
- raise RuntimeError(
44
- "Missing KLING_SECRET_KEY. In your Hugging Face Space, go to Settings β†’ Secrets and add it."
45
- )
46
- if not (KLING_PROXY_URL or KLING_API_URL):
47
- raise RuntimeError(
48
- "Set either KLING_PROXY_URL (your proxy) OR KLING_API_URL (direct API) in Settings β†’ Secrets."
49
- )
50
 
51
- def _post_json(url: str, json_body: dict):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  headers = {
53
- "Authorization": f"Bearer {KLING_SECRET_KEY}",
54
- "Content-Type": "application/json",
55
  }
56
- resp = requests.post(url, json=json_body, headers=headers, timeout=TIMEOUT_SEC)
57
- # Try to provide helpful error info
58
- if resp.status_code // 100 != 2:
59
- try:
60
- msg = resp.json()
61
- except Exception:
62
- msg = resp.text
63
- raise RuntimeError(f"HTTP {resp.status_code}: {msg}")
64
- return resp.json()
65
-
66
- # -------------------------------------------------------------------
67
- # Unified call: works with your proxy or direct API
68
- # Expected proxy/ API contract:
69
- # Request JSON:
70
- # {
71
- # "mode": "txt2img" | "img2img",
72
- # "prompt": "...",
73
- # "size": "1024x1024",
74
- # "seed": 0,
75
- # "strength": 0.8, # only for img2img
76
- # "image_base64": "<PNG b64>" # only for img2img
77
- # }
78
- # Response JSON (synchronous):
79
- # { "image_base64": "<PNG b64>" }
80
- # OR (async job pattern):
81
- # { "job_id": "..." } then a GET to /result?job_id=... returns same {image_base64: ...}
82
- # Adjust if your proxy differs.
83
- # -------------------------------------------------------------------
84
- def kling_generate(prompt: str,
85
- size: str,
86
- seed: int,
87
- strength: float,
88
- init_image: Image.Image | None):
89
- _ensure_ready()
90
- mode = "img2img" if init_image is not None else "txt2img"
91
-
92
  payload = {
93
- "mode": mode,
94
- "prompt": (prompt or "").strip(),
95
- "size": size or DEFAULT_SIZE,
96
- "seed": int(seed) if seed is not None else 0,
 
 
97
  }
98
-
99
- if mode == "img2img":
100
- payload["strength"] = float(strength)
101
- payload["image_base64"] = _b64_image_from_pil(init_image)
102
-
103
- # Decide endpoint
104
- base = KLING_PROXY_URL or KLING_API_URL
105
- # Two possible styles:
106
- # - single endpoint that handles both modes
107
- # - separate endpoints per mode
108
- # Here we use a single generic /generate endpoint for simplicity:
109
- gen_url = base.rstrip("/") + "/generate"
110
-
111
  try:
112
- data = _post_json(gen_url, payload)
113
- except Exception as e:
114
- # If your proxy uses job polling, try a second path:
115
- # On non-2xx, we just bubble up error. You can customize below.
116
- raise
117
-
118
- # Handle both sync and async contract
119
- if "image_base64" in data:
120
- return _pil_from_b64(data["image_base64"])
121
-
122
- # Async job fallback
123
- job_id = data.get("job_id")
124
- if not job_id:
125
- raise RuntimeError("Unexpected response. Neither 'image_base64' nor 'job_id' found.")
126
-
127
- # Polling
128
- result_url = base.rstrip("/") + "/result"
129
- started = time.time()
130
- while True:
131
- if time.time() - started > TIMEOUT_SEC:
132
- raise RuntimeError("Generation timeout. Try smaller size or check your proxy/API.")
133
- try:
134
- r = requests.get(result_url, params={"job_id": job_id}, timeout=TIMEOUT_SEC)
135
- if r.status_code // 100 != 2:
136
- time.sleep(POLL_DELAY)
137
- continue
138
- jd = r.json()
139
- if "status" in jd and jd["status"] in ("queued", "running"):
140
- time.sleep(POLL_DELAY)
141
- continue
142
- if "image_base64" in jd:
143
- return _pil_from_b64(jd["image_base64"])
144
- # If API returns error shape
145
- if "error" in jd:
146
- raise RuntimeError(str(jd["error"]))
147
- except Exception:
148
- time.sleep(POLL_DELAY)
149
-
150
- # -------------------------------------------------------------------
151
- # Gradio UI
152
- # -------------------------------------------------------------------
153
- with gr.Blocks(title="Kling AI β€” Image Generator") as demo:
154
- gr.Markdown(
155
- """
156
- # Kling AI β€” Image & Image-to-Image
157
- - Add your **KLING_SECRET_KEY** in Space **Settings β†’ Secrets**.
158
- - Set either **KLING_PROXY_URL** (recommended) or **KLING_API_URL**.
159
- """
160
- )
161
-
162
- with gr.Row():
163
- with gr.Column():
164
- prompt = gr.Textbox(
165
- label="Prompt",
166
- placeholder="Describe the image you want...",
167
- lines=4
168
- )
169
- init_image = gr.Image(label="Init Image (optional for img2img)", type="pil")
170
- strength = gr.Slider(0.0, 1.0, value=0.8, step=0.05, label="Strength (img2img only)")
171
- size = gr.Dropdown(
172
- ["512x512", "768x768", "1024x1024", "1024x1536", "1536x1024"],
173
- value=DEFAULT_SIZE, label="Size"
174
- )
175
- seed = gr.Number(value=0, precision=0, label="Seed (0 = random)")
176
-
177
- btn = gr.Button("Generate", variant="primary")
178
-
179
- with gr.Column():
180
- out = gr.Image(label="Output", type="pil")
181
-
182
- def _on_click(prompt, size, seed, strength, init_image):
183
- if not prompt and init_image is None:
184
- raise gr.Error("Please enter a prompt or provide an init image.")
185
- try:
186
- img = kling_generate(prompt, size, int(seed), float(strength), init_image)
187
- return img
188
- except Exception as e:
189
- raise gr.Error(str(e))
190
-
191
- btn.click(
192
- _on_click,
193
- inputs=[prompt, size, seed, strength, init_image],
194
- outputs=[out]
195
- )
196
-
197
  if __name__ == "__main__":
198
- 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
 
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-video"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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_video(image, prompt=""):
24
+ """
25
+ Call Kling AI API to generate a video from an input image.
26
+
27
+ Args:
28
+ image: Uploaded image file (from Gradio)
29
+ prompt (str): Optional text prompt to guide video generation
30
+
31
+ Returns:
32
+ str: Path to the downloaded video or error message
33
+ """
34
+ if not image:
35
+ return "Error: Please upload a valid image (PNG/JPEG)."
36
+
37
+ # Convert image to base64
38
+ try:
39
+ with open(image, "rb") as img_file:
40
+ image_base64 = base64.b64encode(img_file.read()).decode("utf-8")
41
+ except Exception as e:
42
+ return f"Error: Failed to process image. Details: {str(e)}"
43
+
44
  headers = {
45
+ "Authorization": f"Bearer {generate_jwt_token()}",
46
+ "Content-Type": "application/json"
47
  }
48
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  payload = {
50
+ "inputs": [{"name": "input", "inputType": "URL", "url": image_base64}],
51
+ "arguments": [
52
+ {"name": "prompt", "value": prompt},
53
+ {"name": "duration", "value": "5"},
54
+ {"name": "aspect_ratio", "value": "16:9"}
55
+ ]
56
  }
57
+
 
 
 
 
 
 
 
 
 
 
 
 
58
  try:
59
+ response = requests.post(API_URL, json=payload, headers=headers, timeout=30)
60
+ response.raise_for_status()
61
+
62
+ data = response.json()
63
+ task_id = data.get("taskId") or data.get("task_id")
64
+ if not task_id:
65
+ return "Error: No task ID returned by the API."
66
+
67
+ # Poll for task completion
68
+ status_url = f"https://api-singapore.klingai.com/v1/predictions/{task_id}"
69
+ for _ in range(60): # Poll for up to 5 minutes
70
+ status_response = requests.get(status_url, headers=headers, timeout=30)
71
+ status_response.raise_for_status()
72
+ status_data = status_response.json()
73
+ if status_data.get("status") == "succeed":
74
+ video_url = status_data.get("task_result", {}).get("videos", [{}])[0].get("url")
75
+ if not video_url:
76
+ return "Error: No video URL in API response."
77
+ # Download the video
78
+ video_response = requests.get(video_url, timeout=30)
79
+ video_response.raise_for_status()
80
+ output_path = Path("output_video.mp4")
81
+ with open(output_path, "wb") as f:
82
+ f.write(video_response.content)
83
+ return str(output_path)
84
+ elif status_data.get("status") == "failed":
85
+ return "Error: Video generation failed."
86
+ time.sleep(5)
87
+
88
+ return "Error: Video generation timed out."
89
+
90
+ except requests.exceptions.RequestException as e:
91
+ return f"Error: API request failed. Details: {str(e)}"
92
+
93
+ def chatbot_interface(image, prompt):
94
+ """
95
+ Gradio interface for image-to-image video generation.
96
+
97
+ Args:
98
+ image: Uploaded image file
99
+ prompt (str): Optional text prompt
100
+
101
+ Returns:
102
+ Video file path or error message
103
+ """
104
+ return generate_video(image, prompt)
105
+
106
+ # Define Gradio interface
107
+ iface = gr.Interface(
108
+ fn=chatbot_interface,
109
+ inputs=[
110
+ gr.Image(type="filepath", label="Upload Image (PNG/JPEG)"),
111
+ gr.Textbox(lines=2, placeholder="Enter an optional prompt (e.g., 'A cat running')", label="Prompt")
112
+ ],
113
+ outputs=gr.Video(label="Generated Video"),
114
+ title="Kling AI Image-to-Video Generator",
115
+ description="Upload a PNG/JPEG image to generate a 5-second video using Kling AI API."
116
+ )
117
+
118
+ # Launch the interface
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
119
  if __name__ == "__main__":
120
+ iface.launch(server_name="0.0.0.0", server_port=7860)