Spaces:
Running
Running
New: Add lang check
Browse files- app.py +29 -1
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import html
|
|
|
|
| 3 |
|
| 4 |
# Projektspezifische Module
|
| 5 |
from lib.bert_regressor import BertMultiHeadRegressor
|
|
@@ -11,8 +13,27 @@ from lib.bert_regressor_utils import (
|
|
| 11 |
ICONS
|
| 12 |
)
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def predict(review: str, mode: str):
|
|
|
|
| 15 |
review = (review or "").strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
if not review:
|
| 17 |
# immer zwei Outputs zurückgeben
|
| 18 |
return "<i>Please enter a review.</i>", {}
|
|
@@ -23,8 +44,15 @@ def predict(review: str, mode: str):
|
|
| 23 |
return html_out, json_out
|
| 24 |
else: # "JSON"
|
| 25 |
html_out = "" # leer lassen
|
| 26 |
-
json_out = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return html_out, json_out
|
|
|
|
|
|
|
| 28 |
|
| 29 |
iface = gr.Interface(
|
| 30 |
fn=predict,
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import fasttext
|
| 3 |
import html
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
# Projektspezifische Module
|
| 7 |
from lib.bert_regressor import BertMultiHeadRegressor
|
|
|
|
| 13 |
ICONS
|
| 14 |
)
|
| 15 |
|
| 16 |
+
######################################
|
| 17 |
+
|
| 18 |
+
# offizielles Mirror-Repo mit lid.176.*
|
| 19 |
+
lid_path = hf_hub_download(
|
| 20 |
+
repo_id="julien-c/fasttext-language-id",
|
| 21 |
+
filename="lid.176.ftz"
|
| 22 |
+
)
|
| 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>", {}
|
|
|
|
| 44 |
return html_out, json_out
|
| 45 |
else: # "JSON"
|
| 46 |
html_out = "" # leer lassen
|
| 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 |
+
|
| 55 |
+
### Create Form interface with Gradio Framework ########################
|
| 56 |
|
| 57 |
iface = gr.Interface(
|
| 58 |
fn=predict,
|
requirements.txt
CHANGED
|
@@ -1,3 +1,4 @@
|
|
| 1 |
torch>=2.2,<3.0
|
| 2 |
transformers==4.51.0
|
| 3 |
-
numpy
|
|
|
|
|
|
| 1 |
torch>=2.2,<3.0
|
| 2 |
transformers==4.51.0
|
| 3 |
+
numpy
|
| 4 |
+
fasttext
|