|
|
|
|
|
import numpy as np |
|
import soundfile |
|
import audresample |
|
import text_utils |
|
|
|
import re |
|
import subprocess |
|
import markdown |
|
import json |
|
from pathlib import Path |
|
from types import SimpleNamespace |
|
from flask import Flask, request, send_from_directory |
|
from flask_cors import CORS |
|
from audiocraft.builders import AudioGen |
|
NUM_SOUND_GENERATIONS = 1 |
|
sound_generator = AudioGen(duration=.74, device='cuda:0').to('cuda:0').eval() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CACHE_DIR = 'flask_cache/' |
|
Path(CACHE_DIR).mkdir(parents=True, exist_ok=True) |
|
|
|
|
|
|
|
|
|
def tts_multi_sentence(scene=None): |
|
if scene is not None and len(scene) >= 4: |
|
print(f'Processing: {scene} ..') |
|
|
|
x = sound_generator.generate( |
|
[scene] * NUM_SOUND_GENERATIONS |
|
).reshape(1, -1).detach().cpu().numpy() |
|
|
|
x /= np.abs(x).max() + 1e-7 |
|
|
|
x = audresample.resample(x, |
|
original_rate=16000, |
|
target_rate=24000)[0, :] |
|
|
|
|
|
|
|
print(f'Craft Finished for: {scene}\n\n\n\n____{x.shape}') |
|
else: |
|
print(scene, '\nDrop\n') |
|
x = np.zeros(400) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
cors = CORS(app) |
|
|
|
|
|
@app.route("/") |
|
def index(): |
|
with open('README.md', 'r') as f: |
|
return markdown.markdown(f.read()) |
|
|
|
|
|
@app.route("/", methods=['GET', 'POST', 'PUT']) |
|
def serve_wav(): |
|
|
|
|
|
r = request.form.to_dict(flat=False) |
|
|
|
|
|
args = SimpleNamespace( |
|
text=None if r.get('text') is None else r.get('text'), |
|
scene=r.get('scene')[0] |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
x = tts_multi_sentence(args.scene) |
|
|
|
OUT_FILE = 'tmp.wav' |
|
soundfile.write(CACHE_DIR + OUT_FILE, x, 16000) |
|
|
|
|
|
|
|
|
|
|
|
|
|
print(f'\n=SERVER saved as {OUT_FILE=}\n') |
|
response = send_from_directory(CACHE_DIR, path=OUT_FILE) |
|
response.headers['suffix-file-type'] = OUT_FILE |
|
return response |
|
|
|
|
|
if __name__ == "__main__": |
|
app.run(host="0.0.0.0") |
|
|