tonyassi commited on
Commit
48d59d1
·
verified ·
1 Parent(s): 2b0db3b

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +237 -0
app.py ADDED
@@ -0,0 +1,237 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import traceback
5
+
6
+ APP_TITLE = "Face Swap Video API Demo"
7
+ API_URL = "https://www.face-swap.co/api"
8
+
9
+ # ----------------------------
10
+ # Helpers
11
+ # ----------------------------
12
+
13
+ def pretty_json(obj) -> str:
14
+ try:
15
+ return json.dumps(obj, indent=2, ensure_ascii=False)
16
+ except Exception:
17
+ return str(obj)
18
+
19
+
20
+ def do_post(url: str, payload: dict, timeout: int = 60) -> str:
21
+ try:
22
+ r = requests.post(url, json=payload, timeout=timeout)
23
+ r.raise_for_status()
24
+ try:
25
+ return pretty_json(r.json())
26
+ except Exception:
27
+ return r.text
28
+ except Exception:
29
+ return f"Traceback: {traceback.format_exc()}"
30
+
31
+
32
+ def do_get(url: str, timeout: int = 30) -> str:
33
+ try:
34
+ r = requests.get(url, timeout=timeout)
35
+ r.raise_for_status()
36
+ try:
37
+ return pretty_json(r.json())
38
+ except Exception:
39
+ return r.text
40
+ except Exception:
41
+ return f"Traceback: {traceback.format_exc()}"
42
+
43
+ # ----------------------------
44
+ # Code template builders (live preview)
45
+ # ----------------------------
46
+
47
+ def _escape_quotes(s: str) -> str:
48
+ return (s or "").replace('"', '\"')
49
+
50
+
51
+ def build_generate_code(api_key: str, img_url: str, vid_url: str, duration: int, gender: str) -> str:
52
+ api_key = _escape_quotes(api_key or 'YOUR_API_KEY')
53
+ img_url = _escape_quotes(img_url or 'https://tinyurl.com/elonmusk-faceswap')
54
+ vid_url = _escape_quotes(vid_url or 'https://tinyurl.com/ironman-faceswap')
55
+ gender = gender or 'all'
56
+ duration = int(duration) if duration else 4
57
+
58
+ return f"""import requests
59
+
60
+ api_key = "{api_key}"
61
+ input_image_url = "{img_url}"
62
+ input_video_url = "{vid_url}"
63
+
64
+ url = "https://www.face-swap.co/api/generate"
65
+ payload = {{
66
+ "key": api_key,
67
+ "input_image_url": input_image_url,
68
+ "input_video_url": input_video_url,
69
+ "duration": {duration}, # 4|60|120|180
70
+ "gender": "{gender}", # all|female|male
71
+ }}
72
+
73
+ r = requests.post(url, json=payload, timeout=60)
74
+ r.raise_for_status()
75
+ print(r.json())
76
+ """
77
+
78
+
79
+ def build_status_code(job_id: str) -> str:
80
+ job_id = _escape_quotes(job_id or 'YOUR_JOB_ID')
81
+ return f"""import requests
82
+
83
+ job_id = "{job_id}"
84
+ r = requests.get(f"https://www.face-swap.co/api/status/{{job_id}}", timeout=30)
85
+ r.raise_for_status()
86
+ print(r.json())
87
+ """
88
+
89
+
90
+ def build_credits_code(api_key: str) -> str:
91
+ api_key = _escape_quotes(api_key or 'YOUR_API_KEY')
92
+ return f"""import requests
93
+
94
+ api_key = "{api_key}"
95
+ r = requests.get(f"https://www.face-swap.co/api/credits/{{api_key}}", timeout=30)
96
+ r.raise_for_status()
97
+ print(r.json())
98
+ """
99
+
100
+
101
+ def build_jobs_code(api_key: str) -> str:
102
+ api_key = _escape_quotes(api_key or 'YOUR_API_KEY')
103
+ return f"""import requests
104
+
105
+ api_key = "{api_key}"
106
+ r = requests.get(f"https://www.face-swap.co/api/jobs/{{api_key}}", timeout=30)
107
+ r.raise_for_status()
108
+ print(r.json())
109
+ """
110
+
111
+ # ----------------------------
112
+ # Inference handlers (Execute buttons)
113
+ # ----------------------------
114
+
115
+ def run_generate(api_key: str, img_url: str, vid_url: str, duration: int, gender: str) -> str:
116
+ payload = {
117
+ "key": api_key,
118
+ "input_image_url": img_url,
119
+ "input_video_url": vid_url,
120
+ "duration": int(duration),
121
+ "gender": gender,
122
+ }
123
+ return do_post(f"{API_URL}/generate", payload, timeout=60)
124
+
125
+
126
+ def run_status(job_id: str) -> str:
127
+ return do_get(f"{API_URL}/status/{job_id}", timeout=30)
128
+
129
+
130
+ def run_credits(api_key: str) -> str:
131
+ return do_get(f"{API_URL}/credits/{api_key}", timeout=30)
132
+
133
+
134
+ def run_jobs(api_key: str) -> str:
135
+ return do_get(f"{API_URL}/jobs/{api_key}", timeout=30)
136
+
137
+
138
+ # ----------------------------
139
+ # UI
140
+ # ----------------------------
141
+
142
+ def build_ui():
143
+ default_image = "https://tinyurl.com/elonmusk-faceswap"
144
+ default_video = "https://tinyurl.com/ironman-faceswap"
145
+
146
+ with gr.Blocks(title=APP_TITLE, css=".gradio-container {max-width: 1100px !important}") as demo:
147
+ gr.Markdown(
148
+ f"""
149
+ # {APP_TITLE}
150
+
151
+ ### **Free API key:** [face-swap.co/api](https://www.face-swap.co/api/?utm_source=hfspace_faceswapvideoapi&utm_medium=hero_cta)
152
+ """
153
+ )
154
+
155
+ # with gr.Row():
156
+ # gr.Button("Open API Portal", variant="primary", link=API_URL)
157
+
158
+ gr.Markdown("---")
159
+
160
+ with gr.Tabs():
161
+ # 1) Generate Video
162
+ with gr.TabItem("1) Generate Video"):
163
+ with gr.Row():
164
+ api_key_in = gr.Textbox(label="API Key", placeholder="YOUR_API_KEY", type="password")
165
+ duration_in = gr.Dropdown([4, 60, 120, 180], value=4, label="Duration (seconds)")
166
+ gender_in = gr.Dropdown(["all", "female", "male"], value="all", label="Gender filter")
167
+ img_in = gr.Textbox(label="Input Image URL", value=default_image)
168
+ vid_in = gr.Textbox(label="Input Video URL", value=default_video)
169
+
170
+ code_init = build_generate_code("YOUR_API_KEY", default_image, default_video, 4, "all")
171
+ gen_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=code_init)
172
+ gen_exec = gr.Button("▶️ Execute", variant="primary")
173
+ gen_out = gr.Textbox(label="Response", lines=14, show_copy_button=True)
174
+
175
+ def update_gen_code(k, im, vi, du, ge):
176
+ return build_generate_code(k, im, vi, du, ge)
177
+ for inp in [api_key_in, img_in, vid_in, duration_in, gender_in]:
178
+ inp.change(update_gen_code, inputs=[api_key_in, img_in, vid_in, duration_in, gender_in], outputs=gen_code)
179
+
180
+ gen_exec.click(run_generate, inputs=[api_key_in, img_in, vid_in, duration_in, gender_in], outputs=gen_out)
181
+
182
+ # 2) Check Status
183
+ with gr.TabItem("2) Check Status"):
184
+ job_in = gr.Textbox(label="Job ID", placeholder="YOUR_JOB_ID")
185
+ status_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=build_status_code("YOUR_JOB_ID"))
186
+ status_exec = gr.Button("▶️ Execute", variant="primary")
187
+ status_out = gr.Textbox(label="Response", lines=12, show_copy_button=True)
188
+
189
+ def update_status_code(j):
190
+ return build_status_code(j)
191
+ job_in.change(update_status_code, inputs=job_in, outputs=status_code)
192
+
193
+ status_exec.click(run_status, inputs=job_in, outputs=status_out)
194
+
195
+ # 3) Get Credits
196
+ with gr.TabItem("3) Get Credits"):
197
+ credits_key_in = gr.Textbox(label="API Key", placeholder="YOUR_API_KEY", type="password")
198
+ credits_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=build_credits_code("YOUR_API_KEY"))
199
+ credits_exec = gr.Button("▶️ Execute", variant="secondary")
200
+ credits_out = gr.Textbox(label="Response", lines=10, show_copy_button=True)
201
+
202
+ def update_credits_code(k):
203
+ return build_credits_code(k)
204
+ credits_key_in.change(update_credits_code, inputs=credits_key_in, outputs=credits_code)
205
+
206
+ credits_exec.click(run_credits, inputs=credits_key_in, outputs=credits_out)
207
+
208
+ # 4) Get Jobs
209
+ with gr.TabItem("4) Get Jobs"):
210
+ jobs_key_in = gr.Textbox(label="API Key", placeholder="YOUR_API_KEY", type="password")
211
+ jobs_code = gr.Code(label="Python (auto‑generated)", language="python", interactive=False, value=build_jobs_code("YOUR_API_KEY"))
212
+ jobs_exec = gr.Button("▶️ Execute", variant="secondary")
213
+ jobs_out = gr.Textbox(label="Response", lines=12, show_copy_button=True)
214
+
215
+ def update_jobs_code(k):
216
+ return build_jobs_code(k)
217
+ jobs_key_in.change(update_jobs_code, inputs=jobs_key_in, outputs=jobs_code)
218
+
219
+ jobs_exec.click(run_jobs, inputs=jobs_key_in, outputs=jobs_out)
220
+
221
+ gr.Markdown(
222
+ """
223
+ ---
224
+ **Notes**
225
+ - Trial runs: set `duration` to `4` seconds.
226
+ - Gender option: `all | female | male` (default `all`).
227
+ - Buttons execute real API calls from the Space; the code blocks are **read‑only previews** that update live and are easy to copy.
228
+ """
229
+ )
230
+
231
+ return demo
232
+
233
+
234
+ demo = build_ui()
235
+
236
+ if __name__ == "__main__":
237
+ demo.launch()