Spaces:
Runtime error
Runtime error
Commit
·
2c5a895
1
Parent(s):
34d3a49
Update main.py
Browse files
main.py
CHANGED
@@ -1,3 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from countriesIdentification import identify_locations
|
2 |
from datesIdentification import dates_binding
|
3 |
from magnitudeIdentification import magnitude_binding
|
@@ -73,65 +88,68 @@ def process_final_dict(final_dictionary):
|
|
73 |
|
74 |
|
75 |
def natural_language_module(sentence):
|
76 |
-
|
77 |
-
|
|
|
78 |
try:
|
79 |
final_dictionary = []
|
80 |
-
|
81 |
# identify whether the sentence is referred on earthquake events
|
82 |
earth_event = identify_earthquake_event(sentence)
|
83 |
-
|
84 |
if earth_event:
|
85 |
final_dictionary.append(earth_event)
|
86 |
-
|
87 |
# identify the target country and city in the sentence
|
88 |
location = identify_locations(sentence)
|
89 |
-
|
90 |
if location:
|
91 |
final_dictionary.append(location)
|
92 |
-
|
93 |
# identify the target comparative in the sentence
|
94 |
comparative = comparatives_binding(sentence)
|
95 |
-
|
96 |
if comparative:
|
97 |
final_dictionary.append(comparative)
|
98 |
-
|
|
|
|
|
99 |
# identify the target date in the sentence
|
100 |
date = dates_binding(sentence)
|
101 |
-
|
102 |
if isinstance(date, list):
|
103 |
-
|
104 |
date_dict = date[0]
|
105 |
date_replc = date[1]
|
106 |
-
|
107 |
if date_dict:
|
108 |
final_dictionary.append(date_dict[0])
|
109 |
-
|
110 |
# we also delete the date reference from the sentence so that there will
|
111 |
# not be any confusion with it for the magnitude identification module
|
112 |
if len(date_replc) == 1:
|
113 |
sentence = sentence.replace(date_replc[0], " ")
|
114 |
-
|
115 |
# in case it is a tuple we add it as it is and we do not substitute something in the sentence
|
116 |
elif isinstance(date, tuple):
|
117 |
final_dictionary.append(date)
|
118 |
-
|
119 |
# identify the target magnitude number in the sentence
|
120 |
magnitude = magnitude_binding(sentence)
|
121 |
-
|
122 |
if magnitude:
|
123 |
final_dictionary.append(magnitude)
|
124 |
-
|
125 |
clean_final_dictionary = process_final_dict(final_dictionary)
|
126 |
-
|
127 |
result = {}
|
128 |
for d in clean_final_dictionary:
|
129 |
result.update(d)
|
130 |
-
|
131 |
return result
|
132 |
|
133 |
except:
|
134 |
-
|
135 |
|
136 |
|
137 |
|
@@ -154,12 +172,55 @@ def process_json_sf(nl_json, sentence):
|
|
154 |
|
155 |
|
156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
157 |
def main(sentence):
|
158 |
"""
|
159 |
Function to bind together all the info and be executed
|
160 |
"""
|
161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
nl_data = natural_language_module(sentence)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
163 |
nl_json = process_json_sf(nl_data, sentence)
|
164 |
|
165 |
return nl_json
|
|
|
1 |
+
import time
|
2 |
+
|
3 |
+
print("\n\n ==== THE NATURAL LANGUAGE MODULE IS BEING LOADED. PLEASE WAIT ==== \n\n")
|
4 |
+
start_time_load = time.time()
|
5 |
+
|
6 |
+
from transformers import logging
|
7 |
+
logging.set_verbosity_error()
|
8 |
+
|
9 |
+
import warnings
|
10 |
+
warnings.filterwarnings("ignore", category=UserWarning)
|
11 |
+
|
12 |
+
import sys
|
13 |
+
|
14 |
+
import requests
|
15 |
+
|
16 |
from countriesIdentification import identify_locations
|
17 |
from datesIdentification import dates_binding
|
18 |
from magnitudeIdentification import magnitude_binding
|
|
|
88 |
|
89 |
|
90 |
def natural_language_module(sentence):
|
91 |
+
"""
|
92 |
+
Function to execute the complete natural language module pipeline
|
93 |
+
"""
|
94 |
try:
|
95 |
final_dictionary = []
|
96 |
+
|
97 |
# identify whether the sentence is referred on earthquake events
|
98 |
earth_event = identify_earthquake_event(sentence)
|
99 |
+
|
100 |
if earth_event:
|
101 |
final_dictionary.append(earth_event)
|
102 |
+
|
103 |
# identify the target country and city in the sentence
|
104 |
location = identify_locations(sentence)
|
105 |
+
|
106 |
if location:
|
107 |
final_dictionary.append(location)
|
108 |
+
|
109 |
# identify the target comparative in the sentence
|
110 |
comparative = comparatives_binding(sentence)
|
111 |
+
|
112 |
if comparative:
|
113 |
final_dictionary.append(comparative)
|
114 |
+
|
115 |
+
print(final_dictionary)
|
116 |
+
|
117 |
# identify the target date in the sentence
|
118 |
date = dates_binding(sentence)
|
119 |
+
|
120 |
if isinstance(date, list):
|
121 |
+
|
122 |
date_dict = date[0]
|
123 |
date_replc = date[1]
|
124 |
+
|
125 |
if date_dict:
|
126 |
final_dictionary.append(date_dict[0])
|
127 |
+
|
128 |
# we also delete the date reference from the sentence so that there will
|
129 |
# not be any confusion with it for the magnitude identification module
|
130 |
if len(date_replc) == 1:
|
131 |
sentence = sentence.replace(date_replc[0], " ")
|
132 |
+
|
133 |
# in case it is a tuple we add it as it is and we do not substitute something in the sentence
|
134 |
elif isinstance(date, tuple):
|
135 |
final_dictionary.append(date)
|
136 |
+
|
137 |
# identify the target magnitude number in the sentence
|
138 |
magnitude = magnitude_binding(sentence)
|
139 |
+
|
140 |
if magnitude:
|
141 |
final_dictionary.append(magnitude)
|
142 |
+
|
143 |
clean_final_dictionary = process_final_dict(final_dictionary)
|
144 |
+
|
145 |
result = {}
|
146 |
for d in clean_final_dictionary:
|
147 |
result.update(d)
|
148 |
+
|
149 |
return result
|
150 |
|
151 |
except:
|
152 |
+
return "\n\n=== AN UNEXPECTED ERROR HAS OCCURED. PLEASE EXECUTE AGAIN THE SCRIPT OR COMMUNICATE WITH THE DEVELOPER TEAM === \n\n"
|
153 |
|
154 |
|
155 |
|
|
|
172 |
|
173 |
|
174 |
|
175 |
+
def send_json_to_endpoint(json_output, username, password):
|
176 |
+
"""
|
177 |
+
Function to send the produced json to a target endpoint
|
178 |
+
"""
|
179 |
+
endpoint_url = "http://160.40.54.158:8087/SemanticFramework/api/retrieve"
|
180 |
+
headers = {'Content-type': 'application/json'}
|
181 |
+
auth = (username, password)
|
182 |
+
|
183 |
+
print(" === THE FINAL OUTPUT THAT IS SENT TO THE SF === \n")
|
184 |
+
print(json_output)
|
185 |
+
|
186 |
+
response = requests.post(endpoint_url, json=json_output, headers=headers, auth=auth)
|
187 |
+
|
188 |
+
if response.status_code == 200:
|
189 |
+
print("\n\n === THE RESPONSE RETRIEVED BY THE SF ===")
|
190 |
+
print("\n")
|
191 |
+
print(response.content)
|
192 |
+
print("\n\n")
|
193 |
+
else:
|
194 |
+
print("Error sending JSON to endpoint")
|
195 |
+
print("\n\n")
|
196 |
+
|
197 |
+
|
198 |
+
|
199 |
def main(sentence):
|
200 |
"""
|
201 |
Function to bind together all the info and be executed
|
202 |
"""
|
203 |
|
204 |
+
end_time_load = time.time()
|
205 |
+
elapsed_time_load = end_time_load - start_time_load
|
206 |
+
|
207 |
+
print(f"=== THE NATURAL LANGUAGE MODULE HAS BEEN LOADED. THE LOADING TIME WAS : {elapsed_time_load} seconds === \n\n")
|
208 |
+
print(" === THE NATURAL LANGUAGE MODULE HAS JUST STARTED PROCESSING THE INPUT SENTENCE === \n")
|
209 |
+
|
210 |
+
start_time = time.time()
|
211 |
+
|
212 |
nl_data = natural_language_module(sentence)
|
213 |
+
|
214 |
+
print("\n")
|
215 |
+
print(" === FINAL OUTPUT === \n")
|
216 |
+
print(nl_data)
|
217 |
+
|
218 |
+
end_time = time.time()
|
219 |
+
elapsed_time = end_time - start_time
|
220 |
+
|
221 |
+
print("\n\n\n ==== THE NATURAL LANGUAGE MODULE HAS FINISHED ITS EXECUTION ==== \n\n\n")
|
222 |
+
print(f" ==== THE TIME OF SENTENCE PROCESSING WAS: {elapsed_time} seconds ==== \n\n")
|
223 |
+
|
224 |
nl_json = process_json_sf(nl_data, sentence)
|
225 |
|
226 |
return nl_json
|