Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
New: Do actual prediction
Browse files- app.py +24 -4
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -3,6 +3,7 @@ import os # Umgebungsvariablen (z.B. HF_TOKEN)
|
|
| 3 |
import types # für Instanz-Monkeypatch (fastText .predict)
|
| 4 |
import html # HTML-Escaping für Ausgabe/Gradio
|
| 5 |
import numpy as np # Numerik (z.B. für Wahrscheinlichkeiten)
|
|
|
|
| 6 |
|
| 7 |
# Machine Learning / NLP
|
| 8 |
import torch # PyTorch (Model, Tensor, Device)
|
|
@@ -13,6 +14,7 @@ import sentencepiece # Required für SentencePiece-basierte Tokeniz
|
|
| 13 |
import tiktoken # Optionaler Converter; verhindert Fallback-Fehler/Warnungen
|
| 14 |
|
| 15 |
# Hugging Face / Ökosystem
|
|
|
|
| 16 |
from transformers import AutoTokenizer # Tokenizer-Lader (mit use_fast=False für SentencePiece)
|
| 17 |
from huggingface_hub import hf_hub_download # Dateien/Weights aus dem HF Hub laden
|
| 18 |
from safetensors.torch import load_file # Sicheres & schnelles Laden von Weights (.safetensors)
|
|
@@ -110,16 +112,24 @@ def is_eng(review: str):
|
|
| 110 |
return lang_label[1] == "__label__en", lang_prob
|
| 111 |
|
| 112 |
### Do actual prediction #########################################################
|
| 113 |
-
|
| 114 |
def predict(review: str, mode: str):
|
| 115 |
|
| 116 |
review = (review or "").strip()
|
| 117 |
|
| 118 |
-
review_is_eng, review_is_eng_prob = is_eng(review)
|
| 119 |
-
|
| 120 |
if not review:
|
| 121 |
# immer zwei Outputs zurückgeben
|
| 122 |
return "<i>Please enter a review.</i>", {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
if mode == "table":
|
| 125 |
html_out = f"<b>{html.escape(review)}</b> | <i>{mode}</i>"
|
|
@@ -130,11 +140,21 @@ def predict(review: str, mode: str):
|
|
| 130 |
json_out = {
|
| 131 |
"review": review,
|
| 132 |
"mode": mode,
|
|
|
|
| 133 |
"device": device,
|
|
|
|
| 134 |
"is_en": {
|
| 135 |
"is": review_is_eng,
|
| 136 |
"prob": review_is_eng_prob
|
| 137 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
}
|
| 139 |
return html_out, json_out
|
| 140 |
|
|
|
|
| 3 |
import types # für Instanz-Monkeypatch (fastText .predict)
|
| 4 |
import html # HTML-Escaping für Ausgabe/Gradio
|
| 5 |
import numpy as np # Numerik (z.B. für Wahrscheinlichkeiten)
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
# Machine Learning / NLP
|
| 9 |
import torch # PyTorch (Model, Tensor, Device)
|
|
|
|
| 14 |
import tiktoken # Optionaler Converter; verhindert Fallback-Fehler/Warnungen
|
| 15 |
|
| 16 |
# Hugging Face / Ökosystem
|
| 17 |
+
import spaces
|
| 18 |
from transformers import AutoTokenizer # Tokenizer-Lader (mit use_fast=False für SentencePiece)
|
| 19 |
from huggingface_hub import hf_hub_download # Dateien/Weights aus dem HF Hub laden
|
| 20 |
from safetensors.torch import load_file # Sicheres & schnelles Laden von Weights (.safetensors)
|
|
|
|
| 112 |
return lang_label[1] == "__label__en", lang_prob
|
| 113 |
|
| 114 |
### Do actual prediction #########################################################
|
| 115 |
+
@spaces.GPU(duration=120) # Sekunden GPU-Zeit pro Call
|
| 116 |
def predict(review: str, mode: str):
|
| 117 |
|
| 118 |
review = (review or "").strip()
|
| 119 |
|
|
|
|
|
|
|
| 120 |
if not review:
|
| 121 |
# immer zwei Outputs zurückgeben
|
| 122 |
return "<i>Please enter a review.</i>", {}
|
| 123 |
+
|
| 124 |
+
prediction_flavours = {}
|
| 125 |
+
|
| 126 |
+
review_is_eng, review_is_eng_prob = is_eng(review)
|
| 127 |
+
|
| 128 |
+
if review_is_eng:
|
| 129 |
+
# Do actual predictions if is english and whisky note
|
| 130 |
+
t_start_flavours = time.time()
|
| 131 |
+
prediction_flavours = predict_flavours(review, model_flavours, tokenizer_flavours, device)
|
| 132 |
+
t_end_flavours = time.time()
|
| 133 |
|
| 134 |
if mode == "table":
|
| 135 |
html_out = f"<b>{html.escape(review)}</b> | <i>{mode}</i>"
|
|
|
|
| 140 |
json_out = {
|
| 141 |
"review": review,
|
| 142 |
"mode": mode,
|
| 143 |
+
'model': FILENAME,
|
| 144 |
"device": device,
|
| 145 |
+
"duration": round((t_end_flavours - t_start_flavours), 3),
|
| 146 |
"is_en": {
|
| 147 |
"is": review_is_eng,
|
| 148 |
"prob": review_is_eng_prob
|
| 149 |
+
},
|
| 150 |
+
"results": [{
|
| 151 |
+
'icon': ICONS[name],
|
| 152 |
+
'name': name,
|
| 153 |
+
'score': score,
|
| 154 |
+
#'level': get_level(score)
|
| 155 |
+
}
|
| 156 |
+
for name, score in prediction_flavours.items()
|
| 157 |
+
],
|
| 158 |
}
|
| 159 |
return html_out, json_out
|
| 160 |
|
requirements.txt
CHANGED
|
@@ -6,4 +6,5 @@ huggingface_hub
|
|
| 6 |
safetensors
|
| 7 |
sentencepiece
|
| 8 |
tiktoken
|
| 9 |
-
accelerate>=0.30
|
|
|
|
|
|
| 6 |
safetensors
|
| 7 |
sentencepiece
|
| 8 |
tiktoken
|
| 9 |
+
accelerate>=0.30
|
| 10 |
+
spaces
|