Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,36 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
preload_models()
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
return (
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
).launch()
|
|
|
|
| 1 |
+
from bark import SAMPLE_RATE, generate_audio, preload_models
|
| 2 |
+
from scipy.io.wavfile import write as write_wav
|
| 3 |
+
import torch
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
from fastapi import FastAPI, Request, Form
|
| 7 |
+
from fastapi.responses import FileResponse
|
| 8 |
+
from fastapi.staticfiles import StaticFiles
|
| 9 |
+
from fastapi.templating import Jinja2Templates
|
| 10 |
+
|
| 11 |
+
# Patch torch.load for PyTorch 2.6+
|
| 12 |
+
if hasattr(torch, 'serialization'):
|
| 13 |
+
torch.serialization.add_safe_globals([
|
| 14 |
+
"numpy.core.multiarray.scalar"
|
| 15 |
+
])
|
| 16 |
+
|
| 17 |
+
# Create FastAPI app
|
| 18 |
+
app = FastAPI()
|
| 19 |
+
|
| 20 |
+
# Mount static and templates folder
|
| 21 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 22 |
+
templates = Jinja2Templates(directory="templates")
|
| 23 |
+
|
| 24 |
+
# Preload Bark models
|
| 25 |
preload_models()
|
| 26 |
|
| 27 |
+
@app.get("/")
|
| 28 |
+
def index(request: Request):
|
| 29 |
+
return templates.TemplateResponse("index.html", {"request": request})
|
| 30 |
|
| 31 |
+
@app.post("/synthesize")
|
| 32 |
+
def synthesize(request: Request, text: str = Form(...)):
|
| 33 |
+
audio_array = generate_audio(text)
|
| 34 |
+
output_path = "static/output.wav"
|
| 35 |
+
write_wav(output_path, SAMPLE_RATE, audio_array)
|
| 36 |
+
return FileResponse(output_path, media_type="audio/wav", filename="output.wav")
|
|
|