mr2along commited on
Commit
c24c808
·
verified ·
1 Parent(s): 17f5844

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -86
app.py CHANGED
@@ -1,91 +1,32 @@
1
  import gradio as gr
2
  import subprocess
 
3
  import os
4
  from datetime import datetime
5
- from huggingface_hub import login, upload_file
6
 
7
- # --- Config ---
8
- REPO_ID = "mr2along/Picture"
9
- HF_TOKEN = os.environ.get("MAGIC")
10
- if not HF_TOKEN:
11
- print("⚠️ Lưu ý: HF_TOKEN không được tìm thấy trong biến môi trường 'MAGIC'.")
12
-
13
- # --- Hàm upload file ---
14
- def upload_to_huggingface(filepath):
15
- try:
16
- login(HF_TOKEN)
17
- filename = os.path.basename(filepath)
18
- upload_file(
19
- path_or_fileobj=filepath,
20
- path_in_repo=filename,
21
- repo_id=REPO_ID,
22
- repo_type="dataset"
23
- )
24
- return f"https://huggingface.co/datasets/{REPO_ID}/blob/main/{filename}"
25
- except Exception as e:
26
- return f"❌ Lỗi upload: {str(e)}"
27
-
28
- # --- Hàm xử chính ---
29
- def run_scripts(target_files, source_file):
30
- output_files, logs = [], ""
31
- if not target_files or not source_file:
32
- return [], "⚠️ Vui lòng chọn đủ source và target!"
33
-
34
- for tgt in target_files:
35
- tgt_path = tgt.name if hasattr(tgt, "name") else str(tgt)
36
- tgt_ext = os.path.splitext(tgt_path)[-1]
37
- ts = datetime.now().strftime("%Y%m%d%H%M%S")
38
- out_path = f"output_{ts}{tgt_ext}"
39
-
40
- cmd = [
41
- "python3", "run.py",
42
- "-s", source_file.name if hasattr(source_file, "name") else str(source_file),
43
- "-t", tgt_path,
44
- "-o", out_path,
45
- "--frame-processor", "face_swapper", "face_enhancer",
46
- "--many-faces"
47
- ]
48
-
49
- try:
50
- res = subprocess.run(cmd, capture_output=True, text=True)
51
- if res.returncode != 0:
52
- logs += f"❌ Lỗi xử lý {tgt_path}:\n{res.stderr}\n"
53
- continue
54
- output_files.append(out_path)
55
- url = upload_to_huggingface(out_path)
56
- logs += f"✅ {out_path} → [Uploaded]({url})\n"
57
- except Exception as e:
58
- logs += f"❌ Exception khi xử lý {tgt_path}: {str(e)}\n"
59
-
60
- return output_files, logs
61
-
62
- # --- UI Gradio v4.44.1 ---
63
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
64
- demo.title = "Face Swapper Tool"
65
-
66
- gr.Markdown("# 🤖 Face Swapper")
67
- gr.Markdown("Upload ảnh/video cần swap và ảnh khuôn mặt để ghép")
68
-
69
- with gr.Row():
70
- target_input = gr.File(
71
- file_types=[("image",), ("video",)],
72
- file_count="multiple",
73
- label="Ảnh/Video Target"
74
- )
75
- source_input = gr.File(
76
- file_types=[("image",)],
77
- label="Ảnh Source"
78
- )
79
-
80
- run_btn = gr.Button("🔄 Chạy Face Swap")
81
- result_gallery = gr.Files(label="📂 File kết quả")
82
- logs_output = gr.Markdown() # Không có label
83
-
84
- run_btn.click(fn=run_scripts, inputs=[target_input, source_input], outputs=[result_gallery, logs_output])
85
-
86
- demo.launch(
87
- share=True,
88
- server_name="0.0.0.0",
89
- server_port=7860,
90
- ssr_mode=False
91
- )
 
1
  import gradio as gr
2
  import subprocess
3
+ import shutil
4
  import os
5
  from datetime import datetime
 
6
 
7
+ def run_scripts(target, source, use_face_enhancer):
8
+ outputfile=[]
9
+ for target_file in target :
10
+ target_extension = os.path.splitext(target_file.name)[-1]
11
+ target_name = os.path.splitext(target_file.name)[1]
12
+ timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
13
+ output_path1 = "output" + timestamp + target_extension
14
+ cmd1 = ["python3", "run.py", "-s", source.name, "-t", target_file.name, "-o", output_path1, "--frame-processor", "face_swapper","face_enhancer",'--many-faces']
15
+ subprocess.run(cmd1)
16
+ outputfile.append(output_path1)
17
+ print(output_path1)
18
+ return outputfile
19
+
20
+ iface = gr.Interface(
21
+ fn=run_scripts,
22
+ inputs=[
23
+ "files",
24
+ "file"
25
+ ],
26
+ outputs="files",
27
+ title="Face swapper",
28
+ description="Upload a target image/video and a source image to swap faces.",
29
+ live=False
30
+ )
31
+
32
+ iface.launch()