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()