Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
import torch
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from fastapi import FastAPI, UploadFile, File, Form, BackgroundTasks
|
| 6 |
from fastapi.responses import FileResponse
|
| 7 |
from TTS.api import TTS
|
| 8 |
import uvicorn
|
| 9 |
-
from pydub import AudioSegment
|
| 10 |
|
| 11 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 12 |
|
|
@@ -33,12 +33,21 @@ OUTPUT_DIR = "outputs"
|
|
| 33 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 34 |
|
| 35 |
# =========================
|
| 36 |
-
# 🔊 AUDIO CONVERT
|
| 37 |
# =========================
|
| 38 |
def convert_to_wav(input_path, output_path):
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# =========================
|
| 44 |
# 🧹 CLEAN
|
|
@@ -79,15 +88,15 @@ async def clone_voice_api(
|
|
| 79 |
|
| 80 |
busy = True
|
| 81 |
|
| 82 |
-
raw_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_raw"
|
| 83 |
input_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_clean.wav"
|
| 84 |
output_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_out.wav"
|
| 85 |
|
| 86 |
-
# Save
|
| 87 |
with open(raw_path, "wb") as f:
|
| 88 |
f.write(await audio.read())
|
| 89 |
|
| 90 |
-
# Convert
|
| 91 |
convert_to_wav(raw_path, input_path)
|
| 92 |
cleanup_files(raw_path)
|
| 93 |
|
|
@@ -139,7 +148,7 @@ async def clone_voice_ui(audio_path, text, language):
|
|
| 139 |
return "✅ Done", output_path
|
| 140 |
|
| 141 |
with gr.Blocks() as demo:
|
| 142 |
-
gr.Markdown("# ⚡ Ultra Fast
|
| 143 |
|
| 144 |
a = gr.Audio(type="filepath")
|
| 145 |
t = gr.Textbox()
|
|
|
|
| 1 |
import os
|
| 2 |
import uuid
|
| 3 |
import torch
|
| 4 |
+
import torchaudio
|
| 5 |
import gradio as gr
|
| 6 |
from fastapi import FastAPI, UploadFile, File, Form, BackgroundTasks
|
| 7 |
from fastapi.responses import FileResponse
|
| 8 |
from TTS.api import TTS
|
| 9 |
import uvicorn
|
|
|
|
| 10 |
|
| 11 |
os.environ["COQUI_TOS_AGREED"] = "1"
|
| 12 |
|
|
|
|
| 33 |
os.makedirs(OUTPUT_DIR, exist_ok=True)
|
| 34 |
|
| 35 |
# =========================
|
| 36 |
+
# 🔊 AUDIO CONVERT (NO FFMPEG)
|
| 37 |
# =========================
|
| 38 |
def convert_to_wav(input_path, output_path):
|
| 39 |
+
waveform, sr = torchaudio.load(input_path)
|
| 40 |
+
|
| 41 |
+
# Convert to mono
|
| 42 |
+
if waveform.shape[0] > 1:
|
| 43 |
+
waveform = waveform.mean(dim=0, keepdim=True)
|
| 44 |
+
|
| 45 |
+
# Resample to 16kHz
|
| 46 |
+
if sr != 16000:
|
| 47 |
+
resampler = torchaudio.transforms.Resample(orig_freq=sr, new_freq=16000)
|
| 48 |
+
waveform = resampler(waveform)
|
| 49 |
+
|
| 50 |
+
torchaudio.save(output_path, waveform, 16000)
|
| 51 |
|
| 52 |
# =========================
|
| 53 |
# 🧹 CLEAN
|
|
|
|
| 88 |
|
| 89 |
busy = True
|
| 90 |
|
| 91 |
+
raw_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_raw.wav"
|
| 92 |
input_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_clean.wav"
|
| 93 |
output_path = f"{OUTPUT_DIR}/{uuid.uuid4()}_out.wav"
|
| 94 |
|
| 95 |
+
# Save file
|
| 96 |
with open(raw_path, "wb") as f:
|
| 97 |
f.write(await audio.read())
|
| 98 |
|
| 99 |
+
# 🔥 Convert (fast)
|
| 100 |
convert_to_wav(raw_path, input_path)
|
| 101 |
cleanup_files(raw_path)
|
| 102 |
|
|
|
|
| 148 |
return "✅ Done", output_path
|
| 149 |
|
| 150 |
with gr.Blocks() as demo:
|
| 151 |
+
gr.Markdown("# ⚡ XTTS (No FFmpeg - Ultra Fast)")
|
| 152 |
|
| 153 |
a = gr.Audio(type="filepath")
|
| 154 |
t = gr.Textbox()
|