ziem-io commited on
Commit
9ea7979
·
1 Parent(s): a8b9f74

New: Add lang check

Browse files
Files changed (1) hide show
  1. app.py +37 -6
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import fasttext
3
  import html
 
 
4
  from huggingface_hub import hf_hub_download
5
 
6
  # Projektspezifische Module
@@ -23,17 +25,44 @@ lid_path = hf_hub_download(
23
 
24
  lid_model = fasttext.load_model(lid_path)
25
 
26
- ### Do actual prediction ##############################################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
- def predict(review: str, mode: str):
 
29
 
30
- review = (review or "").strip()
31
 
32
- # Check language of review
33
  lang_labels, lang_probs = lid_model.predict(review)
34
  lang_label = lang_labels[0]
35
  lang_prob = float(lang_probs[0])
36
 
 
 
 
 
 
 
 
 
 
 
37
  if not review:
38
  # immer zwei Outputs zurückgeben
39
  return "<i>Please enter a review.</i>", {}
@@ -47,8 +76,10 @@ def predict(review: str, mode: str):
47
  json_out = {
48
  "review": review,
49
  "mode": mode,
50
- "lang_label": lang_label,
51
- "lang_prob": lang_prob
 
 
52
  }
53
  return html_out, json_out
54
 
 
1
  import gradio as gr
2
  import fasttext
3
  import html
4
+ import numpy as np
5
+ import types
6
  from huggingface_hub import hf_hub_download
7
 
8
  # Projektspezifische Module
 
25
 
26
  lid_model = fasttext.load_model(lid_path)
27
 
28
+ # robustes predict mit NumPy-2-Fix + Fallback, falls fastText nur Labels liefert
29
+ def _predict_np2_compat(self, text, k=1, threshold=0.0, on_unicode_error='strict'):
30
+ out = self.f.predict(text, k, threshold, on_unicode_error)
31
+ # Fälle:
32
+ # 1) (labels, probs)
33
+ # 2) labels-only (einige Builds/SWIG-Versionen)
34
+ if isinstance(out, tuple) and len(out) == 2:
35
+ labels, probs = out
36
+ else:
37
+ labels = out
38
+ # sinnvolle Defaults, falls keine Wahrscheinlichkeiten vorliegen
39
+ if isinstance(labels, (list, tuple)):
40
+ probs = [1.0] * len(labels)
41
+ else:
42
+ labels = [labels]
43
+ probs = [1.0]
44
+ return labels, np.asarray(probs) # np.asarray statt np.array(copy=False)
45
 
46
+ # Instanz patchen
47
+ lid_model.predict = types.MethodType(_predict_np2_compat, lid_model)
48
 
49
+ ### Check if lang is english ##############################################
50
 
51
+ def is_eng(review: str):
52
  lang_labels, lang_probs = lid_model.predict(review)
53
  lang_label = lang_labels[0]
54
  lang_prob = float(lang_probs[0])
55
 
56
+ return lang_label[1] == "__lang_en__", lang_prob
57
+
58
+ ### Do actual prediction ##############################################
59
+
60
+ def predict(review: str, mode: str):
61
+
62
+ review = (review or "").strip()
63
+
64
+ review_is_eng, review_is_eng_prob = is_eng(review)
65
+
66
  if not review:
67
  # immer zwei Outputs zurückgeben
68
  return "<i>Please enter a review.</i>", {}
 
76
  json_out = {
77
  "review": review,
78
  "mode": mode,
79
+ "is_en": {
80
+ "is": review_is_eng,
81
+ "prob": review_is_eng_prob
82
+ }
83
  }
84
  return html_out, json_out
85