HusseinBashir commited on
Commit
678b81f
·
verified ·
1 Parent(s): bb66fc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -17
app.py CHANGED
@@ -1,17 +1,21 @@
1
- import gradio as gr
 
2
  import torch
3
  import numpy as np
4
  import scipy.io.wavfile
5
  from transformers import VitsModel, AutoTokenizer
6
  import re
7
 
8
- # Load fine-tuned model from Hugging Face Hub or local path
 
 
9
  model = VitsModel.from_pretrained("Somali-tts/somali_tts_model")
10
  tokenizer = AutoTokenizer.from_pretrained("saleolow/somali-mms-tts")
11
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
12
  model.to(device)
13
  model.eval()
14
 
 
15
  number_words = {
16
  0: "eber", 1: "koow", 2: "labo", 3: "seddex", 4: "afar", 5: "shan",
17
  6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
@@ -75,23 +79,13 @@ def normalize_text(text):
75
  text = text.replace("ZamZam", "SamSam")
76
  return text
77
 
78
- def tts(text):
 
 
 
79
  inputs = tokenizer(text, return_tensors="pt").to(device)
80
  with torch.no_grad():
81
  waveform = model(**inputs).waveform.squeeze().cpu().numpy()
82
  filename = "output.wav"
83
  scipy.io.wavfile.write(filename, rate=model.config.sampling_rate, data=(waveform * 32767).astype(np.int16))
84
- return filename # ✅ Audio file path
85
-
86
-
87
-
88
- gr.Interface(
89
- fn=tts,
90
- inputs=gr.Textbox(label="Geli qoraal Soomaali ah"),
91
- outputs=gr.Audio(label="Codka TTS", type="filepath"), # ✅ not gr.File
92
- title="Somali TTS",
93
- description="Ku qor qoraal Soomaaliyeed si aad u maqasho cod dabiici ah.",
94
- ).launch()
95
-
96
-
97
-
 
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 your model
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 conversion (keep your existing number_words + number_to_words)
19
  number_words = {
20
  0: "eber", 1: "koow", 2: "labo", 3: "seddex", 4: "afar", 5: "shan",
21
  6: "lix", 7: "todobo", 8: "sideed", 9: "sagaal", 10: "toban",
 
79
  text = text.replace("ZamZam", "SamSam")
80
  return text
81
 
82
+ @app.post("/tts")
83
+ async def tts(request: Request):
84
+ data = await request.json()
85
+ text = normalize_text(data["text"])
86
  inputs = tokenizer(text, return_tensors="pt").to(device)
87
  with torch.no_grad():
88
  waveform = model(**inputs).waveform.squeeze().cpu().numpy()
89
  filename = "output.wav"
90
  scipy.io.wavfile.write(filename, rate=model.config.sampling_rate, data=(waveform * 32767).astype(np.int16))
91
+ return FileResponse(filename, media_type="audio/wav")