Alicehy commited on
Commit
4ea1822
·
1 Parent(s): 028d68e

Add app + Dockerfile + requirements + UI

Browse files
Files changed (4) hide show
  1. Dockerfile +6 -0
  2. app.py +26 -0
  3. public/index.html +13 -0
  4. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+ WORKDIR /app
3
+ COPY requirements.txt .
4
+ RUN pip install --no-cache-dir -r requirements.txt
5
+ COPY . .
6
+ CMD uvicorn app:app --host 0.0.0.0 --port $PORT
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from fastapi import FastAPI
3
+ from fastapi.responses import StreamingResponse
4
+ from pydantic import BaseModel
5
+ from transformers import VitsModel, AutoTokenizer
6
+ import torch, numpy as np, io
7
+ from scipy.io.wavfile import write
8
+ from fastapi.staticfiles import StaticFiles
9
+
10
+
11
+
12
+ MODEL = VitsModel.from_pretrained("facebook/mms-tts-bor").eval()
13
+ TOK = AutoTokenizer.from_pretrained("facebook/mms-tts-bor")
14
+ app = FastAPI()
15
+ app.mount("/ui", StaticFiles(directory="public", html=True), name="ui")
16
+
17
+ class Inp(BaseModel):
18
+ text: str
19
+
20
+ @app.post("/tts")
21
+ def tts(inp: Inp):
22
+ with torch.inference_mode():
23
+ audio = MODEL(**TOK(inp.text, return_tensors="pt")).waveform.squeeze().cpu().numpy()
24
+ audio = audio / (np.max(np.abs(audio)) + 1e-9)
25
+ buf = io.BytesIO(); write(buf, MODEL.config.sampling_rate, (audio*32767).astype(np.int16)); buf.seek(0)
26
+ return StreamingResponse(buf, media_type="audio/wav")
public/index.html ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!doctype html><meta charset="utf-8">
2
+ <textarea id="t" rows="3" style="width:100%">Aru ebaradoge iwadure boe...</textarea>
3
+ <button id="go">Convert</button>
4
+ <audio id="aud" controls></audio>
5
+ <script>
6
+ document.getElementById('go').onclick = async () => {
7
+ const r = await fetch('/tts',{method:'POST',headers:{'Content-Type':'application/json'},
8
+ body: JSON.stringify({text: document.getElementById('t').value})});
9
+ const b = await r.blob();
10
+ document.getElementById('aud').src = URL.createObjectURL(b);
11
+ document.getElementById('aud').play();
12
+ };
13
+ </script>
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ --extra-index-url https://download.pytorch.org/whl/cpu
2
+ fastapi
3
+ uvicorn
4
+ transformers
5
+ scipy
6
+ torch