HusseinBashir commited on
Commit
54fd70a
·
verified ·
1 Parent(s): 2207201

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Request
2
+ from fastapi.responses import FileResponse
3
+ import torch
4
+ import numpy as np
5
+ import scipy.io.wavfile
6
+ from transformers import VitsModel, AutoTokenizer
7
+ import re
8
+
9
+ app = FastAPI()
10
+
11
+ # Load model and tokenizer
12
+ model = VitsModel.from_pretrained("Somali-tts/somali_tts_model")
13
+ tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
14
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
15
+ model.to(device)
16
+ model.eval()
17
+
18
+ number_words = {
19
+ 0: "eber", 1: "koow", 2: "labo", 3: "seddex", 4: "afar", 5: "shan",
20
+ 6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
21
+ 11: "toban iyo koow", 12: "toban iyo labo", 13: "toban iyo seddex",
22
+ 14: "toban iyo afar", 15: "toban iyo shan", 16: "toban iyo lix",
23
+ 17: "toban iyo todobo", 18: "toban iyo sideed", 19: "toban iyo sagaal",
24
+ 20: "labaatan", 30: "sodon", 40: "afartan", 50: "konton",
25
+ 60: "lixdan", 70: "todobaatan", 80: "sideetan", 90: "sagaashan",
26
+ 100: "boqol", 1000: "kun"
27
+ }
28
+
29
+ def number_to_words(number):
30
+ number = int(number)
31
+ if number < 20:
32
+ return number_words[number]
33
+ elif number < 100:
34
+ tens, unit = divmod(number, 10)
35
+ return number_words[tens * 10] + (" iyo " + number_words[unit] if unit else "")
36
+ elif number < 1000:
37
+ hundreds, remainder = divmod(number, 100)
38
+ part = (number_words[hundreds] + " boqol") if hundreds > 1 else "boqol"
39
+ if remainder:
40
+ part += " iyo " + number_to_words(remainder)
41
+ return part
42
+ elif number < 1000000:
43
+ thousands, remainder = divmod(number, 1000)
44
+ words = [number_to_words(thousands) + " kun" if thousands != 1 else "kun"]
45
+ if remainder:
46
+ words.append("iyo " + number_to_words(remainder))
47
+ return " ".join(words)
48
+ else:
49
+ return str(number)
50
+
51
+ def normalize_text(text):
52
+ numbers = re.findall(r'\d+', text)
53
+ for num in numbers:
54
+ text = text.replace(num, number_to_words(num))
55
+ text = text.replace("KH", "qa").replace("Z", "S")
56
+ text = text.replace("SH", "SHa'a").replace("DH", "Dha'a")
57
+ text = text.replace("ZamZam", "SamSam")
58
+ return text
59
+
60
+ @app.post("/tts")
61
+ async def tts(request: Request):
62
+ data = await request.json()
63
+ text = normalize_text(data["text"])
64
+ inputs = tokenizer(text, return_tensors="pt").to(device)
65
+ with torch.no_grad():
66
+ waveform = model(**inputs).waveform.squeeze().cpu().numpy()
67
+ filename = "output.wav"
68
+ scipy.io.wavfile.write(filename, rate=model.config.sampling_rate, data=(waveform * 32767).astype(np.int16))
69
+ return FileResponse(filename, media_type="audio/wav")