ziem-io commited on
Commit
406793a
·
1 Parent(s): b0c4a5b

New: Add automatic translation

Browse files
Files changed (2) hide show
  1. app.py +16 -9
  2. requirements.txt +2 -1
app.py CHANGED
@@ -24,6 +24,8 @@ from safetensors.torch import load_file # Sicheres & schnelles Laden von We
24
  # UI / Serving
25
  import gradio as gr # Web-UI für Demo/Spaces
26
 
 
 
27
  # Projektspezifische Module
28
  from lib.bert_regressor import BertMultiHeadRegressor
29
  from lib.bert_regressor_utils import (
@@ -44,6 +46,7 @@ REPO_ID = "ziem-io/deberta_flavour_regressor_multi_head"
44
  # (optional) falls das Model-Repo privat ist:
45
  HF_TOKEN = os.getenv("HF_TOKEN") # in Space-Secrets hinterlegen
46
  MODEL_FILE = os.getenv("MODEL_FILE") # in Space-Secrets hinterlegen
 
47
 
48
  ##################################################################################
49
 
@@ -80,11 +83,18 @@ def is_eng(text: str, min_chars: int = 6, threshold: float = 0.1):
80
  lang, prob = ID.classify(t) # prob ∈ [0,1]
81
  return (lang == "en" and prob >= threshold), float(prob)
82
 
 
 
 
 
 
83
  ### Do actual prediction #########################################################
84
  @spaces.GPU(duration=10) # Sekunden GPU-Zeit pro Call
85
  def predict(review: str):
86
 
87
  review = (review or "").strip()
 
 
88
 
89
  # Abort if no text if given
90
  if not review:
@@ -96,11 +106,9 @@ def predict(review: str):
96
 
97
  # Abort if text is not english
98
  if not review_is_eng:
99
- # immer zwei Outputs zurückgeben
100
- return "Currently, only English reviews are supported.", {
101
- "is_eng": review_is_eng,
102
- "prob": review_lang_prob
103
- }
104
 
105
  prediction_flavours = {}
106
  prediction_flavours_list = [0, 0, 0, 0, 0, 0, 0, 0]
@@ -111,9 +119,7 @@ def predict(review: str):
111
  prediction_flavours_list = list(prediction_flavours.values())
112
  t_end_flavours = time.time()
113
 
114
- #html_out = f"<b>{html.escape(review)}</b>"
115
-
116
- html_out = build_svg_with_values(prediction_flavours_list)
117
 
118
  json_out = {
119
  "results": [{
@@ -127,6 +133,7 @@ def predict(review: str):
127
  "review": review,
128
  "model": MODEL_FILE,
129
  "device": device,
 
130
  "duration": round((t_end_flavours - t_start_flavours), 3),
131
  }
132
 
@@ -163,7 +170,7 @@ with gr.Blocks(css=custom_css) as demo:
163
  with gr.Row(): # alles nebeneinander
164
  with gr.Column(scale=1): # linke Seite: Input
165
  review_box = gr.Textbox(
166
- label="Whisky Review (English only)",
167
  lines=8,
168
  placeholder="Enter whisky review",
169
  value=random_text(),
 
24
  # UI / Serving
25
  import gradio as gr # Web-UI für Demo/Spaces
26
 
27
+ import deepl
28
+
29
  # Projektspezifische Module
30
  from lib.bert_regressor import BertMultiHeadRegressor
31
  from lib.bert_regressor_utils import (
 
46
  # (optional) falls das Model-Repo privat ist:
47
  HF_TOKEN = os.getenv("HF_TOKEN") # in Space-Secrets hinterlegen
48
  MODEL_FILE = os.getenv("MODEL_FILE") # in Space-Secrets hinterlegen
49
+ DEEPL_API_KEY = os.getenv("DEEPL_API_KEY") # in Space-Secrets hinterlegen
50
 
51
  ##################################################################################
52
 
 
83
  lang, prob = ID.classify(t) # prob ∈ [0,1]
84
  return (lang == "en" and prob >= threshold), float(prob)
85
 
86
+ def translate_en(text: str, target_lang: str = "EN-GB"):
87
+ deepl_client = deepl.Translator(DEEPL_API_KEY)
88
+ translated_text = deepl_client.translate_text(text, target_lang=target_lang)
89
+ return translated_text
90
+
91
  ### Do actual prediction #########################################################
92
  @spaces.GPU(duration=10) # Sekunden GPU-Zeit pro Call
93
  def predict(review: str):
94
 
95
  review = (review or "").strip()
96
+ is_translated = False
97
+ html_out = ""
98
 
99
  # Abort if no text if given
100
  if not review:
 
106
 
107
  # Abort if text is not english
108
  if not review_is_eng:
109
+ review = translate_en(review)
110
+ html_out += "<b>Your text has been automatically translated.</b>"
111
+ is_translated = True
 
 
112
 
113
  prediction_flavours = {}
114
  prediction_flavours_list = [0, 0, 0, 0, 0, 0, 0, 0]
 
119
  prediction_flavours_list = list(prediction_flavours.values())
120
  t_end_flavours = time.time()
121
 
122
+ html_out += build_svg_with_values(prediction_flavours_list)
 
 
123
 
124
  json_out = {
125
  "results": [{
 
133
  "review": review,
134
  "model": MODEL_FILE,
135
  "device": device,
136
+ "is_translated": is_translated,
137
  "duration": round((t_end_flavours - t_start_flavours), 3),
138
  }
139
 
 
170
  with gr.Row(): # alles nebeneinander
171
  with gr.Column(scale=1): # linke Seite: Input
172
  review_box = gr.Textbox(
173
+ label="Whisky Review",
174
  lines=8,
175
  placeholder="Enter whisky review",
176
  value=random_text(),
requirements.txt CHANGED
@@ -8,4 +8,5 @@ sentencepiece
8
  tiktoken
9
  accelerate>=0.30
10
  spaces
11
- langid
 
 
8
  tiktoken
9
  accelerate>=0.30
10
  spaces
11
+ langid
12
+ deepl