-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext_analyze_test.py
103 lines (73 loc) · 6.39 KB
/
text_analyze_test.py
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
# produces json files of analyzed text.
#from server import analyze
#import torch
#from server import get_all_projects
# projects = {}
# print(str(torch.cuda.is_available()) + 'text analyze test')
#req = {
# "project": "new",
# "text": "The following is a transcript from The Guardian."
#}
#projects = get_all_projects()
#ret = analyze(req)
#print(ret)
import json
from backend import api
def remove_symbols_from_text(text):
# given string, removes symbols
for char in text:
if char in "“”":
text = text.replace(char, '"')
if char in "‘’":
text = text.replace(char, '\'')
if char in "–":
text = text.replace(char, '-')
return text
'''raw_text = """
In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.
The scientist named the population, after their distinctive horn, Ovid's Unicorn. These four-horned, silver-white unicorns were previously unknown to science.
Now, after almost two centuries, the mystery of what sparked this odd phenomenon is finally solved.––
Dr. Jorge Pérez, an evolutionary biologist from the University of La Paz, and several companions, were exploring the Andes Mountains when they found a small valley, with no other animals or humans. Pérez noticed that the valley had what appeared to be a natural fountain, surrounded by two peaks of rock and silver snow.
Pérez and the others then ventured further into the valley. "By the time we reached the top of one peak, the water looked blue, with some crystals on top," said Pérez.
Pérez and his friends were astonished to see the unicorn herd. These creatures could be seen from the air without having to move too much to see them - they were so close they could touch their horns.
While examining these bizarre creatures the scientists discovered that the creatures also spoke some fairly regular English. Pérez stated, "We can see, for example, that they have a common 'language,' something like a dialect or dialectic."
Dr. Pérez believes that the unicorns may have originated in Argentina, where the animals were believed to be descendants of a lost race of people who lived there before the arrival of humans in those parts of South America.
While their origins are still unclear, some believe that perhaps the creatures were created when a human and a unicorn met each other in a time before human civilization. According to Pérez, "In South America, such incidents seem to be quite common."
However, Pérez also pointed out that it is likely that the only way of knowing for sure if unicorns are indeed the descendants of a lost alien race is through DNA. "But they seem to be able to communicate in English quite well, which I believe is a sign of evolution, or at least a change in social organization," said the scientist.
""" '''
# raw_text = """
# In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.
# """
raw_text= "In a shocking finding, scientist discovered a herd of unicorns living in a remote, previously unexplored valley, in the Andes Mountains. Even more surprising to the researchers was the fact that the unicorns spoke perfect English.\nThe scientist named the population, after their distinctive horn, Ovid's Unicorn. These four-horned, silver-white unicorns were previously unknown to science.\n\nNow, after almost two centuries, the mystery of what sparked this odd phenomenon is finally solved.\n\nDr. Jorge Pérez, an evolutionary biologist from the University of La Paz, and several companions, were exploring the Andes Mountains when they found a small valley, with no other animals or humans. Pérez noticed that the valley had what appeared to be a natural fountain, surrounded by two peaks of rock and silver snow.\n\nPérez and the others then ventured further into the valley. \"By the time we reached the top of one peak, the water looked blue, with some crystals on top,\" said Pérez.\n\nPérez and his friends were astonished to see the unicorn herd. These creatures could be seen from the air without having to move too much to see them – they were so close they could touch their horns.\n\nWhile examining these bizarre creatures the scientists discovered that the creatures also spoke some fairly regular English. Pérez stated, \"We can see, for example, that they have a common 'language,' something like a dialect or dialectic.\"\n\nDr. Pérez believes that the unicorns may have originated in Argentina, where the animals were believed to be descendants of a lost race of people who lived there before the arrival of humans in those parts of South America.\n\nWhile their origins are still unclear, some believe that perhaps the creatures were created when a human and a unicorn met each other in a time before human civilization. According to Pérez, \"In South America, such incidents seem to be quite common.\"\n\nHowever, Pérez also pointed out that it is likely that the only way of knowing for sure if unicorns are indeed the descendants of a lost alien race is through DNA. \"But they seem to be able to communicate in English quite well, which I believe is a sign of evolution, or at least a change in social organization,\" said the scientist."
'''
Tests for BERT
'''
"""lm = api.BERTLM()
start = api.time.time()
payload_bert = lm.check_probabilities(raw_text, topk=5)
end = api.time.time()
print("{:.2f} Seconds for a run with BERT".format(end - start))"""
# print("SAMPLE:", sample)
'''
Tests for GPT-2
'''
raw_text = remove_symbols_from_text(raw_text)
lm = api.LM()
start = api.time.time()
payload = lm.check_probabilities(raw_text, topk=20)
end = api.time.time()
print("{:.2f} Seconds for a check with GPT-2".format(end - start))
'''for item in payload["pred_topk"]:
print(lm.postprocess(item[0][0]))'''
"""start = api.time.time()
sample = lm.sample_unconditional()
end = api.time.time()
print("{:.2f} Seconds for a sample from GPT-2".format(end - start))"""
# print("SAMPLE:", sample)
res = {
"request": {'project': "new", 'text': raw_text},
"result": payload
}
print(res)
#with open('test_json.json', 'w') as outfile:
# json.dump(res, outfile)