Spaces:
Sleeping
Sleeping
cryptocalypse
commited on
Commit
•
284c1ac
1
Parent(s):
103c053
libs entropy and read files
Browse files- app.py +1 -0
- lib/pipes.py +102 -0
app.py
CHANGED
@@ -111,6 +111,7 @@ with gr.Blocks(title="Sophia, Torah Codes",css=css,js=js) as app:
|
|
111 |
retry_btn=None,
|
112 |
undo_btn="Undo",
|
113 |
clear_btn="Clear",
|
|
|
114 |
)
|
115 |
|
116 |
#with gr.Tab("Chat"):
|
|
|
111 |
retry_btn=None,
|
112 |
undo_btn="Undo",
|
113 |
clear_btn="Clear",
|
114 |
+
examples=["I want you to interpret a dream where I travel to space and see the earth in small size, then a fireball comes for me and I teleport to another planet full of fruits, trees and forests, there I meet a witch who makes me drink a potion and then I wake up"]
|
115 |
)
|
116 |
|
117 |
#with gr.Tab("Chat"):
|
lib/pipes.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline
|
2 |
+
from diffusers import DiffusionPipeline
|
3 |
+
from transformers import AutoModelForSeq2SeqLM
|
4 |
+
from samplings import top_p_sampling, temperature_sampling
|
5 |
+
import torch
|
6 |
+
|
7 |
+
class AIAssistant:
|
8 |
+
def __init__(self):
|
9 |
+
pass
|
10 |
+
|
11 |
+
def entity_pos_tagger(self, example):
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained("Davlan/bert-base-multilingual-cased-ner-hrl")
|
13 |
+
model = AutoModelForTokenClassification.from_pretrained("Davlan/bert-base-multilingual-cased-ner-hrl")
|
14 |
+
nlp = pipeline("ner", model=model, tokenizer=tokenizer)
|
15 |
+
ner_results = nlp(example)
|
16 |
+
return ner_results
|
17 |
+
|
18 |
+
def text_to_image_generation(self, prompt, n_steps=40, high_noise_frac=0.8):
|
19 |
+
base = DiffusionPipeline.from_pretrained(
|
20 |
+
"stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
|
21 |
+
)
|
22 |
+
base.to("cuda")
|
23 |
+
refiner = DiffusionPipeline.from_pretrained(
|
24 |
+
"stabilityai/stable-diffusion-xl-refiner-1.0",
|
25 |
+
text_encoder_2=base.text_encoder_2,
|
26 |
+
vae=base.vae,
|
27 |
+
torch_dtype=torch.float16,
|
28 |
+
use_safetensors=True,
|
29 |
+
variant="fp16",
|
30 |
+
)
|
31 |
+
refiner.to("cuda")
|
32 |
+
|
33 |
+
image = base(
|
34 |
+
prompt=prompt,
|
35 |
+
num_inference_steps=n_steps,
|
36 |
+
denoising_end=high_noise_frac,
|
37 |
+
output_type="latent",
|
38 |
+
).images
|
39 |
+
image = refiner(
|
40 |
+
prompt=prompt,
|
41 |
+
num_inference_steps=n_steps,
|
42 |
+
denoising_start=high_noise_frac,
|
43 |
+
image=image,
|
44 |
+
).images[0]
|
45 |
+
return image
|
46 |
+
|
47 |
+
def grammatical_pos_tagger(self, text):
|
48 |
+
nlp_pos = pipeline(
|
49 |
+
"ner",
|
50 |
+
model="mrm8488/bert-spanish-cased-finetuned-pos",
|
51 |
+
tokenizer=(
|
52 |
+
'mrm8488/bert-spanish-cased-finetuned-pos',
|
53 |
+
{"use_fast": False}
|
54 |
+
))
|
55 |
+
return nlp_pos(text)
|
56 |
+
|
57 |
+
def text_to_music(self, text, max_length=1024, top_p=0.9, temperature=1.0):
|
58 |
+
tokenizer = AutoTokenizer.from_pretrained('sander-wood/text-to-music')
|
59 |
+
model = AutoModelForSeq2SeqLM.from_pretrained('sander-wood/text-to-music')
|
60 |
+
|
61 |
+
input_ids = tokenizer(text,
|
62 |
+
return_tensors='pt',
|
63 |
+
truncation=True,
|
64 |
+
max_length=max_length)['input_ids']
|
65 |
+
|
66 |
+
decoder_start_token_id = model.config.decoder_start_token_id
|
67 |
+
eos_token_id = model.config.eos_token_id
|
68 |
+
|
69 |
+
decoder_input_ids = torch.tensor([[decoder_start_token_id]])
|
70 |
+
|
71 |
+
for t_idx in range(max_length):
|
72 |
+
outputs = model(input_ids=input_ids,
|
73 |
+
decoder_input_ids=decoder_input_ids)
|
74 |
+
probs = outputs.logits[0][-1]
|
75 |
+
probs = torch.nn.Softmax(dim=-1)(probs).detach().numpy()
|
76 |
+
sampled_id = temperature_sampling(probs=top_p_sampling(probs,
|
77 |
+
top_p=top_p,
|
78 |
+
return_probs=True),
|
79 |
+
temperature=temperature)
|
80 |
+
decoder_input_ids = torch.cat((decoder_input_ids, torch.tensor([[sampled_id]])), 1)
|
81 |
+
if sampled_id!=eos_token_id:
|
82 |
+
continue
|
83 |
+
else:
|
84 |
+
tune = "X:1\n"
|
85 |
+
tune += tokenizer.decode(decoder_input_ids[0], skip_special_tokens=True)
|
86 |
+
return tune
|
87 |
+
break
|
88 |
+
|
89 |
+
# Ejemplo de uso
|
90 |
+
assistant = AIAssistant()
|
91 |
+
ner_results = assistant.entity_pos_tagger("Nader Jokhadar had given Syria the lead with a well-struck header in the seventh minute.")
|
92 |
+
print(ner_results)
|
93 |
+
|
94 |
+
image = assistant.text_to_image_generation("A majestic lion jumping from a big stone at night")
|
95 |
+
print(image)
|
96 |
+
|
97 |
+
pos_tags = assistant.grammatical_pos_tagger('Mis amigos están pensando en viajar a Londres este verano')
|
98 |
+
print(pos_tags)
|
99 |
+
|
100 |
+
tune = assistant.text_to_music("This is a traditional Irish dance music.")
|
101 |
+
print(tune)
|
102 |
+
|