ziem-io commited on
Commit
f1b3d54
·
1 Parent(s): 32c703c

Fix: Abort if text ist not eng

Browse files
Files changed (1) hide show
  1. app.py +19 -14
app.py CHANGED
@@ -112,7 +112,7 @@ def is_eng(review: str):
112
  lang_label = lang_labels[0]
113
  lang_prob = float(lang_probs[0])
114
 
115
- return lang_label[1] == "__label__en", lang_prob
116
 
117
  ### Do actual prediction #########################################################
118
  @spaces.GPU(duration=10) # Sekunden GPU-Zeit pro Call
@@ -120,23 +120,32 @@ def predict(review: str):
120
 
121
  review = (review or "").strip()
122
 
 
123
  if not review:
124
  # immer zwei Outputs zurückgeben
125
  return "<i>Please enter a review.</i>", {}
126
 
 
 
 
 
 
 
 
 
 
 
 
127
  prediction_flavours = {}
128
  prediction_flavours_list = [0, 0, 0, 0, 0, 0, 0, 0]
129
 
130
- review_is_eng, review_is_eng_prob = is_eng(review)
131
-
132
- if review_is_eng:
133
- # Do actual predictions if is english and whisky note
134
- t_start_flavours = time.time()
135
- prediction_flavours = predict_flavours(review, model_flavours, tokenizer_flavours, device)
136
- prediction_flavours_list = list(prediction_flavours.values())
137
- t_end_flavours = time.time()
138
 
139
- #html_out = f"<b>{html.escape(review)}</b>"
140
 
141
  html_out = build_svg_with_values(prediction_flavours_list)
142
 
@@ -145,10 +154,6 @@ def predict(review: str):
145
  "model": FILENAME,
146
  "device": device,
147
  "duration": round((t_end_flavours - t_start_flavours), 3),
148
- "is_en": {
149
- "is": review_is_eng,
150
- "prob": review_is_eng_prob
151
- },
152
  "results": [{
153
  #'icon': ICONS[name],
154
  "name": name,
 
112
  lang_label = lang_labels[0]
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
 
120
 
121
  review = (review or "").strip()
122
 
123
+ # Abort if no text if given
124
  if not review:
125
  # immer zwei Outputs zurückgeben
126
  return "<i>Please enter a review.</i>", {}
127
 
128
+ # Check for lang of text
129
+ review_is_eng, review_lang_label, review_lang_prob = is_eng(review)
130
+
131
+ # Abort if text is not english
132
+ if not review_is_eng:
133
+ # immer zwei Outputs zurückgeben
134
+ return "<i>English texts only</i>", {"is_en": {
135
+ "lang": review_lang_label.replace("__label__", ""),
136
+ "prob": review_lang_prob
137
+ }}
138
+
139
  prediction_flavours = {}
140
  prediction_flavours_list = [0, 0, 0, 0, 0, 0, 0, 0]
141
 
142
+ # Do actual predictions if is english and whisky note
143
+ t_start_flavours = time.time()
144
+ prediction_flavours = predict_flavours(review, model_flavours, tokenizer_flavours, device)
145
+ prediction_flavours_list = list(prediction_flavours.values())
146
+ t_end_flavours = time.time()
 
 
 
147
 
148
+ #html_out = f"<b>{html.escape(review)}</b>"
149
 
150
  html_out = build_svg_with_values(prediction_flavours_list)
151
 
 
154
  "model": FILENAME,
155
  "device": device,
156
  "duration": round((t_end_flavours - t_start_flavours), 3),
 
 
 
 
157
  "results": [{
158
  #'icon': ICONS[name],
159
  "name": name,