Spaces:
Running
Running
Update: Make language check softer
Browse files
app.py
CHANGED
|
@@ -100,19 +100,15 @@ def _predict_np2_compat(self, text, k=1, threshold=0.0, on_unicode_error='strict
|
|
| 100 |
lid_model.predict = types.MethodType(_predict_np2_compat, lid_model)
|
| 101 |
|
| 102 |
### Check if lang is english #####################################################
|
| 103 |
-
|
| 104 |
def is_eng(review: str):
|
| 105 |
-
lang_labels, lang_probs = lid_model.predict(review)
|
| 106 |
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
return False, 0.0
|
| 111 |
|
| 112 |
-
|
| 113 |
-
lang_prob = float(lang_probs[0])
|
| 114 |
-
|
| 115 |
-
return lang_label[1] == "__label__en", lang_label[1], lang_prob
|
| 116 |
|
| 117 |
### Do actual prediction #########################################################
|
| 118 |
@spaces.GPU(duration=10) # Sekunden GPU-Zeit pro Call
|
|
@@ -126,13 +122,13 @@ def predict(review: str):
|
|
| 126 |
return "Please enter a review.", {}
|
| 127 |
|
| 128 |
# Check for lang of text
|
| 129 |
-
review_is_eng,
|
| 130 |
|
| 131 |
# Abort if text is not english
|
| 132 |
if not review_is_eng:
|
| 133 |
# immer zwei Outputs zurückgeben
|
| 134 |
return "Only English reviews are supported.", {
|
| 135 |
-
"
|
| 136 |
"prob": review_lang_prob
|
| 137 |
}
|
| 138 |
|
|
|
|
| 100 |
lid_model.predict = types.MethodType(_predict_np2_compat, lid_model)
|
| 101 |
|
| 102 |
### Check if lang is english #####################################################
|
|
|
|
| 103 |
def is_eng(review: str):
|
| 104 |
+
lang_labels, lang_probs = lid_model.predict(review, k=3)
|
| 105 |
|
| 106 |
+
if "__label__en" in lang_labels:
|
| 107 |
+
idx = lang_labels.index("__label__en")
|
| 108 |
|
| 109 |
+
return True, float(lang_probs[idx])
|
|
|
|
| 110 |
|
| 111 |
+
return False, 0.0
|
|
|
|
|
|
|
|
|
|
| 112 |
|
| 113 |
### Do actual prediction #########################################################
|
| 114 |
@spaces.GPU(duration=10) # Sekunden GPU-Zeit pro Call
|
|
|
|
| 122 |
return "Please enter a review.", {}
|
| 123 |
|
| 124 |
# Check for lang of text
|
| 125 |
+
review_is_eng, review_lang_prob = is_eng(review)
|
| 126 |
|
| 127 |
# Abort if text is not english
|
| 128 |
if not review_is_eng:
|
| 129 |
# immer zwei Outputs zurückgeben
|
| 130 |
return "Only English reviews are supported.", {
|
| 131 |
+
"is_eng": review_is_eng,
|
| 132 |
"prob": review_lang_prob
|
| 133 |
}
|
| 134 |
|