Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import subprocess | |
| import uuid | |
| import os | |
| def swap_faces(source_file, target_file, mode): | |
| output_ext = "jpg" if mode in ["image_to_image", "selfie_to_selfie"] else "mp4" | |
| output_path = f"/tmp/output_{uuid.uuid4().hex}.{output_ext}" | |
| tmp_source = f"/tmp/source_{uuid.uuid4().hex}.jpg" | |
| tmp_target = f"/tmp/target_{uuid.uuid4().hex}" | |
| if mode == "image_to_video": | |
| tmp_target += ".mp4" | |
| else: | |
| tmp_target += ".jpg" | |
| with open(tmp_source, "wb") as f: | |
| f.write(source_file) | |
| with open(tmp_target, "wb") as f: | |
| f.write(target_file) | |
| command = [ | |
| "python3", "facefusion.py", "run", | |
| "--source-paths", tmp_source, | |
| "--target-path", tmp_target, | |
| "--output-path", output_path, | |
| "--processors", "face_swapper", | |
| "--log-level", "debug" | |
| ] | |
| result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| print("=== STDOUT ===") | |
| print(result.stdout.decode()) | |
| print("=== STDERR ===") | |
| print(result.stderr.decode()) | |
| if result.returncode != 0: | |
| raise Exception(f"Processing failed:\n{result.stderr.decode()}") | |
| # โ ืืืืงื ืื ืืงืืืฅ ื ืืฆืจ ืืืืช | |
| if not os.path.exists(output_path): | |
| raise Exception("โ ืืงืืืฅ ืื ื ืืฆืจ โ ืื ืจืื ืฉืืชืืืื ื ืืฉื ืคื ืืืืช ืืื ืืืืืืจ ืฉืืืื.") | |
| return output_path | |
| iface = gr.Interface( | |
| fn=swap_faces, | |
| inputs=[ | |
| gr.File(label="ืืืจ ืชืืื ืช ืืงืืจ (Selfie)", type="binary"), | |
| gr.File(label="ืืืจ ืืขื (ืชืืื ื ืืืืืจืื ืื ืืืืื)", type="binary"), | |
| gr.Radio( | |
| choices=[ | |
| ("ืืืืคืช ืคื ืื ืืชืืื ื ืืืืืจืื", "image_to_image"), | |
| ("ืฉื ื ืื ืฉืื ืืืืืคืื ืคื ืื", "selfie_to_selfie"), | |
| ("ืืืืคืช ืคื ืื ืขื ืืืืื", "image_to_video") | |
| ], | |
| label="ืืืจ ืืฆื ืคืขืืื", | |
| value="image_to_image" | |
| ) | |
| ], | |
| outputs=gr.File(label="ืงืืืฅ ืชืืฆืื"), | |
| title="๐ญ FaceFusion API - ืืขืืืช ืงืืฆืื", | |
| description="ืืขืื ืฉื ื ืงืืฆืื (ืชืืื ืืช ืื ืืืืื), ืืืืจ ืืช ืกืื ืืืืจื ืฉืชืจืฆื ืืืฆืข" | |
| ) | |
| iface.launch() | |