Spaces:
Sleeping
Sleeping
Arnold Manzano
commited on
Commit
·
bf72b12
1
Parent(s):
59bad9f
Added job count, fix video quality, tweak logging
Browse files- app.py +18 -5
- patches/facefusion.ini +1 -1
app.py
CHANGED
|
@@ -12,6 +12,15 @@ app = FastAPI()
|
|
| 12 |
# This acts as a queue: only 1 task can hold the "permit" at a time.
|
| 13 |
cpu_semaphore = asyncio.Semaphore(1)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
# Configuration
|
| 16 |
SOURCE_FACE = "source.jpg"
|
| 17 |
OUTPUT_DIR = Path("outputs")
|
|
@@ -26,7 +35,7 @@ def run_facefusion(target_path: Path, output_path: Path):
|
|
| 26 |
"--target-path", str(target_path),
|
| 27 |
"--output-path", str(output_path)
|
| 28 |
]
|
| 29 |
-
print(f"Executing: {' '.join(cmd)}")
|
| 30 |
try:
|
| 31 |
# We use subprocess.run here because it's called inside to_thread
|
| 32 |
subprocess.run(cmd, check=True)
|
|
@@ -34,13 +43,13 @@ def run_facefusion(target_path: Path, output_path: Path):
|
|
| 34 |
# if target_path.exists():
|
| 35 |
# target_path.unlink()
|
| 36 |
except Exception as e:
|
| 37 |
-
print(f"Error during processing: {e}")
|
| 38 |
|
| 39 |
async def background_worker(url: str, target_path: Path, output_path: Path, headers: dict):
|
| 40 |
"""Handles queuing, downloading, and processing"""
|
| 41 |
|
| 42 |
async with cpu_semaphore:
|
| 43 |
-
print(f"Starting processing for {url}")
|
| 44 |
|
| 45 |
try:
|
| 46 |
response = requests.get(url, headers=headers, timeout=30, stream=True)
|
|
@@ -49,12 +58,13 @@ async def background_worker(url: str, target_path: Path, output_path: Path, head
|
|
| 49 |
for chunk in response.iter_content(chunk_size=8192):
|
| 50 |
f.write(chunk)
|
| 51 |
except Exception as e:
|
| 52 |
-
print(f"Download failed in background: {e}")
|
| 53 |
return
|
| 54 |
await asyncio.to_thread(run_facefusion, target_path, output_path)
|
| 55 |
|
| 56 |
@app.get("/", response_class=HTMLResponse)
|
| 57 |
async def index():
|
|
|
|
| 58 |
files = []
|
| 59 |
for ext in EXTENSIONS:
|
| 60 |
files.extend(OUTPUT_DIR.glob(ext))
|
|
@@ -74,6 +84,7 @@ async def index():
|
|
| 74 |
<input type="text" name="url" placeholder="Paste Image/Video URL..." style="width:300px;" required>
|
| 75 |
<button type="submit">Start Swapping</button>
|
| 76 |
</form>
|
|
|
|
| 77 |
<hr>
|
| 78 |
<h2>Processed Files</h2>
|
| 79 |
<p><button onclick="location.reload()">Refresh List</button></p>
|
|
@@ -100,6 +111,8 @@ async def process_swap(background_tasks: BackgroundTasks, url: str = Form(...)):
|
|
| 100 |
"Connection": "keep-alive",
|
| 101 |
}
|
| 102 |
|
|
|
|
|
|
|
| 103 |
background_tasks.add_task(background_worker, url, target_path, output_path, headers)
|
| 104 |
|
| 105 |
return HTMLResponse(content=f"""
|
|
@@ -112,7 +125,7 @@ async def process_swap(background_tasks: BackgroundTasks, url: str = Form(...)):
|
|
| 112 |
@app.get("/download/{filename}")
|
| 113 |
async def download_file(filename: str):
|
| 114 |
file_path = OUTPUT_DIR / filename
|
| 115 |
-
print(f"Searching for file at: {file_path.absolute()}")
|
| 116 |
if file_path.exists():
|
| 117 |
return FileResponse(path=file_path)
|
| 118 |
raise HTTPException(status_code=404, detail="File not found.")
|
|
|
|
| 12 |
# This acts as a queue: only 1 task can hold the "permit" at a time.
|
| 13 |
cpu_semaphore = asyncio.Semaphore(1)
|
| 14 |
|
| 15 |
+
def get_queue_status():
|
| 16 |
+
# If value is 1, nobody is using it. If 0, it's busy.
|
| 17 |
+
is_busy = cpu_semaphore.locked()
|
| 18 |
+
# Number of tasks currently waiting for the lock
|
| 19 |
+
waiting_count = len(cpu_semaphore._waiters) if cpu_semaphore._waiters else 0
|
| 20 |
+
if not is_busy:
|
| 21 |
+
return "Idle"
|
| 22 |
+
return f"Processing (plus {waiting_count} in queue)"
|
| 23 |
+
|
| 24 |
# Configuration
|
| 25 |
SOURCE_FACE = "source.jpg"
|
| 26 |
OUTPUT_DIR = Path("outputs")
|
|
|
|
| 35 |
"--target-path", str(target_path),
|
| 36 |
"--output-path", str(output_path)
|
| 37 |
]
|
| 38 |
+
print(f"###### Executing: {' '.join(cmd)}")
|
| 39 |
try:
|
| 40 |
# We use subprocess.run here because it's called inside to_thread
|
| 41 |
subprocess.run(cmd, check=True)
|
|
|
|
| 43 |
# if target_path.exists():
|
| 44 |
# target_path.unlink()
|
| 45 |
except Exception as e:
|
| 46 |
+
print(f"###### Error during processing: {e}")
|
| 47 |
|
| 48 |
async def background_worker(url: str, target_path: Path, output_path: Path, headers: dict):
|
| 49 |
"""Handles queuing, downloading, and processing"""
|
| 50 |
|
| 51 |
async with cpu_semaphore:
|
| 52 |
+
print(f"###### Starting processing for {url}")
|
| 53 |
|
| 54 |
try:
|
| 55 |
response = requests.get(url, headers=headers, timeout=30, stream=True)
|
|
|
|
| 58 |
for chunk in response.iter_content(chunk_size=8192):
|
| 59 |
f.write(chunk)
|
| 60 |
except Exception as e:
|
| 61 |
+
print(f"###### Download failed in background: {e}")
|
| 62 |
return
|
| 63 |
await asyncio.to_thread(run_facefusion, target_path, output_path)
|
| 64 |
|
| 65 |
@app.get("/", response_class=HTMLResponse)
|
| 66 |
async def index():
|
| 67 |
+
status = get_queue_status()
|
| 68 |
files = []
|
| 69 |
for ext in EXTENSIONS:
|
| 70 |
files.extend(OUTPUT_DIR.glob(ext))
|
|
|
|
| 84 |
<input type="text" name="url" placeholder="Paste Image/Video URL..." style="width:300px;" required>
|
| 85 |
<button type="submit">Start Swapping</button>
|
| 86 |
</form>
|
| 87 |
+
<p><strong>System Status:</strong> {status}</p>
|
| 88 |
<hr>
|
| 89 |
<h2>Processed Files</h2>
|
| 90 |
<p><button onclick="location.reload()">Refresh List</button></p>
|
|
|
|
| 111 |
"Connection": "keep-alive",
|
| 112 |
}
|
| 113 |
|
| 114 |
+
print(f"###### Queueing job {url}")
|
| 115 |
+
|
| 116 |
background_tasks.add_task(background_worker, url, target_path, output_path, headers)
|
| 117 |
|
| 118 |
return HTMLResponse(content=f"""
|
|
|
|
| 125 |
@app.get("/download/{filename}")
|
| 126 |
async def download_file(filename: str):
|
| 127 |
file_path = OUTPUT_DIR / filename
|
| 128 |
+
print(f"###### Searching for file at: {file_path.absolute()}")
|
| 129 |
if file_path.exists():
|
| 130 |
return FileResponse(path=file_path)
|
| 131 |
raise HTTPException(status_code=404, detail="File not found.")
|
patches/facefusion.ini
CHANGED
|
@@ -58,7 +58,7 @@ output_audio_quality =
|
|
| 58 |
output_audio_volume =
|
| 59 |
output_video_encoder =
|
| 60 |
output_video_preset =
|
| 61 |
-
output_video_quality =
|
| 62 |
output_video_scale =
|
| 63 |
output_video_fps =
|
| 64 |
|
|
|
|
| 58 |
output_audio_volume =
|
| 59 |
output_video_encoder =
|
| 60 |
output_video_preset =
|
| 61 |
+
output_video_quality = 80
|
| 62 |
output_video_scale =
|
| 63 |
output_video_fps =
|
| 64 |
|