Spaces:
Runtime error
Runtime error
Countdown timer
Browse files
app.py
CHANGED
@@ -7,6 +7,7 @@ from thefuzz import process, fuzz
|
|
7 |
import numpy as np
|
8 |
import re
|
9 |
from string import punctuation
|
|
|
10 |
|
11 |
|
12 |
API_URL = "https://api-inference.huggingface.co/models/Dabid/abusive-tagalog-profanity-detection"
|
@@ -37,29 +38,18 @@ obj_pronouns = read_text('obj_pronouns')
|
|
37 |
profanities = read_text('profanities', 'json')
|
38 |
|
39 |
|
40 |
-
# for profanity in profanities:
|
41 |
-
# print(profanity, process.extractOne(profanity, tweet.split(), scorer=fuzz.ratio))
|
42 |
-
|
43 |
|
44 |
def fuzzy_lookup(tweet):
|
45 |
|
46 |
matched_profanity = dict()
|
47 |
-
|
48 |
-
# Convert Profanity Dict to List
|
49 |
lookup_profanity = np.concatenate([np.hstack(list(profanities.values())), list(profanities.keys())])
|
50 |
|
51 |
-
# Loop each word in tweet
|
52 |
for word in tweet.split():
|
53 |
scores = []
|
54 |
matched_words = []
|
55 |
-
|
56 |
-
# Remove punctuations
|
57 |
word = word.strip(punctuation)
|
58 |
-
|
59 |
-
# Only get digits and letters then lowercase
|
60 |
processed_word = re.sub("[^a-zA-Z0-9@]", "", word)
|
61 |
-
|
62 |
-
# If word > 4 chars
|
63 |
if len(processed_word) >= 4:
|
64 |
# Get fuzzy ratio
|
65 |
for lookup_word in lookup_words:
|
@@ -134,6 +124,19 @@ def preprocess(tweet):
|
|
134 |
return preprocessed_tweet, matches
|
135 |
|
136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
def predict(tweet):
|
138 |
|
139 |
preprocessed_tweet, matched_profanity = preprocess(tweet)
|
@@ -142,8 +145,9 @@ def predict(tweet):
|
|
142 |
|
143 |
if type(prediction) is dict:
|
144 |
# return "Model is still loading. Try again."
|
145 |
-
print(prediction)
|
146 |
-
|
|
|
147 |
|
148 |
if bool(matched_profanity) == False:
|
149 |
return "No profanity"
|
|
|
7 |
import numpy as np
|
8 |
import re
|
9 |
from string import punctuation
|
10 |
+
import time
|
11 |
|
12 |
|
13 |
API_URL = "https://api-inference.huggingface.co/models/Dabid/abusive-tagalog-profanity-detection"
|
|
|
38 |
profanities = read_text('profanities', 'json')
|
39 |
|
40 |
|
|
|
|
|
|
|
41 |
|
42 |
def fuzzy_lookup(tweet):
|
43 |
|
44 |
matched_profanity = dict()
|
|
|
|
|
45 |
lookup_profanity = np.concatenate([np.hstack(list(profanities.values())), list(profanities.keys())])
|
46 |
|
|
|
47 |
for word in tweet.split():
|
48 |
scores = []
|
49 |
matched_words = []
|
|
|
|
|
50 |
word = word.strip(punctuation)
|
|
|
|
|
51 |
processed_word = re.sub("[^a-zA-Z0-9@]", "", word)
|
52 |
+
|
|
|
53 |
if len(processed_word) >= 4:
|
54 |
# Get fuzzy ratio
|
55 |
for lookup_word in lookup_words:
|
|
|
124 |
return preprocessed_tweet, matches
|
125 |
|
126 |
|
127 |
+
def countdown(seconds):
|
128 |
+
start_time = time.time()
|
129 |
+
while True:
|
130 |
+
elapsed_time = int(time.time() - start_time)
|
131 |
+
remaining_time = max(seconds - elapsed_time, 0)
|
132 |
+
if remaining_time == 0:
|
133 |
+
print("Time's up!")
|
134 |
+
break
|
135 |
+
print(remaining_time)
|
136 |
+
yield remaining_time
|
137 |
+
time.sleep(1)
|
138 |
+
|
139 |
+
|
140 |
def predict(tweet):
|
141 |
|
142 |
preprocessed_tweet, matched_profanity = preprocess(tweet)
|
|
|
145 |
|
146 |
if type(prediction) is dict:
|
147 |
# return "Model is still loading. Try again."
|
148 |
+
print(prediction['estimated_time'])
|
149 |
+
cd = countdown(prediction['estimated_time']) # TODO build a countdown
|
150 |
+
return f"Model is loading. Try again after {next(cd)} seconds."
|
151 |
|
152 |
if bool(matched_profanity) == False:
|
153 |
return "No profanity"
|