Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,46 @@
|
|
1 |
-
|
2 |
-
import
|
3 |
-
import numpy as np
|
4 |
-
import scipy.io.wavfile
|
5 |
from transformers import VitsModel, AutoTokenizer
|
6 |
-
import
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
13 |
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
11: "toban iyo koow", 12: "toban iyo labo", 13: "toban iyo seddex",
|
18 |
-
14: "toban iyo afar", 15: "toban iyo shan", 16: "toban iyo lix",
|
19 |
-
17: "toban iyo todobo", 18: "toban iyo sideed", 19: "toban iyo sagaal",
|
20 |
-
20: "labaatan", 30: "sodon", 40: "afartan", 50: "konton",
|
21 |
-
60: "lixdan", 70: "todobaatan", 80: "sideetan", 90: "sagaashan",
|
22 |
-
100: "boqol", 1000: "kun"
|
23 |
-
}
|
24 |
|
25 |
-
|
26 |
-
|
27 |
-
if number < 20:
|
28 |
-
return number_words[number]
|
29 |
-
elif number < 100:
|
30 |
-
tens, unit = divmod(number, 10)
|
31 |
-
return number_words[tens * 10] + (" iyo " + number_words[unit] if unit else "")
|
32 |
-
elif number < 1000:
|
33 |
-
hundreds, remainder = divmod(number, 100)
|
34 |
-
part = (number_words[hundreds] + " boqol") if hundreds > 1 else "boqol"
|
35 |
-
if remainder:
|
36 |
-
part += " iyo " + number_to_words(remainder)
|
37 |
-
return part
|
38 |
-
elif number < 1000000:
|
39 |
-
thousands, remainder = divmod(number, 1000)
|
40 |
-
words = []
|
41 |
-
if thousands == 1:
|
42 |
-
words.append("kun")
|
43 |
-
else:
|
44 |
-
words.append(number_to_words(thousands) + " kun")
|
45 |
-
if remainder:
|
46 |
-
words.append("iyo " + number_to_words(remainder))
|
47 |
-
return " ".join(words)
|
48 |
-
else:
|
49 |
-
return str(number)
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
|
|
56 |
|
57 |
-
|
58 |
-
text = normalize_text(text)
|
59 |
-
inputs = tokenizer(text, return_tensors="pt").to(device)
|
60 |
with torch.no_grad():
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
inputs=gr.Textbox(label="Qor qoraalka af-Soomaaliga"),
|
69 |
-
outputs=gr.Audio(type="filepath", label="Codka TTS"),
|
70 |
-
title="Somali TTS API",
|
71 |
-
description="Ku qor qoraal si aad u maqasho codka af-Soomaaliga",
|
72 |
-
).launch()
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
|
|
|
|
3 |
from transformers import VitsModel, AutoTokenizer
|
4 |
+
import torch
|
5 |
+
import base64
|
6 |
+
import io
|
7 |
+
import soundfile as sf
|
8 |
|
9 |
+
app = FastAPI()
|
|
|
10 |
|
11 |
+
# Load model & tokenizer hal mar marka server-ka bilaabmo
|
12 |
+
model_name = "Somali-tts/somali_tts_model"
|
13 |
+
model = VitsModel.from_pretrained(model_name)
|
14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
15 |
|
16 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
17 |
+
model.to(device)
|
18 |
+
model.eval()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
class TTSRequest(BaseModel):
|
21 |
+
text: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
@app.post("/tts")
|
24 |
+
async def tts(request: TTSRequest):
|
25 |
+
text = request.text
|
26 |
+
# Tokenize input text
|
27 |
+
tokens = tokenizer(text, return_tensors="pt")
|
28 |
+
tokens = {k: v.to(device) for k, v in tokens.items()}
|
29 |
|
30 |
+
# Generate speech waveform tensor
|
|
|
|
|
31 |
with torch.no_grad():
|
32 |
+
audio = model.generate_speech(tokens['input_ids'])
|
33 |
+
|
34 |
+
# Convert tensor to numpy array
|
35 |
+
audio_np = audio.squeeze().cpu().numpy()
|
36 |
+
|
37 |
+
# Write to WAV buffer
|
38 |
+
buffer = io.BytesIO()
|
39 |
+
sf.write(buffer, audio_np, samplerate=22050, format='WAV')
|
40 |
+
wav_bytes = buffer.getvalue()
|
41 |
+
|
42 |
+
# Encode wav bytes to base64
|
43 |
+
b64_audio = base64.b64encode(wav_bytes).decode('utf-8')
|
44 |
|
45 |
+
# Return base64 audio string with wav header
|
46 |
+
return {"audio": f"data:audio/wav;base64,{b64_audio}"}
|
|
|
|
|
|
|
|
|
|