aastha-malik commited on
Commit
ee20eba
Β·
1 Parent(s): ae7d512

Remove YouTube download (blocked on HF), upload-only

Browse files
Files changed (2) hide show
  1. app.py +15 -53
  2. requirements.txt +0 -1
app.py CHANGED
@@ -45,31 +45,6 @@ swapper = insightface.model_zoo.get_model(INSWAPPER_PATH, providers=PROVIDERS)
45
 
46
  print("Models loaded.")
47
 
48
- # ── Helpers ──────────────────────────────────────────────────────────
49
- def download_youtube(url: str, out_path: str):
50
- raw = out_path.replace(".mp4", "_raw.mp4")
51
- import yt_dlp
52
- ydl_opts = {
53
- "outtmpl": raw,
54
- "format": "best[height<=480]/best",
55
- "http_headers": {
56
- "User-Agent": (
57
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
58
- "AppleWebKit/537.36 Chrome/120.0.0.0 Safari/537.36"
59
- )
60
- },
61
- }
62
- with yt_dlp.YoutubeDL(ydl_opts) as ydl:
63
- ydl.download([url])
64
- subprocess.run(
65
- ["ffmpeg", "-y", "-i", raw,
66
- "-vcodec", "libx264", "-acodec", "aac", "-preset", "fast",
67
- out_path, "-loglevel", "error"],
68
- check=True,
69
- )
70
- if os.path.exists(raw):
71
- os.remove(raw)
72
-
73
 
74
  def to_h264(src: str, dst: str):
75
  subprocess.run(
@@ -81,11 +56,11 @@ def to_h264(src: str, dst: str):
81
 
82
 
83
  # ── Core processing ──────────────────────────────────────────────────
84
- def process(face_image, video_file, youtube_url, trim_seconds, progress=gr.Progress(track_tqdm=True)):
85
  if face_image is None:
86
  return None, "Please upload a source face image."
87
- if video_file is None and not (youtube_url or "").strip():
88
- return None, "Please upload a video file or paste a YouTube URL."
89
 
90
  try:
91
  progress(0.0, desc="Detecting source face...")
@@ -108,30 +83,25 @@ def process(face_image, video_file, youtube_url, trim_seconds, progress=gr.Progr
108
 
109
  # Prepare video
110
  progress(0.05, desc="Preparing video...")
111
- raw_video = f"{WORK_DIR}/temp/input.mp4"
 
112
 
113
- if (youtube_url or "").strip():
114
- progress(0.05, desc="Downloading YouTube video...")
115
- download_youtube(youtube_url.strip(), raw_video)
116
- else:
117
- shutil.copy(video_file, raw_video)
118
- converted = f"{WORK_DIR}/temp/input_h264.mp4"
119
- to_h264(raw_video, converted)
120
- shutil.move(converted, raw_video)
121
 
122
  # Verify codec
123
- cap_check = cv2.VideoCapture(raw_video)
124
  ok, _ = cap_check.read()
125
  cap_check.release()
126
  if not ok:
127
- return None, "Could not read the video. Try a different file or URL."
128
 
129
  # Trim
130
- input_video = raw_video
131
  if trim_seconds and int(trim_seconds) > 0:
132
  trimmed = f"{WORK_DIR}/temp/input_trimmed.mp4"
133
  subprocess.run(
134
- ["ffmpeg", "-y", "-i", raw_video,
135
  "-t", str(int(trim_seconds)),
136
  "-c:v", "libx264", "-c:a", "aac",
137
  trimmed, "-loglevel", "error"],
@@ -140,7 +110,7 @@ def process(face_image, video_file, youtube_url, trim_seconds, progress=gr.Progr
140
  input_video = trimmed
141
 
142
  # Video info
143
- cap = cv2.VideoCapture(input_video)
144
  fps = cap.get(cv2.CAP_PROP_FPS)
145
  total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
146
  w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
@@ -162,10 +132,8 @@ def process(face_image, video_file, youtube_url, trim_seconds, progress=gr.Progr
162
 
163
  faces = face_app.get(frame)
164
  result = frame.copy()
165
-
166
  for face in faces:
167
  result = swapper.get(result, face, source_face, paste_back=True)
168
-
169
  writer.write(result)
170
 
171
  cap.release()
@@ -185,7 +153,7 @@ def process(face_image, video_file, youtube_url, trim_seconds, progress=gr.Progr
185
 
186
  progress(1.0, desc="Done!")
187
  size = os.path.getsize(final_out) / (1024 * 1024)
188
- return final_out, f"Done! {total} frames | {size:.1f} MB output"
189
 
190
  except Exception as e:
191
  return None, f"Error: {e}"
@@ -198,8 +166,7 @@ with gr.Blocks(title="Face Fusion") as demo:
198
  # 🎭 Face Fusion β€” AI Video Face Swap
199
  Swap any face into a video using **InsightFace + inswapper_128**.
200
 
201
- > **Note:** This Space runs on CPU β€” processing takes ~1–3 min per 10 seconds of video.
202
- > For faster results, run the notebook on Kaggle with a free GPU.
203
  """)
204
 
205
  with gr.Row():
@@ -209,11 +176,6 @@ Swap any face into a video using **InsightFace + inswapper_128**.
209
  type="filepath",
210
  height=220,
211
  )
212
- youtube_input = gr.Textbox(
213
- label="YouTube URL (optional)",
214
- placeholder="https://www.youtube.com/watch?v=...",
215
- )
216
- gr.Markdown("**β€” or β€”**")
217
  video_input = gr.Video(label="Upload Video File")
218
  trim_input = gr.Slider(
219
  label="Trim to first N seconds (0 = full video)",
@@ -235,7 +197,7 @@ Swap any face into a video using **InsightFace + inswapper_128**.
235
 
236
  run_btn.click(
237
  fn=process,
238
- inputs=[face_input, video_input, youtube_input, trim_input],
239
  outputs=[video_out, status_box],
240
  )
241
 
 
45
 
46
  print("Models loaded.")
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  def to_h264(src: str, dst: str):
50
  subprocess.run(
 
56
 
57
 
58
  # ── Core processing ──────────────────────────────────────────────────
59
+ def process(face_image, video_file, trim_seconds, progress=gr.Progress(track_tqdm=True)):
60
  if face_image is None:
61
  return None, "Please upload a source face image."
62
+ if video_file is None:
63
+ return None, "Please upload a video file."
64
 
65
  try:
66
  progress(0.0, desc="Detecting source face...")
 
83
 
84
  # Prepare video
85
  progress(0.05, desc="Preparing video...")
86
+ raw_video = f"{WORK_DIR}/temp/input.mp4"
87
+ converted = f"{WORK_DIR}/temp/input_h264.mp4"
88
 
89
+ shutil.copy(video_file, raw_video)
90
+ to_h264(raw_video, converted)
 
 
 
 
 
 
91
 
92
  # Verify codec
93
+ cap_check = cv2.VideoCapture(converted)
94
  ok, _ = cap_check.read()
95
  cap_check.release()
96
  if not ok:
97
+ return None, "Could not read the video β€” try a different file format."
98
 
99
  # Trim
100
+ input_video = converted
101
  if trim_seconds and int(trim_seconds) > 0:
102
  trimmed = f"{WORK_DIR}/temp/input_trimmed.mp4"
103
  subprocess.run(
104
+ ["ffmpeg", "-y", "-i", converted,
105
  "-t", str(int(trim_seconds)),
106
  "-c:v", "libx264", "-c:a", "aac",
107
  trimmed, "-loglevel", "error"],
 
110
  input_video = trimmed
111
 
112
  # Video info
113
+ cap = cv2.VideoCapture(input_video)
114
  fps = cap.get(cv2.CAP_PROP_FPS)
115
  total = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
116
  w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 
132
 
133
  faces = face_app.get(frame)
134
  result = frame.copy()
 
135
  for face in faces:
136
  result = swapper.get(result, face, source_face, paste_back=True)
 
137
  writer.write(result)
138
 
139
  cap.release()
 
153
 
154
  progress(1.0, desc="Done!")
155
  size = os.path.getsize(final_out) / (1024 * 1024)
156
+ return final_out, f"Done! {total} frames | {size:.1f} MB"
157
 
158
  except Exception as e:
159
  return None, f"Error: {e}"
 
166
  # 🎭 Face Fusion β€” AI Video Face Swap
167
  Swap any face into a video using **InsightFace + inswapper_128**.
168
 
169
+ > **Note:** Runs on CPU β€” ~1–3 min per 10 seconds of video. For GPU speed, run the notebook on Kaggle.
 
170
  """)
171
 
172
  with gr.Row():
 
176
  type="filepath",
177
  height=220,
178
  )
 
 
 
 
 
179
  video_input = gr.Video(label="Upload Video File")
180
  trim_input = gr.Slider(
181
  label="Trim to first N seconds (0 = full video)",
 
197
 
198
  run_btn.click(
199
  fn=process,
200
+ inputs=[face_input, video_input, trim_input],
201
  outputs=[video_out, status_box],
202
  )
203
 
requirements.txt CHANGED
@@ -2,5 +2,4 @@ insightface==0.7.3
2
  onnxruntime
3
  opencv-python-headless
4
  huggingface_hub
5
- yt-dlp
6
  gradio
 
2
  onnxruntime
3
  opencv-python-headless
4
  huggingface_hub
 
5
  gradio