Ii commited on
Commit
ad53592
·
verified ·
1 Parent(s): 6680833

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -8
app.py CHANGED
@@ -3,6 +3,7 @@ import gradio as gr
3
  from refacer import Refacer
4
  import os
5
  import requests
 
6
 
7
  # Hugging Face URL to download the model
8
  model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
@@ -47,15 +48,19 @@ def run(video_path, *vars):
47
  refaced_video_path = refacer.reface(video_path, faces)
48
  print(f"Refaced video can be found at {refaced_video_path}")
49
 
50
- # Convert the output video to memory buffer
51
- video_buffer = io.BytesIO()
52
  with open(refaced_video_path, "rb") as f:
53
- video_buffer.write(f.read())
54
-
55
- # Rewind the buffer to the beginning
56
- video_buffer.seek(0)
57
-
58
- return video_buffer # Gradio will handle the video display
 
 
 
 
 
59
 
60
  # Prepare Gradio components
61
  with gr.Blocks() as demo:
 
3
  from refacer import Refacer
4
  import os
5
  import requests
6
+ import tempfile # Importing tempfile module
7
 
8
  # Hugging Face URL to download the model
9
  model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
 
48
  refaced_video_path = refacer.reface(video_path, faces)
49
  print(f"Refaced video can be found at {refaced_video_path}")
50
 
51
+ # Open the refaced video file and write to a temporary file
 
52
  with open(refaced_video_path, "rb") as f:
53
+ # Use tempfile to store the refaced video in memory
54
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4")
55
+ temp_file.write(f.read())
56
+ temp_file.close() # Close the temporary file
57
+
58
+ # Reopen the temp file to use in the Gradio UI
59
+ with open(temp_file.name, "rb") as temp_f:
60
+ video_buffer = io.BytesIO(temp_f.read())
61
+
62
+ # Return the video buffer for Gradio to display in the UI
63
+ return video_buffer # Gradio will handle the video display
64
 
65
  # Prepare Gradio components
66
  with gr.Blocks() as demo: