torahCodes/Torah_Codes
4
1from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline2from diffusers import DiffusionPipeline3from transformers import AutoModelForSeq2SeqLM4from samplings import top_p_sampling, temperature_sampling5import torch6from sentence_transformers import SentenceTransformer, util7from datasets import load_dataset8import soundfile as sf9import unicodedata10import itertools11 12 13class AIAssistant:14 def __init__(self):15 pass16 17 18 ## generate regexp for search over memory19 def gen_search_expr(self,palabras_unidas):20 21 combinaciones = []22 23 for i in range(1, len(palabras_unidas) + 1):24 for combinacion in itertools.combinations(palabras_unidas, i):25 regex = ".*?".join(combinacion)26 combinaciones.append(regex)27 28 return combinaciones29 30 ## join taggued tokens into words31 def process_list(self,lista):32 palabras_unidas = []33 palabra_actual = ""34 35 for token in lista:36 if token.startswith("##"):37 palabra_actual += token[2:]38 else:39 if palabra_actual:40 palabras_unidas.append(palabra_actual)41 palabra_actual = ""42 palabra_actual += token43 44 if palabra_actual:45 palabras_unidas.append(palabra_actual)46 47 return [unicodedata.normalize("NFKD", palabra).encode("ASCII", "ignore").decode("ASCII").lower() for palabra in palabras_unidas]48 49 50 ## gramatical classificator51 def grammatical_pos_tagger(self, text):52 nlp_pos = pipeline("token-classification", model="QCRI/bert-base-multilingual-cased-pos-english", tokenizer="QCRI/bert-base-multilingual-cased-pos-english")53 res = nlp_pos(text)54 return res55 56 57 ## entity classifier58 def entity_pos_tagger(self, txt):59 tokenizer = AutoTokenizer.from_pretrained("Davlan/bert-base-multilingual-cased-ner-hrl")60 model = AutoModelForTokenClassification.from_pretrained("Davlan/bert-base-multilingual-cased-ner-hrl")61 nlp = pipeline("ner", model=model, tokenizer=tokenizer)62 ner_results = nlp(txt)63 return ner_results64 65 66 ## sentiment analysis67 def sentiment_tags(self,text):68 distilled_student_sentiment_classifier = pipeline(69 model="lxyuan/distilbert-base-multilingual-cased-sentiments-student", 70 return_all_scores=True71 )72 73 # english74 return distilled_student_sentiment_classifier(text)75 76 ## check similarity among sentences (group of tokens (words))77 def similarity_tag(self, sentenceA,sentenceB):78 res=[]79 model = SentenceTransformer('abbasgolestani/ag-nli-bert-mpnet-base-uncased-sentence-similarity-v1') 80 81 # Two lists of sentences82 #sentences1 = ['I am honored to be given the opportunity to help make our company better',83 # 'I love my job and what I do here',84 # 'I am excited about our company’s vision']85 86 #sentences2 = ['I am hopeful about the future of our company',87 # 'My work is aligning with my passion',88 # 'Definitely our company vision will be the next breakthrough to change the world and I’m so happy and proud to work here']89 90 sentences1 = sentenceA91 sentences2 = sentenceB92 #Compute embedding for both lists93 embeddings1 = model.encode(sentences1, convert_to_tensor=True)94 embeddings2 = model.encode(sentences2, convert_to_tensor=True)95 96 #Compute cosine-similarities97 cosine_scores = util.cos_sim(embeddings1, embeddings2)98 99 #Output the pairs with their score100 for i in range(len(sentences1)):101 try:102 res.append({"A": sentences1[i], "B":sentences2[i], "score":cosine_scores[i][i]})103 except:104 pass105 106 #print("{} \t\t {} \t\t Score: {:.4f}".format(sentences1[i], sentences2[i], cosine_scores[i][i]))107 108 return res109 110 111 112 ## text to speech113 def texto_to_speech(self,txt): 114 synthesiser = pipeline("text-to-speech", "microsoft/speecht5_tts")115 116 embeddings_dataset = load_dataset("Matthijs/cmu-arctic-xvectors", split="validation")117 speaker_embedding = torch.tensor(embeddings_dataset[7306]["xvector"]).unsqueeze(0)118 # You can replace this embedding with your own as well.119 120 speech = synthesiser(txt, forward_params={"speaker_embeddings": speaker_embedding})121 sf.write("speech.wav", speech["audio"], samplerate=speech["sampling_rate"])122 123 return speech 124 ## text to stable difusor generated image125 def text_to_image_generation(self, prompt, n_steps=40, high_noise_frac=0.8):126 base = DiffusionPipeline.from_pretrained(127 "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True128 )129 base.to("cuda")130 refiner = DiffusionPipeline.from_pretrained(131 "stabilityai/stable-diffusion-xl-refiner-1.0",132 text_encoder_2=base.text_encoder_2,133 vae=base.vae,134 torch_dtype=torch.float16,135 use_safetensors=True,136 variant="fp16",137 )138 refiner.to("cuda")139 140 image = base(141 prompt=prompt,142 num_inference_steps=n_steps,143 denoising_end=high_noise_frac,144 output_type="latent",145 ).images146 image = refiner(147 prompt=prompt,148 num_inference_steps=n_steps,149 denoising_start=high_noise_frac,150 image=image,151 ).images[0]152 return image153 154 155 ## pass text prompt to music156 def text_to_music(self, text, max_length=1024, top_p=0.9, temperature=1.0):157 tokenizer = AutoTokenizer.from_pretrained('sander-wood/text-to-music')158 model = AutoModelForSeq2SeqLM.from_pretrained('sander-wood/text-to-music')159 160 input_ids = tokenizer(text,161 return_tensors='pt',162 truncation=True,163 max_length=max_length)['input_ids']164 165 decoder_start_token_id = model.config.decoder_start_token_id166 eos_token_id = model.config.eos_token_id167 168 decoder_input_ids = torch.tensor([[decoder_start_token_id]])169 170 for t_idx in range(max_length):171 outputs = model(input_ids=input_ids,172 decoder_input_ids=decoder_input_ids)173 probs = outputs.logits[0][-1]174 probs = torch.nn.Softmax(dim=-1)(probs).detach().numpy()175 sampled_id = temperature_sampling(probs=top_p_sampling(probs,176 top_p=top_p,177 return_probs=True),178 temperature=temperature)179 decoder_input_ids = torch.cat((decoder_input_ids, torch.tensor([[sampled_id]])), 1)180 if sampled_id!=eos_token_id:181 continue182 else:183 tune = "X:1\n"184 tune += tokenizer.decode(decoder_input_ids[0], skip_special_tokens=True)185 return tune186 break187 188 189if __name__ == "__main__":190 191 # Ejemplo de uso192 assistant = AIAssistant()193 ner_results = assistant.entity_pos_tagger("Nader Jokhadar had given Syria the lead with a well-struck header in the seventh minute.")194 print(ner_results)195 196 image = assistant.text_to_image_generation("A majestic lion jumping from a big stone at night")197 print(image)198 199 pos_tags = assistant.grammatical_pos_tagger('Mis amigos están pensando en viajar a Londres este verano')200 print(pos_tags)201 202 tune = assistant.text_to_music("This is a traditional Irish dance music.")203 print(tune)204 205 