umersalim commited on
Commit
49f3460
·
verified ·
1 Parent(s): c412903

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +145 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,145 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import re
4
+ from dataclasses import dataclass
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ from neucodec import NeuCodec
7
+
8
+ @dataclass
9
+ class Config:
10
+ model_name = "StepSharp/urdu-tts"
11
+ device_map = "auto"
12
+ max_new_tokens = 2048
13
+ temperature = 0.8
14
+ top_p = 0.95
15
+ repetition_penalty = 1.1
16
+
17
+
18
+ class UrduTTS:
19
+ def __init__(self):
20
+ self.device = "cuda" if torch.cuda.is_available() else "cpu"
21
+
22
+ self.tokenizer = AutoTokenizer.from_pretrained(
23
+ Config.model_name
24
+ )
25
+
26
+ self.model = AutoModelForCausalLM.from_pretrained(
27
+ Config.model_name,
28
+ torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,
29
+ device_map=Config.device_map,
30
+ )
31
+
32
+ self.codec = NeuCodec.from_pretrained(
33
+ "neuphonic/neucodec"
34
+ ).eval().to(self.device)
35
+
36
+ vocab = self.tokenizer.get_vocab()
37
+ self.speech_end = vocab["<|im_end|>"]
38
+
39
+ def synthesize(self, text, description):
40
+
41
+ speaker = "OutteTTS-urdu-dataset_audio_uat_speaker"
42
+
43
+ prompt = (
44
+ f"<|im_start|>{speaker}: {text}"
45
+ f"<|description|>{description}"
46
+ f"<|speech_start|>"
47
+ )
48
+
49
+ inputs = self.tokenizer(
50
+ prompt,
51
+ return_tensors="pt"
52
+ )
53
+
54
+ input_ids = inputs.input_ids.to(self.device)
55
+
56
+ output = self.model.generate(
57
+ input_ids=input_ids,
58
+ max_new_tokens=2048,
59
+ do_sample=True,
60
+ temperature=0.8,
61
+ top_p=0.95,
62
+ repetition_penalty=1.1,
63
+ eos_token_id=self.speech_end,
64
+ )
65
+
66
+ decoded = self.tokenizer.decode(
67
+ output[0],
68
+ skip_special_tokens=False
69
+ )
70
+
71
+ audio_tokens = re.findall(
72
+ r"<\|s_(\d+)\|>",
73
+ decoded
74
+ )
75
+
76
+ audio_tokens = [int(x) for x in audio_tokens]
77
+
78
+ codes = (
79
+ torch.tensor(audio_tokens)
80
+ .unsqueeze(0)
81
+ .unsqueeze(0)
82
+ .to(self.device)
83
+ )
84
+
85
+ with torch.inference_mode():
86
+ waveform = self.codec.decode_code(codes)
87
+
88
+ audio = waveform[0, 0].cpu().numpy()
89
+
90
+ return 24000, audio
91
+
92
+ # model load
93
+ tts = UrduTTS()
94
+
95
+
96
+ def generate_audio(text, description):
97
+ return tts.synthesize(text, description)
98
+ # return None
99
+
100
+
101
+ with gr.Blocks(title="Urdu TTS") as demo:
102
+
103
+ gr.Markdown(
104
+ """
105
+ # Urdu Text-to-Speech
106
+ Enter Urdu text and generate speech.
107
+ """
108
+ )
109
+
110
+ text = gr.Textbox(
111
+ label="Urdu Text",
112
+ lines=4,
113
+ placeholder="اردو متن درج کریں"
114
+ )
115
+
116
+ description = gr.Textbox(
117
+ label="Voice Description",
118
+ value="A male Urdu speaker with a calm and clear tone."
119
+ )
120
+
121
+ btn = gr.Button("Generate Speech")
122
+
123
+ output = gr.Audio(
124
+ label="Generated Audio"
125
+ )
126
+
127
+ btn.click(
128
+ fn=generate_audio,
129
+ inputs=[text, description],
130
+ outputs=output
131
+ )
132
+
133
+ gr.Examples(
134
+ examples=[
135
+ ["میری عمر اس وقت 26 سال ہے اور اگلا سال 2025 ہوگا۔"],
136
+ ["براہ کرم چند لمحے انتظار کریں۔"],
137
+ ["محکمۂ موسمیات کے مطابق درجۂ حرارت ٤٦٫٨ ڈگری سینٹی گریڈ تک پہنچ سکتا ہے، لہٰذا شہری غیرضروری سفر سے گریز کریں۔"],
138
+ ["بین الاقوامی خلائی تحقیقاتی ادارے نے اعلان کیا کہ سیٹلائٹ “PakSat-X2”"],
139
+ ["اگر temperature 42.7 ڈگری سینٹی گریڈ سے تجاوز کر جائے تو server automatically shutdown ہو جائے گا۔"]
140
+ ],
141
+ inputs=text
142
+ )
143
+
144
+ if __name__ == "__main__":
145
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ transformers
4
+ accelerate
5
+ sentencepiece
6
+ neucodec