Spaces:
Configuration error
Configuration error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,19 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
-
import numpy as np
|
| 4 |
-
import scipy.io.wavfile
|
| 5 |
-
from transformers import VitsModel, AutoTokenizer
|
| 6 |
-
import re
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
-
|
| 12 |
-
model.to(device).eval()
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
|
| 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 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 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 |
-
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 |
-
return text
|
| 56 |
-
|
| 57 |
-
def tts(text):
|
| 58 |
-
text = normalize_text(text)
|
| 59 |
-
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 60 |
-
with torch.no_grad():
|
| 61 |
-
waveform = model(**inputs).waveform.squeeze().cpu().numpy()
|
| 62 |
-
output_path = "output.wav"
|
| 63 |
-
scipy.io.wavfile.write(output_path, rate=model.config.sampling_rate, data=(waveform * 32767).astype(np.int16))
|
| 64 |
-
return output_path
|
| 65 |
-
|
| 66 |
-
gr.Interface(
|
| 67 |
-
fn=tts,
|
| 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 transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 2 |
+
from fastapi import FastAPI
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("cmlang/somali-flan-t5")
|
| 7 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("cmlang/somali-flan-t5")
|
| 8 |
|
| 9 |
+
app = FastAPI()
|
|
|
|
| 10 |
|
| 11 |
+
class Message(BaseModel):
|
| 12 |
+
text: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
@app.post("/chatbot")
|
| 15 |
+
async def chatbot(msg: Message):
|
| 16 |
+
inputs = tokenizer(msg.text, return_tensors="pt")
|
| 17 |
+
outputs = model.generate(**inputs, max_length=150)
|
| 18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 19 |
+
return {"response": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|