File size: 14,663 Bytes
273cd6b
 
 
 
 
712a04d
6cc4283
973c041
 
 
 
 
 
21c61c8
973c041
21c61c8
 
 
 
973c041
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d65ec35
 
 
973c041
 
 
479acdd
 
233814f
e6bbc72
 
 
6489eac
e6bbc72
 
 
2a84d5f
 
 
e0d8ef7
 
 
 
 
 
e6bbc72
 
 
 
 
 
 
1318041
 
e6bbc72
 
 
973c041
4e497b1
973c041
 
 
9bba678
973c041
 
bc4cee8
d65ec35
45416f5
 
 
2abe848
 
 
 
 
 
bc4cee8
21c61c8
2abe848
bb1a6ad
a740b3f
2116406
696ac95
712a04d
e897a8d
712a04d
479acdd
712a04d
273cd6b
6cc4283
f1db6b0
6cc4283
 
 
6882990
6cc4283
edc1919
27b9232
f1db6b0
 
7926329
02b77f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7926329
f192519
7926329
f192519
 
 
 
a9705e7
f192519
 
a9705e7
 
f192519
 
 
82c24c7
 
 
 
 
 
 
 
 
 
 
 
 
 
790af18
 
 
 
d6d1977
82c24c7
c962614
 
82c24c7
c962614
6489eac
c962614
d47249c
82c24c7
 
c962614
 
 
6489eac
c962614
 
d47249c
6bdcbf1
82c24c7
d28f8b7
 
82c24c7
 
 
 
d28f8b7
9ab3574
d873b1b
102ba33
82c24c7
 
 
 
 
 
 
 
 
 
d47249c
 
82c24c7
 
 
 
d2e875d
d28f8b7
4e497b1
 
 
 
 
28534de
4e497b1
d873b1b
 
 
6489eac
 
 
 
 
d873b1b
 
6489eac
d873b1b
28534de
 
 
 
ef68d30
 
 
 
 
6bdcbf1
31b6072
6bdcbf1
9ab3574
 
 
 
76d6781
45416f5
9ab3574
45416f5
9ab3574
 
7cfa1ec
7f5dd13
9ab3574
 
 
 
 
6882990
d9ceaee
31b6072
 
45416f5
9ab3574
bba3493
9ab3574
eef1809
973c041
db8cb05
973c041
 
d873b1b
 
 
6489eac
 
 
3bb95e2
221b2cc
d873b1b
 
6489eac
d873b1b
db8cb05
973c041
 
 
 
 
d873b1b
973c041
7926329
 
 
 
 
f192519
 
 
 
 
973c041
a9705e7
7926329
973c041
368a410
8fc7059
3b54e05
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import os

import numpy as np

import gradio as gr
import requests
from genai_chat_ai import AI,create_chat_session

import torch
from typing import Any, Callable, Optional, Tuple, Union,Iterator
import numpy as np
import torch.nn as nn # Import the missing module

import noisereduce as nr

def remove_noise_nr(audio_data,sr=16000):
    """يزيل الضوضاء باستخدام مكتبة noisereduce."""
    reduced_noise = nr.reduce_noise(y=audio_data, sr=sr)
    return reduced_noise

def _inference_forward_stream(
        self,
        input_ids: Optional[torch.Tensor] = None,
        attention_mask: Optional[torch.Tensor] = None,
        speaker_embeddings: Optional[torch.Tensor] = None,
        output_attentions: Optional[bool] = None,
        output_hidden_states: Optional[bool] = None,
        return_dict: Optional[bool] = None,
        padding_mask: Optional[torch.Tensor] = None,
        chunk_size: int = 32,  # Chunk size for streaming output
    ) -> Iterator[torch.Tensor]:
        """Generates speech waveforms in a streaming fashion."""
        if attention_mask is not None:
            padding_mask = attention_mask.unsqueeze(-1).float()
        else:
            padding_mask = torch.ones_like(input_ids).unsqueeze(-1).float()



        text_encoder_output = self.text_encoder(
            input_ids=input_ids,
            padding_mask=padding_mask,
            attention_mask=attention_mask,
            output_attentions=output_attentions,
            output_hidden_states=output_hidden_states,
            return_dict=return_dict,
        )
        hidden_states = text_encoder_output[0] if not return_dict else text_encoder_output.last_hidden_state
        hidden_states = hidden_states.transpose(1, 2)
        input_padding_mask = padding_mask.transpose(1, 2)

        prior_means = text_encoder_output[1] if not return_dict else text_encoder_output.prior_means
        prior_log_variances = text_encoder_output[2] if not return_dict else text_encoder_output.prior_log_variances

        if self.config.use_stochastic_duration_prediction:
            log_duration = self.duration_predictor(
                hidden_states,
                input_padding_mask,
                speaker_embeddings,
                reverse=True,
                noise_scale=self.noise_scale_duration,
            )
        else:
            log_duration = self.duration_predictor(hidden_states, input_padding_mask, speaker_embeddings)

        length_scale = 1.0 / self.speaking_rate
        duration = torch.ceil(torch.exp(log_duration) * input_padding_mask * length_scale)
        predicted_lengths = torch.clamp_min(torch.sum(duration, [1, 2]), 1).long()


        # Create a padding mask for the output lengths of shape (batch, 1, max_output_length)
        indices = torch.arange(predicted_lengths.max(), dtype=predicted_lengths.dtype, device=predicted_lengths.device)
        output_padding_mask = indices.unsqueeze(0) < predicted_lengths.unsqueeze(1)
        output_padding_mask = output_padding_mask.unsqueeze(1).to(input_padding_mask.dtype)

        # Reconstruct an attention tensor of shape (batch, 1, out_length, in_length)
        attn_mask = torch.unsqueeze(input_padding_mask, 2) * torch.unsqueeze(output_padding_mask, -1)
        batch_size, _, output_length, input_length = attn_mask.shape
        cum_duration = torch.cumsum(duration, -1).view(batch_size * input_length, 1)
        indices = torch.arange(output_length, dtype=duration.dtype, device=duration.device)
        valid_indices = indices.unsqueeze(0) < cum_duration
        valid_indices = valid_indices.to(attn_mask.dtype).view(batch_size, input_length, output_length)
        padded_indices = valid_indices - nn.functional.pad(valid_indices, [0, 0, 1, 0, 0, 0])[:, :-1]
        attn = padded_indices.unsqueeze(1).transpose(2, 3) * attn_mask

        # Expand prior distribution
        prior_means = torch.matmul(attn.squeeze(1), prior_means).transpose(1, 2)
        prior_log_variances = torch.matmul(attn.squeeze(1), prior_log_variances).transpose(1, 2)

        prior_latents = prior_means + torch.randn_like(prior_means) * torch.exp(prior_log_variances) * self.noise_scale
        latents = self.flow(prior_latents, output_padding_mask, speaker_embeddings, reverse=True)

        spectrogram = latents * output_padding_mask

        for i in range(0, spectrogram.size(-1), chunk_size):
            with torch.no_grad():
                wav=self.decoder(spectrogram[:,:,i : i + chunk_size] ,speaker_embeddings)
            yield wav.squeeze().cpu().numpy()



api_key = os.environ.get("Id_mode_vits") 
headers = {"Authorization": f"Bearer {api_key}"}

from transformers import AutoTokenizer,VitsModel
import torch
models= {}
tokenizer = AutoTokenizer.from_pretrained("wasmdashai/vits-ar-sa-huba",token=api_key)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def  get_model(name_model):
    global models
    if name_model in   models:
        return models[name_model]
    models[name_model]=VitsModel.from_pretrained(name_model,token=api_key).to(device)
    models[name_model].decoder.apply_weight_norm()
    # torch.nn.utils.weight_norm(self.decoder.conv_pre)
    # torch.nn.utils.weight_norm(self.decoder.conv_post)
    for flow in models[name_model].flow.flows:
        torch.nn.utils.weight_norm(flow.conv_pre)
        torch.nn.utils.weight_norm(flow.conv_post)
    return models[name_model]

def  genrate_speech(text,name_model):
    inputs=tokenizer(text,return_tensors="pt")
    model=get_model(name_model) 
    with torch.no_grad():
         wav=model(
             input_ids= inputs.input_ids.to(device),
             attention_mask=inputs.attention_mask.to(device),
             speaker_id=0
             ).waveform.cpu().numpy().reshape(-1)
    return model.config.sampling_rate,wav

def generate_audio(text,name_model,speaker_id=None):
    inputs = tokenizer(text, return_tensors="pt")#.input_ids

    speaker_embeddings = None
    model=get_model(name_model)
    #torch.cuda.empty_cache()
    with torch.no_grad():
        for chunk in _inference_forward_stream(model,input_ids=inputs.input_ids,attention_mask=inputs.attention_mask,speaker_embeddings= speaker_embeddings,chunk_size=256):
            yield  16000,chunk#.squeeze().cpu().numpy()#.astype(np.int16).tobytes()    
def generate_audio_ai(text,name_model):
    text_answer = get_answer_ai(text)
    text_answer = remove_extra_spaces(text_answer)
    inputs = tokenizer(text_answer, return_tensors="pt")#.input_ids

    speaker_embeddings = None
    model=get_model(name_model)
    #torch.cuda.empty_cache()
    with torch.no_grad():
        for chunk in _inference_forward_stream(model,input_ids=inputs.input_ids,attention_mask=inputs.attention_mask,speaker_embeddings= speaker_embeddings,chunk_size=256):
            yield  16000,remove_noise_nr(chunk)#.cpu().numpy().squeeze()#.astype(np.int16).tobytes()    

  
def remove_extra_spaces(text):

  return ' '.join(text.split())

def query(text,API_URL):
  payload={"inputs": text}
  response = requests.post(API_URL, headers=headers, json=payload)
  return response.content

def   get_answer_ai(text):
      global AI
      try:
          response = AI.send_message(text)
          return response.text

          
      except :
          AI=create_chat_session()
          response = AI.send_message(text)
          return response.text



def   get_answer_ai_stream(text):
      #if session_ai is  None:
      global AI
          
      try:
          
            response = AI.send_message(text,stream=True)
            return response

          
      except :
              AI=create_chat_session()
              response = AI.send_message(text,stream=True)
              return response


          
def t2t(text):
    
    return get_answer_ai(text)



def t2tstream(text):
    st=''
    response=get_answer_ai_stream(text)
    for chk in response:
         st+=chk.text
         yield st


    #return get_answer_ai(text)
import gradio as gr
import os
import plotly.express as px

# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.

def random_plot():
    df = px.data.iris()
    fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species",
                    size='petal_length', hover_data=['petal_width'])
    return fig

def print_like_dislike(x: gr.LikeData):
    print(x.index, x.value, x.liked)
from gradio_multimodalchatbot import MultimodalChatbot
from gradio.data_classes import FileData
import tempfile
import soundfile as sf
from gradio_client import Client
def add_message(history, message):
    for x in message["files"]:
        history.append(((x,), None))
    if message["text"] is not None:
        history.append((message["text"], None))
        response_audio = genrate_speech(message["text"],'wasmdashai/vits-ar-sa-huba')
        history.append((gr.Audio(response_audio,scale=1,streaming=True),None)) 
    return history

def bot(history,message):
    if message["text"] is not None:
       txt_ai=get_answer_ai(message["text"] )
       history[-1][1]=txt_ai#((None,txt_ai))
       response_audio = genrate_speech(txt_ai,'wasmdashai/vits-ar-sa-huba')
       history.append((None,gr.Audio(response_audio,scale=1,streaming=True))) 
    
    return history, gr.MultimodalTextbox(value=None, interactive=False)

fig = random_plot()


    

  # متغير لتخزين سجل المحادثة


with gr.Blocks() as demo:  # Use gr.Blocks to wrap the entire interface
    
    with gr.Tab("ChatBot "):
        chatbot = gr.Chatbot(
        elem_id="chatbot",
        bubble_full_width=False,
        scale=1,
               )

        chat_input = gr.MultimodalTextbox(interactive=True,
                                          file_count="single",
                                          placeholder="Enter message or upload file...", show_label=False,)
    
        chat_msg = chat_input.submit(add_message, [chatbot, chat_input], [chatbot])
        bot_msg = chat_msg.then(bot, [chatbot, chat_input], [chatbot, chat_input], api_name="bot_response")
        bot_msg.then(lambda: gr.MultimodalTextbox(interactive=True), None, [chat_input])
    
        chatbot.like(print_like_dislike, None, None)

        # audio.change(chatbot_fn, [txt, audio], chatbot)
        
    with gr.Tab("Chat AI "):
        gr.Markdown("## AI: محادثة صوتية بالذكاء الاصطناعي باللهجة السعودية")
        with gr.Row(): # Arrange input/output components side-by-side
            with gr.Column():
                text_input = gr.Textbox(label="أدخل أي نص")
                
                    
                with gr.Column():
                        model_choices = gr.Dropdown(
                            choices=[
                                "wasmdashai/vits-ar-sa",
                                "wasmdashai/vits-ar-sa-huba",
                                "wasmdashai/vits-ar-sa-ms",
                                "wasmdashai/vits-ar-sa-magd",
                                "wasmdashai/vits-ar-sa-fahd",
                            ],
                            label="اختر النموذج",
                            value="wasmdashai/vits-ar-sa-huba",
                        )
                
                with gr.Row():
                    btn = gr.Button("إرسال")
                    btn_ai_only = gr.Button("توليد رد الذكاء الاصطناعي فقط")
                with gr.Row():    
                    user_audio = gr.Audio(label="صوت المدخل")
                    ai_audio = gr.Audio(label="رد AI الصوتي")
                ai_text = gr.Textbox(label="رد AI النصي")
                ai_audio2 = gr.Audio(label="2رد AI الصوتي",streaming=True)

       

        # Use a single button to trigger both functionalities
        def process_audio(text, model_choice, generate_user_audio=True):
            API_URL = f"https://api-inference.huggingface.co/models/{model_choice}"
            text_answer = get_answer_ai(text)
            text_answer = remove_extra_spaces(text_answer)
            data_ai = genrate_speech(text_answer,model_choice)#query(text_answer, API_URL)
            if generate_user_audio:  # Generate user audio if needed
                data_user =genrate_speech(text,model_choice)# query(text, API_URL)
                return data_user, data_ai, text_answer
            else:
                return  data_ai  # Return None for user_audio
 
        btn.click(
            process_audio,  # Call the combined function
            inputs=[text_input, model_choices],
            outputs=[user_audio, ai_audio, ai_text],
        )

        # 
      
        btn_ai_only.click(
                generate_audio_ai,
                inputs=[text_input, model_choices],
                outputs=[ai_audio2],
            )
    with gr.Tab("Live "):
        gr.Markdown("## VITS: تحويل النص إلى كلام")
        
        with gr.Row():
            speaker_id_input = gr.Number(label="معرّف المتحدث (اختياري)", interactive=True)
            with gr.Column():
                        model_choices2 = gr.Dropdown(
                            choices=[
                                "wasmdashai/vits-ar-sa",
                                "wasmdashai/vits-ar-sa-huba",
                                "wasmdashai/vits-ar-sa-ms",
                                "wasmdashai/vits-ar-sa-A",
                                "wasmdashai/model-dash-fahd",
                            ],
                            label="اختر النموذج",
                            value="wasmdashai/vits-ar-sa-huba",
                        )
        text_input = gr.Textbox(label="أدخل النص هنا")
        generate_button = gr.Button("توليد وتشغيل الصوت")
      
        audio_player = gr.Audio(label="أ audio",streaming=True)
    
        # Update the event binding
        generate_button.click(generate_audio, inputs=[text_input,model_choices2], outputs=audio_player)

    with gr.Tab("T2T "):
        gr.Markdown("## T2T")
        text_inputk = gr.Textbox(label="أدخل النص هنا")
        text_out = gr.Textbox()
        text_inputk.submit(t2t, [text_inputk], [text_out])
    with gr.Tab("T2TSTREAM "):
        gr.Markdown("## T2TSTREAM ")
        text_inputk2 = gr.Textbox(label="أدخل النص هنا")
        text_out1 = gr.Textbox()
        text_inputk2.submit(t2tstream, [text_inputk2], [text_out1])
        
        
            
        
 
if __name__ == "__main__":
    demo.launch(show_error=True)