Spaces:
Build error
Build error
| import gradio as gr | |
| import requests | |
| import json | |
| import traceback | |
| APP_TITLE = "Face Swap Video API Demo" | |
| API_URL = "https://www.face-swap.co/api" | |
| # ---------------------------- | |
| # Helpers | |
| # ---------------------------- | |
| def pretty_json(obj) -> str: | |
| try: | |
| return json.dumps(obj, indent=2, ensure_ascii=False) | |
| except Exception: | |
| return str(obj) | |
| def do_post(url: str, payload: dict, timeout: int = 60) -> str: | |
| try: | |
| r = requests.post(url, json=payload, timeout=timeout) | |
| r.raise_for_status() | |
| try: | |
| return pretty_json(r.json()) | |
| except Exception: | |
| return r.text | |
| except Exception: | |
| return f"Traceback: {traceback.format_exc()}" | |
| def do_get(url: str, timeout: int = 30) -> str: | |
| try: | |
| r = requests.get(url, timeout=timeout) | |
| r.raise_for_status() | |
| try: | |
| return pretty_json(r.json()) | |
| except Exception: | |
| return r.text | |
| except Exception: | |
| return f"Traceback: {traceback.format_exc()}" | |
| # ---------------------------- | |
| # Code template builders (live preview) | |
| # ---------------------------- | |
| def _escape_quotes(s: str) -> str: | |
| return (s or "").replace('"', '\"') | |
| def build_generate_code(api_key: str, img_url: str, vid_url: str, duration: int, gender: str) -> str: | |
| api_key = _escape_quotes(api_key or 'YOUR_API_KEY') | |
| img_url = _escape_quotes(img_url or 'https://tinyurl.com/elonmusk-faceswap') | |
| vid_url = _escape_quotes(vid_url or 'https://tinyurl.com/ironman-faceswap') | |
| gender = gender or 'all' | |
| duration = int(duration) if duration else 4 | |
| return f"""import requests | |
| api_key = "{api_key}" | |
| input_image_url = "{img_url}" | |
| input_video_url = "{vid_url}" | |
| url = "https://www.face-swap.co/api/generate" | |
| payload = {{ | |
| "key": api_key, | |
| "input_image_url": input_image_url, | |
| "input_video_url": input_video_url, | |
| "duration": {duration}, # 4|60|120|180 | |
| "gender": "{gender}", # all|female|male | |
| }} | |
| r = requests.post(url, json=payload, timeout=60) | |
| r.raise_for_status() | |
| print(r.json()) | |
| """ | |
| def build_status_code(job_id: str) -> str: | |
| job_id = _escape_quotes(job_id or 'YOUR_JOB_ID') | |
| return f"""import requests | |
| job_id = "{job_id}" | |
| r = requests.get(f"https://www.face-swap.co/api/status/{{job_id}}", timeout=30) | |
| r.raise_for_status() | |
| print(r.json()) | |
| """ | |
| def build_credits_code(api_key: str) -> str: | |
| api_key = _escape_quotes(api_key or 'YOUR_API_KEY') | |
| return f"""import requests | |
| api_key = "{api_key}" | |
| r = requests.get(f"https://www.face-swap.co/api/credits/{{api_key}}", timeout=30) | |
| r.raise_for_status() | |
| print(r.json()) | |
| """ | |
| def build_jobs_code(api_key: str) -> str: | |
| api_key = _escape_quotes(api_key or 'YOUR_API_KEY') | |
| return f"""import requests | |
| api_key = "{api_key}" | |
| r = requests.get(f"https://www.face-swap.co/api/jobs/{{api_key}}", timeout=30) | |
| r.raise_for_status() | |
| print(r.json()) | |
| """ | |
| # ---------------------------- | |
| # Inference handlers (Execute buttons) | |
| # ---------------------------- | |
| def run_generate(api_key: str, img_url: str, vid_url: str, duration: int, gender: str) -> str: | |
| payload = { | |
| "key": api_key, | |
| "input_image_url": img_url, | |
| "input_video_url": vid_url, | |
| "duration": int(duration), | |
| "gender": gender, | |
| } | |
| return do_post(f"{API_URL}/generate", payload, timeout=60) | |
| def run_status(job_id: str) -> str: | |
| return do_get(f"{API_URL}/status/{job_id}", timeout=30) | |
| def run_credits(api_key: str) -> str: | |
| return do_get(f"{API_URL}/credits/{api_key}", timeout=30) | |
| def run_jobs(api_key: str) -> str: | |
| return do_get(f"{API_URL}/jobs/{api_key}", timeout=30) | |
| # ---------------------------- | |
| # UI | |
| # ---------------------------- | |
| def build_ui(): | |
| default_image = "https://tinyurl.com/elonmusk-faceswap" | |
| default_video = "https://tinyurl.com/ironman-faceswap" | |
| with gr.Blocks(title=APP_TITLE, css=".gradio-container {max-width: 1100px !important}") as demo: | |
| gr.Markdown( | |
| f""" | |
| # {APP_TITLE} | |
| ### **Free API key:** [face-swap.co/api](https://www.face-swap.co/api/?utm_source=hfspace_faceswapvideoapi&utm_medium=hero_cta) | |
| """ | |
| ) | |
| # with gr.Row(): | |
| # gr.Button("Open API Portal", variant="primary", link=API_URL) | |
| gr.Markdown("---") | |
| with gr.Tabs(): | |
| # 1) Generate Video | |
| with gr.TabItem("1) Generate Video"): | |
| with gr.Row(): | |
| api_key_in = gr.Textbox(label="API Key", placeholder="YOUR_API_KEY", type="password") | |
| duration_in = gr.Dropdown([4, 60, 120, 180], value=4, label="Duration (seconds)") | |
| gender_in = gr.Dropdown(["all", "female", "male"], value="all", label="Gender filter") | |
| img_in = gr.Textbox(label="Input Image URL", value=default_image) | |
| vid_in = gr.Textbox(label="Input Video URL", value=default_video) | |
| code_init = build_generate_code("YOUR_API_KEY", default_image, default_video, 4, "all") | |
| gen_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=code_init) | |
| gen_exec = gr.Button("▶️ Execute", variant="primary") | |
| gen_out = gr.Textbox(label="Response", lines=14, show_copy_button=True) | |
| def update_gen_code(k, im, vi, du, ge): | |
| return build_generate_code(k, im, vi, du, ge) | |
| for inp in [api_key_in, img_in, vid_in, duration_in, gender_in]: | |
| inp.change(update_gen_code, inputs=[api_key_in, img_in, vid_in, duration_in, gender_in], outputs=gen_code) | |
| gen_exec.click(run_generate, inputs=[api_key_in, img_in, vid_in, duration_in, gender_in], outputs=gen_out) | |
| # 2) Check Status | |
| with gr.TabItem("2) Check Status"): | |
| job_in = gr.Textbox(label="Job ID", placeholder="YOUR_JOB_ID") | |
| status_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=build_status_code("YOUR_JOB_ID")) | |
| status_exec = gr.Button("▶️ Execute", variant="primary") | |
| status_out = gr.Textbox(label="Response", lines=12, show_copy_button=True) | |
| def update_status_code(j): | |
| return build_status_code(j) | |
| job_in.change(update_status_code, inputs=job_in, outputs=status_code) | |
| status_exec.click(run_status, inputs=job_in, outputs=status_out) | |
| # 3) Get Credits | |
| with gr.TabItem("3) Get Credits"): | |
| credits_key_in = gr.Textbox(label="API Key", placeholder="YOUR_API_KEY", type="password") | |
| credits_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=build_credits_code("YOUR_API_KEY")) | |
| credits_exec = gr.Button("▶️ Execute", variant="secondary") | |
| credits_out = gr.Textbox(label="Response", lines=10, show_copy_button=True) | |
| def update_credits_code(k): | |
| return build_credits_code(k) | |
| credits_key_in.change(update_credits_code, inputs=credits_key_in, outputs=credits_code) | |
| credits_exec.click(run_credits, inputs=credits_key_in, outputs=credits_out) | |
| # 4) Get Jobs | |
| with gr.TabItem("4) Get Jobs"): | |
| jobs_key_in = gr.Textbox(label="API Key", placeholder="YOUR_API_KEY", type="password") | |
| jobs_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=build_jobs_code("YOUR_API_KEY")) | |
| jobs_exec = gr.Button("▶️ Execute", variant="secondary") | |
| jobs_out = gr.Textbox(label="Response", lines=12, show_copy_button=True) | |
| def update_jobs_code(k): | |
| return build_jobs_code(k) | |
| jobs_key_in.change(update_jobs_code, inputs=jobs_key_in, outputs=jobs_code) | |
| jobs_exec.click(run_jobs, inputs=jobs_key_in, outputs=jobs_out) | |
| gr.Markdown( | |
| """ | |
| --- | |
| **Notes** | |
| - Trial runs: set `duration` to `4` seconds. | |
| - Gender option: `all | female | male` (default `all`). | |
| - Buttons execute real API calls from the Space; the code blocks are **read‑only previews** that update live and are easy to copy. | |
| """ | |
| ) | |
| return demo | |
| demo = build_ui() | |
| if __name__ == "__main__": | |
| demo.launch() | |