Spaces:
Build error
Build error
File size: 8,392 Bytes
17f80f4 e5c3f61 4106fa2 17f80f4 cbe9858 17f80f4 cbe9858 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 f6afb9e 17f80f4 5133a1d f6afb9e 17f80f4 e5c3f61 17f80f4 f6afb9e 17f80f4 f6afb9e 51f4994 17f80f4 84970a1 17f80f4 84970a1 17f80f4 84970a1 17f80f4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
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()
|