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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
import openpyxl
|
4 |
+
import tempfile
|
5 |
+
import pycountry
|
6 |
+
|
7 |
+
classifier = pipeline("zero-shot-classification",
|
8 |
+
model="LogicSpine/address-large-text-classifier")
|
9 |
+
|
10 |
+
def check_for_third(address: str) -> bool:
|
11 |
+
countries = [country.name.lower() for country in pycountry.countries]
|
12 |
+
old_country_names = [
|
13 |
+
"burma",
|
14 |
+
"ceylon",
|
15 |
+
"persia",
|
16 |
+
"zaire",
|
17 |
+
"upper volta",
|
18 |
+
"swaziland",
|
19 |
+
"macedonia",
|
20 |
+
"czech republic",
|
21 |
+
"turkey",
|
22 |
+
"holland",
|
23 |
+
"kampuchea",
|
24 |
+
"dahomey",
|
25 |
+
"bechuanaland",
|
26 |
+
"gold coast",
|
27 |
+
"nyasaland",
|
28 |
+
"korea"
|
29 |
+
]
|
30 |
+
countries = countries + old_country_names
|
31 |
+
if "," in address:
|
32 |
+
address = address.split(",")
|
33 |
+
else:
|
34 |
+
address = [address.strip()]
|
35 |
+
|
36 |
+
for ad in address:
|
37 |
+
if ad.lower().strip() in countries:
|
38 |
+
return True
|
39 |
+
return False
|
40 |
+
|
41 |
+
def check_for_first(address: str) -> bool:
|
42 |
+
keyword_list = ["school", "laboratory", "department"]
|
43 |
+
for key in keyword_list:
|
44 |
+
if key.lower() in address.lower():
|
45 |
+
return True
|
46 |
+
return False
|
47 |
+
|
48 |
+
def check_for_second(address: str) -> bool:
|
49 |
+
keyword_list = ["university"]
|
50 |
+
for key in keyword_list:
|
51 |
+
if key.lower() in address.lower().strip():
|
52 |
+
return True
|
53 |
+
return False
|
54 |
+
|
55 |
+
def compaire_two(bigger: str, smaller: str, mid: int) -> bool:
|
56 |
+
"""Helps to find the result according to the priority
|
57 |
+
|
58 |
+
Args:
|
59 |
+
bigger (str): Pass the bigger
|
60 |
+
smaller (str): And smaller
|
61 |
+
mid (int): Pass the mid where 1 reffer to 1st and 2 as 2nd and 3 as 3rd
|
62 |
+
|
63 |
+
Raises:
|
64 |
+
ValueError: If invalid mid is passed
|
65 |
+
|
66 |
+
Returns:
|
67 |
+
bool: if bigger have more priority then True else False
|
68 |
+
"""
|
69 |
+
if mid == 1:
|
70 |
+
if check_for_first(bigger):
|
71 |
+
return True
|
72 |
+
lab = ["School", "Department", "Laboratory"]
|
73 |
+
elif mid == 2:
|
74 |
+
if check_for_second(bigger):
|
75 |
+
return True
|
76 |
+
lab = ["University", "Polytechnic"]
|
77 |
+
elif mid == 3:
|
78 |
+
if check_for_third(bigger):
|
79 |
+
return True
|
80 |
+
lab = ["State", "District", "Country"]
|
81 |
+
else:
|
82 |
+
raise ValueError(f"Invalid value passed in mid : {mid}")
|
83 |
+
|
84 |
+
sb = classifier(bigger, lab)
|
85 |
+
ss = classifier(smaller, lab)
|
86 |
+
|
87 |
+
result_bigger = sum(sb["scores"])
|
88 |
+
result_smaller = sum(ss["scores"])
|
89 |
+
|
90 |
+
if result_bigger > result_smaller:
|
91 |
+
return True
|
92 |
+
return False
|
93 |
+
|
94 |
+
def get_ai_position(address: str) -> int:
|
95 |
+
"""This function use AI to find the position of the address
|
96 |
+
|
97 |
+
Args:
|
98 |
+
address (str): Pass the address here
|
99 |
+
|
100 |
+
Returns:
|
101 |
+
int: Return the mid 1 for 4th and 2 for 5th and 3 for 6th
|
102 |
+
"""
|
103 |
+
if check_for_first(address):
|
104 |
+
return 1
|
105 |
+
if check_for_second(address):
|
106 |
+
return 2
|
107 |
+
if check_for_third(address):
|
108 |
+
return 3
|
109 |
+
|
110 |
+
result_first = sum(classifier(address, ["School", "Department", "Laboratory"])["scores"])
|
111 |
+
result_second = sum(classifier(address, ["University", "Polytechnic"])["scores"])
|
112 |
+
result_third = sum(classifier(address, ["State", "District", "Country"])["scores"])
|
113 |
+
|
114 |
+
total = max(result_first, result_second, result_third)
|
115 |
+
|
116 |
+
if total == result_first:
|
117 |
+
return 1
|
118 |
+
elif total == result_second:
|
119 |
+
return 2
|
120 |
+
if total == result_third:
|
121 |
+
return 3
|
122 |
+
else:
|
123 |
+
return 3
|
124 |
+
|
125 |
+
def compare_by_mid(bigger: int, smaller: int, address: str, threshold: float = 0.1) -> bool:
|
126 |
+
"""Helps to find the proper position for the address according to the mid
|
127 |
+
|
128 |
+
Args:
|
129 |
+
bigger (int): Pass the mid 1, 2 or 3
|
130 |
+
smaller (int): Pass the mid 1, 2 or 3
|
131 |
+
address (str): If possibility of bigger is more then return True else False
|
132 |
+
threshold (float): Minimum score difference to consider valid comparison
|
133 |
+
|
134 |
+
Returns:
|
135 |
+
bool: Boolean
|
136 |
+
"""
|
137 |
+
if bigger == 1:
|
138 |
+
if check_for_first(bigger):
|
139 |
+
return True
|
140 |
+
bigger_l = ["School", "Department", "Laboratory"]
|
141 |
+
elif bigger == 2:
|
142 |
+
if check_for_second(address):
|
143 |
+
return True
|
144 |
+
bigger_l = ["University", "Polytechnic"]
|
145 |
+
else:
|
146 |
+
if check_for_third(address):
|
147 |
+
return True
|
148 |
+
bigger_l = ["State", "District", "Country"]
|
149 |
+
|
150 |
+
if smaller == 1:
|
151 |
+
smaller_l = ["School", "Department", "Laboratory"]
|
152 |
+
elif smaller == 2:
|
153 |
+
smaller_l = ["University", "Polytechnic"]
|
154 |
+
else:
|
155 |
+
smaller_l = ["State", "District", "Country"]
|
156 |
+
|
157 |
+
result_bigger = classifier(address, bigger_l)
|
158 |
+
result_smaller = classifier(address, smaller_l)
|
159 |
+
|
160 |
+
max_bigger = max(result_bigger["scores"])
|
161 |
+
max_smaller = max(result_smaller["scores"])
|
162 |
+
score_difference = max_smaller - max_bigger
|
163 |
+
return score_difference > threshold
|
164 |
+
|
165 |
+
def find_missing_data(data1: str, data2: str, data3: str, var1: str, var2: str, var3: str) -> str:
|
166 |
+
"""Helps to find the missing data
|
167 |
+
|
168 |
+
Args:
|
169 |
+
data1 (str): Pass the first data or you can say address
|
170 |
+
data2 (str): Pass the 2nd address
|
171 |
+
data3 (str): pass third address
|
172 |
+
var1 (str): pass the first variable to check
|
173 |
+
var2 (str): pass 2nd variable to check
|
174 |
+
var3 (str): pass the third variable to check the 3rd address
|
175 |
+
|
176 |
+
Returns:
|
177 |
+
str: return the address as string
|
178 |
+
"""
|
179 |
+
data_set = {data1, data2, data3}
|
180 |
+
variables_filled = {var1, var2, var3}
|
181 |
+
missing_data = data_set - variables_filled
|
182 |
+
if missing_data:
|
183 |
+
return ', '.join(missing_data)
|
184 |
+
else:
|
185 |
+
return "All data has been assigned correctly."
|
186 |
+
|
187 |
+
def swapper(i1, i2, i3):
|
188 |
+
first, second, third = None, None, None
|
189 |
+
|
190 |
+
inputs = [i1, i2, i3]
|
191 |
+
first_candidates = []
|
192 |
+
|
193 |
+
for data in inputs:
|
194 |
+
original_data = data
|
195 |
+
|
196 |
+
if check_for_third(data):
|
197 |
+
if third is None:
|
198 |
+
third = data
|
199 |
+
else:
|
200 |
+
third, data = data, third
|
201 |
+
if check_for_first(data):
|
202 |
+
first_candidates.append(data)
|
203 |
+
elif check_for_second(data):
|
204 |
+
if second is None:
|
205 |
+
second = data
|
206 |
+
else:
|
207 |
+
second, data = data, second
|
208 |
+
|
209 |
+
elif check_for_first(data):
|
210 |
+
first_candidates.append(data)
|
211 |
+
|
212 |
+
elif check_for_second(data):
|
213 |
+
if second is None:
|
214 |
+
second = data
|
215 |
+
else:
|
216 |
+
second, data = data, second
|
217 |
+
if first_candidates:
|
218 |
+
first = first_candidates[0] if first is None else first
|
219 |
+
if len(first_candidates) > 1:
|
220 |
+
second = first_candidates[1] if second is None else second
|
221 |
+
remaining_data = [i1, i2, i3]
|
222 |
+
if first is None:
|
223 |
+
first = remaining_data.pop(remaining_data.index(next(filter(lambda x: x not in {first, second, third}, remaining_data), None)))
|
224 |
+
if second is None:
|
225 |
+
second = remaining_data.pop(remaining_data.index(next(filter(lambda x: x not in {first, second, third}, remaining_data), None)))
|
226 |
+
if third is None:
|
227 |
+
third = remaining_data.pop(remaining_data.index(next(filter(lambda x: x not in {first, second, third}, remaining_data), None)))
|
228 |
+
|
229 |
+
return first, second, third
|
230 |
+
|
231 |
+
def settle_all_address(address_first: str, address_second: str, address_third: str):
|
232 |
+
address_1 = address_first
|
233 |
+
address_2 = address_second
|
234 |
+
address_3 = address_third
|
235 |
+
r_add1 = None
|
236 |
+
r_add2 = None
|
237 |
+
r_add3 = None
|
238 |
+
|
239 |
+
# Check for first function
|
240 |
+
if check_for_first(address_first):
|
241 |
+
r_add1 = address_first
|
242 |
+
|
243 |
+
elif check_for_first(address_second):
|
244 |
+
r_add1 = address_second
|
245 |
+
|
246 |
+
elif check_for_first(address_third):
|
247 |
+
r_add1 = address_third
|
248 |
+
|
249 |
+
# Check for second function
|
250 |
+
if check_for_second(address_first):
|
251 |
+
r_add2 = address_first
|
252 |
+
|
253 |
+
elif check_for_second(address_second):
|
254 |
+
r_add2 = address_second
|
255 |
+
|
256 |
+
elif check_for_second(address_third):
|
257 |
+
r_add2 = address_third
|
258 |
+
|
259 |
+
# Check for third function
|
260 |
+
if check_for_third(address_first):
|
261 |
+
r_add3 = address_first
|
262 |
+
|
263 |
+
elif check_for_third(address_second):
|
264 |
+
r_add3 = address_second
|
265 |
+
|
266 |
+
elif check_for_third(address_third):
|
267 |
+
r_add3 = address_third
|
268 |
+
|
269 |
+
if r_add1 == r_add2 or r_add1 == r_add3 or r_add2 == r_add3:
|
270 |
+
# Duplicate data found now perform the comparizon in here
|
271 |
+
if r_add1 == r_add2 == r_add3:
|
272 |
+
r_add1 = None
|
273 |
+
r_add2 = None
|
274 |
+
r_add3 = None
|
275 |
+
else:
|
276 |
+
if r_add1 == r_add2 and r_add1 != None: # If address 1 and address 2 is same then use AI for checking
|
277 |
+
m_add = find_missing_data(address_1, address_2, address_3, r_add1, r_add2, r_add3) # Find the missing address and add it to the r_add3
|
278 |
+
if compaire_two(m_add, r_add2, 1):
|
279 |
+
r_add2 = m_add
|
280 |
+
else:
|
281 |
+
r_add1 = m_add
|
282 |
+
|
283 |
+
elif r_add1 == r_add3 and r_add1 != None:
|
284 |
+
m_add = find_missing_data(address_1, address_2, address_3, r_add1, r_add2, r_add3) # Find the missing address and add it to the r_add3
|
285 |
+
if compaire_two(m_add, r_add3, 1):
|
286 |
+
r_add1 = m_add
|
287 |
+
else:
|
288 |
+
r_add3 = m_add
|
289 |
+
|
290 |
+
elif r_add2 == r_add3 and r_add2 != None:
|
291 |
+
m_add = find_missing_data(address_1, address_2, address_3, r_add1, r_add2, r_add3) # Find the missing address and add it to the r_add3
|
292 |
+
if compaire_two(m_add, r_add3, 3):
|
293 |
+
r_add3 = m_add
|
294 |
+
else:
|
295 |
+
r_add2 = m_add
|
296 |
+
|
297 |
+
if r_add1 == None or r_add2 == None or r_add3 == None:
|
298 |
+
# if any of them is None then calculate the address
|
299 |
+
ai_position1 = get_ai_position(address_1)
|
300 |
+
ai_position2 = get_ai_position(address_2)
|
301 |
+
ai_position3 = get_ai_position(address_3)
|
302 |
+
|
303 |
+
if ai_position1 == 3:
|
304 |
+
if r_add3:
|
305 |
+
pass
|
306 |
+
else:
|
307 |
+
r_add3 = address_1
|
308 |
+
if r_add1 == None or r_add2 == None:
|
309 |
+
if r_add3 == address_1:
|
310 |
+
if compare_by_mid(1, 2, address_2):
|
311 |
+
r_add1 = address_2
|
312 |
+
r_add2 = address_3
|
313 |
+
else:
|
314 |
+
r_add1 = address_3
|
315 |
+
r_add2 = address_2
|
316 |
+
|
317 |
+
elif r_add3 == address_2:
|
318 |
+
if compare_by_mid(1, 2, address_1):
|
319 |
+
r_add1 = address_1
|
320 |
+
r_add2 = address_3
|
321 |
+
else:
|
322 |
+
r_add1 = address_3
|
323 |
+
r_add2 = address_1
|
324 |
+
|
325 |
+
elif r_add3 == address_3:
|
326 |
+
if compare_by_mid(1, 2, address_1):
|
327 |
+
r_add1 = address_1
|
328 |
+
r_add2 = address_2
|
329 |
+
else:
|
330 |
+
r_add1 = address_2
|
331 |
+
r_add2 = address_1
|
332 |
+
|
333 |
+
elif ai_position1 == 2:
|
334 |
+
if r_add2:
|
335 |
+
pass
|
336 |
+
else:
|
337 |
+
r_add2 = address_1
|
338 |
+
if r_add1 == None or r_add3 == None:
|
339 |
+
if r_add2 == address_1:
|
340 |
+
if compare_by_mid(1, 3, address_2):
|
341 |
+
r_add1 = address_2
|
342 |
+
r_add3 = address_3
|
343 |
+
else:
|
344 |
+
r_add1 = address_3
|
345 |
+
r_add3 = address_2
|
346 |
+
|
347 |
+
elif r_add2 == address_2:
|
348 |
+
if compare_by_mid(1, 3, address_1):
|
349 |
+
r_add1 = address_1
|
350 |
+
r_add3 = address_3
|
351 |
+
else:
|
352 |
+
r_add1 = address_3
|
353 |
+
r_add3 = address_1
|
354 |
+
|
355 |
+
elif r_add2 == address_3:
|
356 |
+
if compare_by_mid(1, 3, address_1):
|
357 |
+
r_add1 = address_1
|
358 |
+
r_add3 = address_2
|
359 |
+
else:
|
360 |
+
r_add1 = address_2
|
361 |
+
r_add3 = address_1
|
362 |
+
|
363 |
+
else:
|
364 |
+
if r_add1:
|
365 |
+
pass
|
366 |
+
else:
|
367 |
+
r_add1 = address_1
|
368 |
+
if r_add2 == None or r_add3 == None:
|
369 |
+
if r_add1 == address_1:
|
370 |
+
if compare_by_mid(2, 3, address_2):
|
371 |
+
r_add2 = address_2
|
372 |
+
r_add3 = address_3
|
373 |
+
else:
|
374 |
+
r_add2 = address_3
|
375 |
+
r_add3 = address_2
|
376 |
+
|
377 |
+
elif r_add1 == address_2:
|
378 |
+
if compare_by_mid(2, 3, address_1):
|
379 |
+
r_add2 = address_1
|
380 |
+
r_add3 = address_3
|
381 |
+
else:
|
382 |
+
r_add2 = address_3
|
383 |
+
r_add3 = address_1
|
384 |
+
|
385 |
+
elif r_add1 == address_3:
|
386 |
+
if compare_by_mid(2, 3, address_1):
|
387 |
+
r_add2 = address_1
|
388 |
+
r_add3 = address_2
|
389 |
+
else:
|
390 |
+
r_add2 = address_2
|
391 |
+
r_add3 = address_1
|
392 |
+
|
393 |
+
|
394 |
+
if ai_position2 == 3:
|
395 |
+
if r_add3:
|
396 |
+
pass
|
397 |
+
else:
|
398 |
+
r_add3 = address_2
|
399 |
+
if r_add1 == None or r_add2 == None:
|
400 |
+
if r_add3 == address_1:
|
401 |
+
if compare_by_mid(1, 2, address_2):
|
402 |
+
r_add1 = address_2
|
403 |
+
r_add2 = address_3
|
404 |
+
else:
|
405 |
+
r_add1 = address_3
|
406 |
+
r_add2 = address_2
|
407 |
+
|
408 |
+
elif r_add3 == address_2:
|
409 |
+
if compare_by_mid(1, 2, address_1):
|
410 |
+
r_add1 = address_1
|
411 |
+
r_add2 = address_3
|
412 |
+
else:
|
413 |
+
r_add1 = address_3
|
414 |
+
r_add2 = address_1
|
415 |
+
|
416 |
+
elif r_add3 == address_3:
|
417 |
+
if compare_by_mid(1, 2, address_1):
|
418 |
+
r_add1 = address_1
|
419 |
+
r_add2 = address_2
|
420 |
+
else:
|
421 |
+
r_add1 = address_2
|
422 |
+
r_add2 = address_1
|
423 |
+
|
424 |
+
elif ai_position2 == 2:
|
425 |
+
if r_add2:
|
426 |
+
pass
|
427 |
+
else:
|
428 |
+
r_add2 = address_2
|
429 |
+
if r_add1 == None or r_add3 == None:
|
430 |
+
if r_add2 == address_1:
|
431 |
+
if compare_by_mid(1, 3, address_2):
|
432 |
+
r_add1 = address_2
|
433 |
+
r_add3 = address_3
|
434 |
+
else:
|
435 |
+
r_add1 = address_3
|
436 |
+
r_add3 = address_2
|
437 |
+
|
438 |
+
elif r_add2 == address_2:
|
439 |
+
if compare_by_mid(1, 3, address_1):
|
440 |
+
r_add1 = address_1
|
441 |
+
r_add3 = address_3
|
442 |
+
else:
|
443 |
+
r_add1 = address_3
|
444 |
+
r_add3 = address_1
|
445 |
+
|
446 |
+
elif r_add2 == address_3:
|
447 |
+
if compare_by_mid(1, 3, address_1):
|
448 |
+
r_add1 = address_1
|
449 |
+
r_add3 = address_2
|
450 |
+
else:
|
451 |
+
r_add1 = address_2
|
452 |
+
r_add3 = address_1
|
453 |
+
else:
|
454 |
+
if r_add1:
|
455 |
+
pass
|
456 |
+
else:
|
457 |
+
r_add1 = address_2
|
458 |
+
if r_add2 == None or r_add3 == None:
|
459 |
+
if r_add1 == address_1:
|
460 |
+
if compare_by_mid(2, 3, address_2):
|
461 |
+
r_add2 = address_2
|
462 |
+
r_add3 = address_3
|
463 |
+
else:
|
464 |
+
r_add2 = address_3
|
465 |
+
r_add3 = address_2
|
466 |
+
|
467 |
+
elif r_add1 == address_2:
|
468 |
+
if compare_by_mid(2, 3, address_1):
|
469 |
+
r_add2 = address_1
|
470 |
+
r_add3 = address_3
|
471 |
+
else:
|
472 |
+
r_add2 = address_3
|
473 |
+
r_add3 = address_1
|
474 |
+
|
475 |
+
elif r_add1 == address_3:
|
476 |
+
if compare_by_mid(2, 3, address_1):
|
477 |
+
r_add2 = address_1
|
478 |
+
r_add3 = address_2
|
479 |
+
else:
|
480 |
+
r_add2 = address_2
|
481 |
+
r_add3 = address_1
|
482 |
+
|
483 |
+
|
484 |
+
if ai_position3 == 3:
|
485 |
+
if r_add3:
|
486 |
+
pass
|
487 |
+
else:
|
488 |
+
r_add3 = address_3
|
489 |
+
if r_add1 == None or r_add2 == None:
|
490 |
+
if r_add3 == address_1:
|
491 |
+
if compare_by_mid(1, 2, address_2):
|
492 |
+
r_add1 = address_2
|
493 |
+
r_add2 = address_3
|
494 |
+
else:
|
495 |
+
r_add1 = address_3
|
496 |
+
r_add2 = address_2
|
497 |
+
|
498 |
+
elif r_add3 == address_2:
|
499 |
+
if compare_by_mid(1, 2, address_1):
|
500 |
+
r_add1 = address_1
|
501 |
+
r_add2 = address_3
|
502 |
+
else:
|
503 |
+
r_add1 = address_3
|
504 |
+
r_add2 = address_1
|
505 |
+
|
506 |
+
elif r_add3 == address_3:
|
507 |
+
if compare_by_mid(1, 2, address_1):
|
508 |
+
r_add1 = address_1
|
509 |
+
r_add2 = address_2
|
510 |
+
else:
|
511 |
+
r_add1 = address_2
|
512 |
+
r_add2 = address_1
|
513 |
+
|
514 |
+
elif ai_position3 == 2:
|
515 |
+
if r_add2:
|
516 |
+
pass
|
517 |
+
else:
|
518 |
+
r_add2 = address_3
|
519 |
+
if r_add1 == None or r_add3 == None:
|
520 |
+
if r_add2 == address_1:
|
521 |
+
if compare_by_mid(1, 3, address_2):
|
522 |
+
r_add1 = address_2
|
523 |
+
r_add3 = address_3
|
524 |
+
else:
|
525 |
+
r_add1 = address_3
|
526 |
+
r_add3 = address_2
|
527 |
+
|
528 |
+
elif r_add2 == address_2:
|
529 |
+
if compare_by_mid(1, 3, address_1):
|
530 |
+
r_add1 = address_1
|
531 |
+
r_add3 = address_3
|
532 |
+
else:
|
533 |
+
r_add1 = address_3
|
534 |
+
r_add3 = address_1
|
535 |
+
|
536 |
+
elif r_add2 == address_3:
|
537 |
+
if compare_by_mid(1, 3, address_1):
|
538 |
+
r_add1 = address_1
|
539 |
+
r_add3 = address_2
|
540 |
+
else:
|
541 |
+
r_add1 = address_2
|
542 |
+
r_add3 = address_1
|
543 |
+
|
544 |
+
else:
|
545 |
+
if r_add1:
|
546 |
+
pass
|
547 |
+
else:
|
548 |
+
r_add1 = address_3
|
549 |
+
if r_add2 == None or r_add3 == None:
|
550 |
+
if r_add1 == address_1:
|
551 |
+
if compare_by_mid(2, 3, address_2):
|
552 |
+
r_add2 = address_2
|
553 |
+
r_add3 = address_3
|
554 |
+
else:
|
555 |
+
r_add2 = address_3
|
556 |
+
r_add3 = address_2
|
557 |
+
|
558 |
+
elif r_add1 == address_2:
|
559 |
+
if compare_by_mid(2, 3, address_1):
|
560 |
+
r_add2 = address_1
|
561 |
+
r_add3 = address_3
|
562 |
+
else:
|
563 |
+
r_add2 = address_3
|
564 |
+
r_add3 = address_1
|
565 |
+
|
566 |
+
elif r_add1 == address_3:
|
567 |
+
if compare_by_mid(2, 3, address_1):
|
568 |
+
r_add2 = address_1
|
569 |
+
r_add3 = address_2
|
570 |
+
else:
|
571 |
+
r_add2 = address_2
|
572 |
+
r_add3 = address_1
|
573 |
+
|
574 |
+
return swapper(r_add1, r_add2, r_add3)
|
575 |
+
|
576 |
+
def process_file(filepath: str):
|
577 |
+
wb = openpyxl.load_workbook(filepath, data_only=True)
|
578 |
+
ws = wb.active
|
579 |
+
new_wb = openpyxl.Workbook()
|
580 |
+
new_ws = new_wb.active
|
581 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.xlsx')
|
582 |
+
temp_path = temp_file.name
|
583 |
+
|
584 |
+
columns_to_process = [4, 5, 6]
|
585 |
+
|
586 |
+
for col in range(1, ws.max_column + 1):
|
587 |
+
new_ws.cell(row=1, column=col).value = ws.cell(row=1, column=col).value
|
588 |
+
|
589 |
+
empty_rows = 0
|
590 |
+
|
591 |
+
for row in ws.iter_rows(min_row=2, max_row=ws.max_row):
|
592 |
+
|
593 |
+
if empty_rows > 3:
|
594 |
+
break
|
595 |
+
|
596 |
+
row_num = row[0].row
|
597 |
+
|
598 |
+
for col in range(1, ws.max_column + 1):
|
599 |
+
if col not in columns_to_process:
|
600 |
+
new_ws.cell(row=row_num, column=col).value = ws.cell(row=row_num, column=col).value
|
601 |
+
else:
|
602 |
+
new_ws.cell(row=row_num, column=col).value = None
|
603 |
+
|
604 |
+
address_first = ws.cell(row=row_num, column=4).value
|
605 |
+
address_second = ws.cell(row=row_num, column=5).value
|
606 |
+
address_third = ws.cell(row=row_num, column=6).value
|
607 |
+
|
608 |
+
if address_first != None and address_second != None and address_third != None:
|
609 |
+
|
610 |
+
# print(f"Processing {address_first} | {address_second} | {address_third}")
|
611 |
+
|
612 |
+
ad1, ad2, ad3 = settle_all_address(address_first, address_second, address_third)
|
613 |
+
|
614 |
+
new_ws.cell(row=row_num, column=4).value = ad1
|
615 |
+
new_ws.cell(row=row_num, column=5).value = ad2
|
616 |
+
new_ws.cell(row=row_num, column=6).value = ad3
|
617 |
+
|
618 |
+
print(f"Adding : {ad1} | {ad2} | {ad3}")
|
619 |
+
|
620 |
+
else:
|
621 |
+
empty_rows += 1
|
622 |
+
|
623 |
+
new_wb.save(temp_path)
|
624 |
+
return temp_path
|
625 |
+
|
626 |
+
def gradio_process(file):
|
627 |
+
file_path = file.name
|
628 |
+
output_file_path = process_file(file_path)
|
629 |
+
return output_file_path
|
630 |
+
|
631 |
+
iface = gr.Interface(
|
632 |
+
fn=gradio_process,
|
633 |
+
inputs=gr.File(),
|
634 |
+
outputs=gr.File(),
|
635 |
+
title="AI Address Processor",
|
636 |
+
description="Upload an Excel file, and the AI will process the addresses."
|
637 |
+
)
|
638 |
+
|
639 |
+
iface.launch(debug=True)
|