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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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import os
import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_io as tfio
import csv
from scipy.io import wavfile
import scipy
import librosa
import soundfile as sf
import time
import soundfile as sf
import gradio as gr

from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
from transformers import AutoProcessor
from transformers import BarkModel
from optimum.bettertransformer import BetterTransformer
import torch

from nemo.collections.tts.models import FastPitchModel

from nemo.collections.tts.models import HifiGanModel

from deep_translator import GoogleTranslator
from haystack.document_stores import InMemoryDocumentStore
from haystack.nodes import EmbeddingRetriever


# --- Load models ---

#Load a model from tensorflow hub
def load_model_hub(model_url):
    model = hub.load(model_url)
    return model

# Load a model from the project folder
def load_model_file(model_path):
    interpreter = tf.lite.Interpreter(model_path)
    interpreter.allocate_tensors()
    return interpreter

# --- Initialize models ---

def initialize_text_to_speech_model():
  spec_generator = FastPitchModel.from_pretrained("nvidia/tts_en_fastpitch")
  # Load vocoder
  model = HifiGanModel.from_pretrained(model_name="nvidia/tts_hifigan")
  return spec_generator, model


def initialize_tt5_model():
  from transformers import SpeechT5ForTextToSpeech, SpeechT5Processor, SpeechT5HifiGan
  from datasets import load_dataset

  dataset = load_dataset("pedropauletti/librispeech-portuguese")

  model = SpeechT5ForTextToSpeech.from_pretrained("pedropauletti/speecht5_finetuned_librispeech_pt")

  processor = SpeechT5Processor.from_pretrained("microsoft/speecht5_tts")

  vocoder = SpeechT5HifiGan.from_pretrained("microsoft/speecht5_hifigan")

  example = dataset["test"][100]
  speaker_embeddings = torch.tensor(example["speaker_embeddings"]).unsqueeze(0)

  return model, processor, vocoder, speaker_embeddings


def load_qa_model():
  document_store = InMemoryDocumentStore()
  retriever = EmbeddingRetriever(
    document_store=document_store,
    embedding_model="sentence-transformers/all-MiniLM-L6-v2",
    use_gpu=False,
    scale_score=False,
  )
  # Get dataframe with columns "question", "answer" and some custom metadata
  df = pd.read_csv('content/social-faq.csv', on_bad_lines='skip', delimiter=';')
  # Minimal cleaning
  df.fillna(value="", inplace=True)
  df["question"] = df["question"].apply(lambda x: x.strip())

  questions = list(df["question"].values)
  df["embedding"] = retriever.embed_queries(queries=questions).tolist()
  df = df.rename(columns={"question": "content"})

  # Convert Dataframe to list of dicts and index them in our DocumentStore
  docs_to_index = df.to_dict(orient="records")
  document_store.write_documents(docs_to_index)

  return retriever


# --- Audio pre-processing ---

# Utility functions for loading audio files and making sure the sample rate is correct.
@tf.function
def load_wav_16k_mono(filename):
    """ Load a WAV file, convert it to a float tensor, resample to 16 kHz single-channel audio. """
    file_contents = tf.io.read_file(filename)
    wav, sample_rate = tf.audio.decode_wav(
          file_contents,
          desired_channels=1)
    wav = tf.squeeze(wav, axis=-1)
    sample_rate = tf.cast(sample_rate, dtype=tf.int64)
    wav = tfio.audio.resample(wav, rate_in=sample_rate, rate_out=16000)
    return wav


def load_wav_16k_mono_librosa(filename):
    """ Load a WAV file, convert it to a float tensor, resample to 16 kHz single-channel audio using librosa. """
    wav, sample_rate = librosa.load(filename, sr=16000, mono=True)
    return wav


def load_wav_16k_mono_soundfile(filename):
    """ Load a WAV file, convert it to a float tensor, resample to 16 kHz single-channel audio using soundfile. """
    wav, sample_rate = sf.read(filename, dtype='float32')
    # Resample to 16 kHz if necessary
    if sample_rate != 16000:
        wav = librosa.resample(wav, orig_sr=sample_rate, target_sr=16000)
    return wav


# --- History ---
def updateHistory():
  global history
  return history

def clearHistory():
  global history
  history = ""
  return history

def clear():
  return None

# --- Output Format ---

def format_dictionary(dictionary):
    result = []
    for key, value in dictionary.items():
        percentage = int(value * 100)
        result.append(f"{key}: {percentage}%")
    return ', '.join(result)


def format_json(json_data):
    confidence_strings = [f"{item['label']}: {round(item['confidence']*100)}%" for item in json_data['confidences']]
    result_string = f"{', '.join(confidence_strings)}"
    return result_string

def format_json_pt(json_data):
    from unidecode import unidecode
    confidence_strings = [f"{item['label']}... " for item in json_data['confidences']]
    result_string = f"{', '.join(confidence_strings)}"
    return unidecode(result_string)


# --- Classification ---

def load_label_mapping(csv_path):
    label_mapping = {}
    with open(csv_path, newline='', encoding='utf-8') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            label_mapping[int(row['index'])] = row['display_name']
    return label_mapping


def predict_yamnet(interpreter, waveform, input_details, output_details, label_mapping):
    # Pré-processamento da waveform para corresponder aos requisitos do modelo
    input_shape = input_details[0]['shape']
    input_data = np.array(waveform, dtype=np.float32)

    if input_data.shape != input_shape:
        # Redimensionar ou preencher a waveform para corresponder ao tamanho esperado
        if input_data.shape[0] < input_shape[0]:
            # Preencher a waveform com zeros
            padding = np.zeros((input_shape[0] - input_data.shape[0],))
            input_data = np.concatenate((input_data, padding))
        elif input_data.shape[0] > input_shape[0]:
            # Redimensionar a waveform
            input_data = input_data[:input_shape[0]]

    input_data = np.reshape(input_data, input_shape)

    # Executar a inferência
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()

    # Obter os resultados da inferência
    output_data = interpreter.get_tensor(output_details[0]['index'])

    # Processar os resultados e imprimir nome da etiqueta
    top_labels_indices = np.argsort(output_data[0])[::-1][:3]
    results = []
    for i in top_labels_indices:
        label_name = label_mapping.get(i, "Unknown Label")
        probability = float(output_data[0][i])  # Converter para float
        results.append({'label': label_name, 'probability': str(probability)})

    return results  # Retornar um dicionário contendo a lista de resultados


def classify(audio, language="en-us"):
    #Preprocessing audio
    wav_data = load_wav_16k_mono_librosa(audio)

    if(language == "pt-br"):
      #Label Mapping
      label_mapping = load_label_mapping('content/yamnet_class_map_ptbr.csv')
    else:
      label_mapping = load_label_mapping('content/yamnet_class_map.csv')

    #Load Model by File
    model = load_model_file('content/yamnet_classification.tflite')
    input_details = model.get_input_details()
    output_details = model.get_output_details()

    #Classification
    result = predict_yamnet(model, wav_data, input_details, output_details, label_mapping)

    return result

def classify_realtime(language, audio, state):
    #Preprocessing audio
    wav_data = load_wav_16k_mono_librosa(audio)

    if(language == "pt-br"):
      #Label Mapping
      label_mapping = load_label_mapping('content/yamnet_class_map_ptbr.csv')
    else:
      label_mapping = load_label_mapping('content/yamnet_class_map.csv')

    #Load Model by File
    model = load_model_file('content/yamnet_classification.tflite')
    input_details = model.get_input_details()
    output_details = model.get_output_details()

    #Classification
    result = predict_yamnet(model, wav_data, input_details, output_details, label_mapping)

    state += result + "  "

    return result, state


# --- TTS ---

def generate_audio(spec_generator, model, input_text):
  parsed = spec_generator.parse(input_text)
  spectrogram = spec_generator.generate_spectrogram(tokens=parsed)
  audio = model.convert_spectrogram_to_audio(spec=spectrogram)
  return 22050, audio.cpu().detach().numpy().squeeze()


def generate_audio_tt5(model, processor, vocoder, speaker_embeddings, text):
  inputs = processor(text=text, return_tensors="pt")
  audio = model.generate_speech(inputs["input_ids"], speaker_embeddings, vocoder=vocoder)
  return 16000, audio.cpu().detach().numpy().squeeze()



def TTS(json_input, language):
    global spec_generator, model_nvidia, history
    global model_tt5, processor, vocoder, speaker_embeddings

    if language == 'en-us':
      sr, generatedAudio = generate_audio(spec_generator, model_nvidia, format_json(json_input))
    else:
      sr, generatedAudio = generate_audio_tt5(model_tt5, processor, vocoder, speaker_embeddings, format_json_pt(json_input))

    return (sr, generatedAudio)


def TTS_ASR(json_input, language):
    global spec_generator, model_nvidia, history
    global model_tt5, processor, vocoder, speaker_embeddings

    if language == 'en-us':
      sr, generatedAudio = generate_audio(spec_generator, model_nvidia, json_input['label'])
    else:
      sr, generatedAudio = generate_audio_tt5(model_tt5, processor, vocoder, speaker_embeddings, json_input['label'])

    return (sr, generatedAudio)


def TTS_chatbot(language):
    global spec_generator, model_nvidia, history
    global model_tt5, processor, vocoder, speaker_embeddings
    global last_answer

    if language == 'en-us':
      sr, generatedAudio = generate_audio(spec_generator, model_nvidia, last_answer)
    else:
      sr, generatedAudio = generate_audio_tt5(model_tt5, processor, vocoder, speaker_embeddings, last_answer)

    return (sr, generatedAudio)

# --- ASR ---

def transcribe_speech(filepath, language):
    print(filepath)
    if(language == "pt-br"):
        output = pipe(
        filepath,
        max_new_tokens=256,
        generate_kwargs={
            "task": "transcribe",
            "language": "portuguese",
        },
        chunk_length_s=30,
        batch_size=8,
    )
    else:
        output = pipe_en(
            filepath,
            max_new_tokens=256,
            generate_kwargs={
                "task": "transcribe",
                "language": "english",
            },
            chunk_length_s=30,
            batch_size=8,
        )


    return output["text"]


def transcribe_speech_realtime(filepath, state):
    output = pipe(
        filepath,
        max_new_tokens=256,
        generate_kwargs={
            "task": "transcribe",
            "language": "english",
        },
        chunk_length_s=30,
        batch_size=8,
    )
    state += output["text"] + " "
    return output["text"], state


def transcribe_realtime(new_chunk, stream):
    sr, y = new_chunk
    y = y.astype(np.float32)
    y /= np.max(np.abs(y))

    if stream is not None:
        stream = np.concatenate([stream, y])
    else:
        stream = y
    return stream, pipe_en({"sampling_rate": sr, "raw": stream})["text"]


# --- Translation ---

def translate_enpt(text):
  global enpt_pipeline
  translation = enpt_pipeline(f"translate English to Portuguese: {text}")
  return translation[0]['generated_text']


# --- Gradio Interface ---

def interface(language, audio):
    global classificationResult
    result = classify(language, audio)
    dic =  {result[0]['label']: float(result[0]['probability']),
            result[1]['label']: float(result[1]['probability']),
            result[2]['label']: float(result[2]['probability'])
            }
    # history += result[0]['label'] + '\n'
    classificationResult = dic

    return dic

def interface_realtime(language, audio):
    global history
    result = classify(language, audio)
    dic =  {result[0]['label']: float(result[0]['probability']),
            result[1]['label']: float(result[1]['probability']),
            result[2]['label']: float(result[2]['probability'])
            }
    history = result[0]['label'] + '\n' + history
    return dic


    
# --- QA Model ---

def get_answers(retriever, query):
  from haystack.pipelines import FAQPipeline

  pipe = FAQPipeline(retriever=retriever)

  from haystack.utils import print_answers

  # Run any question and change top_k to see more or less answers
  prediction = pipe.run(query=query, params={"Retriever": {"top_k": 1}})

  answers = prediction['answers']

  if answers:
    return answers[0].answer
  else:
    return "I don't have an answer to that question"


def add_text(chat_history, text):
    chat_history = chat_history + [(text, None)]
    return chat_history, gr.Textbox(value="", interactive=False)


def chatbot_response(chat_history, language):
    
    chat_history[-1][1] = ""

    global retriever
    global last_answer

    if language == 'pt-br':
      response = get_answers(retriever, GoogleTranslator(source='pt', target='en').translate(chat_history[-1][0]))
      response = GoogleTranslator(source='en', target='pt').translate(response)
    else:
      response = get_answers(retriever, chat_history[-1][0])

    last_answer = response

    for character in response:
      chat_history[-1][1] += character
      time.sleep(0.01)
      yield chat_history


        
retriever = load_qa_model()
spec_generator, model_nvidia = initialize_text_to_speech_model()
model_tt5, processor, vocoder, speaker_embeddings = initialize_tt5_model()
pipe = pipeline("automatic-speech-recognition", model="pedropauletti/whisper-small-pt")
pipe_en = pipeline("automatic-speech-recognition", model="openai/whisper-small")