Spaces:
Running on CPU Upgrade

ziem-io commited on
Commit
ba84108
·
1 Parent(s): ec632c7

Update: Codestyle

Browse files
Files changed (1) hide show
  1. app.py +31 -35
app.py CHANGED
@@ -1,42 +1,38 @@
1
  # Standardbibliotheken
2
- 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
- import time
7
- import random
8
 
9
  # Machine Learning / NLP
10
- import torch # PyTorch (Model, Tensor, Device)
11
- import fasttext # Sprach-ID (lid.176)
12
- # Diese beiden werden oft nicht direkt aufgerufen, müssen aber installiert sein,
13
- # damit Hugging Face/Tokenizer korrekt funktionieren (SentencePiece-Backends, Converter).
14
- import sentencepiece # Required für SentencePiece-basierte Tokenizer (DeBERTa v3)
15
- import tiktoken # Optionaler Converter; verhindert Fallback-Fehler/Warnungen
16
- from langid.langid import LanguageIdentifier, model
17
-
18
- # Hugging Face / Ökosystem
19
- import spaces
20
- from transformers import AutoTokenizer # Tokenizer-Lader (mit use_fast=False für SentencePiece)
21
- from huggingface_hub import hf_hub_download # Dateien/Weights aus dem HF Hub laden
22
- from safetensors.torch import load_file # Sicheres & schnelles Laden von Weights (.safetensors)
 
 
23
 
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 (
32
- #load_model_and_tokenizer,
33
- predict_flavours,
34
- #predict_is_review,
35
- #TARGET_COLUMNS,
36
- #ICONS
37
  )
38
- from lib.wheel import build_svg_with_values
39
- from lib.examples import EXAMPLES
40
 
41
  ### Stettings ####################################################################
42
 
@@ -76,14 +72,14 @@ model_flavours.to(device).eval()
76
 
77
  ID = LanguageIdentifier.from_modelstring(model, norm_probs=True)
78
 
79
- def is_eng(text: str, min_chars: int = 6, threshold: float = 0.1):
80
  t = (text or "").strip()
81
  if len(t) < min_chars:
82
  return True, 0.0
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
  result = deepl_client.translate_text(text, target_lang=target_lang)
89
  return result.text
@@ -102,11 +98,11 @@ def predict(review: str):
102
  return "Please enter a review.", {}
103
 
104
  # Check for lang of text
105
- review_is_eng, review_lang_prob = is_eng(review)
106
 
107
  # Abort if text is not english
108
  if not review_is_eng:
109
- review = translate_en(review)
110
  html_out += f"""<div style='border-radius: 2px; padding: 1px 5px; background-color: rgb(255, 237, 213);'>Your text has been automatically translated</div>
111
  <p>{review}</p>
112
  """
@@ -146,7 +142,7 @@ def predict(review: str):
146
  def random_text():
147
  return random.choice(EXAMPLES)
148
 
149
- def get_device_info():
150
  if torch.cuda.is_available():
151
  return f"<span style='border-radius: 2px; padding: 1px 5px; background-color: rgb(220, 252, 231);'>Runs on GPU: {torch.cuda.get_device_name(0)}</span>"
152
  else:
@@ -167,7 +163,7 @@ with gr.Blocks(css=custom_css) as demo:
167
  <h3>Automatically turns Whisky Tasting Notes into Flavour Wheels.</h3>
168
  <p>This model is a fine-tuned version of <a href='https://huggingface.co/microsoft/deberta-v3-base'>microsoft/deberta-v3-base</a> designed to analyze English whisky tasting notes. It predicts the intensity of eight sensory categories — <strong>grainy</strong>, <strong>grassy</strong>, <strong>fragrant</strong>, <strong>fruity</strong>, <strong>peated</strong>, <strong>woody</strong>, <strong>winey</strong> and <strong>off-notes</strong> — on a continuous scale from 0 (none) to 4 (extreme).</p>
169
  """)
170
- gr.HTML(f"<span style='color: var(--block-title-text-color)'>{get_device_info()}</span>")
171
 
172
  with gr.Row(): # alles nebeneinander
173
  with gr.Column(scale=1): # linke Seite: Input
 
1
  # Standardbibliotheken
2
+ import os # Umgebungsvariablen (z.B. HF_TOKEN)
3
+ import time # Timing / Performance-Messung
4
+ import random # Zufallswerte (z.B. Beispiel-Reviews)
5
+ import html # HTML-Escaping für sichere Ausgabe in Gradio
6
+ import types # Monkeypatching von Instanzen (fastText .predict)
7
+ import numpy as np # Numerische Arrays und Wahrscheinlichkeiten
8
 
9
  # Machine Learning / NLP
10
+ import torch # PyTorch: Modelle, Tensoren, Devices
11
+ import fasttext # Sprach-ID-Modell (lid.176)
12
+ # Folgende sind notwendig, auch wenn sie nicht explizit genutzt werden:
13
+ import sentencepiece # Pflicht für SentencePiece-basierte Tokenizer (z.B. DeBERTa v3)
14
+ import tiktoken # Optionaler Converter (verhindert Fallback-Fehler bei Tokenizer)
15
+ from langid.langid import LanguageIdentifier, model # Alternative Sprach-ID
16
+
17
+ # Hugging Face Ökosystem
18
+ import spaces # HF Spaces-Dekoratoren (@spaces.GPU)
19
+ from transformers import AutoTokenizer # Tokenizer laden (use_fast=False für DeBERTa v3)
20
+ from huggingface_hub import hf_hub_download # Download von Dateien/Weights aus dem HF Hub
21
+ from safetensors.torch import load_file # Sicheres & schnelles Laden von Weights (.safetensors)
22
+
23
+ # Übersetzung
24
+ import deepl # DeepL API für automatische Übersetzung
25
 
26
  # UI / Serving
27
+ import gradio as gr # Web-UI für Demo/Spaces
 
 
28
 
29
  # Projektspezifische Module
30
  from lib.bert_regressor import BertMultiHeadRegressor
31
  from lib.bert_regressor_utils import (
32
+ predict_flavours, # Hauptfunktion: Vorhersage der 8 Aromenachsen
 
 
 
 
33
  )
34
+ from lib.wheel import build_svg_with_values # SVG-Rendering für Flavour Wheel
35
+ from lib.examples import EXAMPLES # Beispiel-Reviews (vordefiniert)
36
 
37
  ### Stettings ####################################################################
38
 
 
72
 
73
  ID = LanguageIdentifier.from_modelstring(model, norm_probs=True)
74
 
75
+ def _is_eng(text: str, min_chars: int = 6, threshold: float = 0.1):
76
  t = (text or "").strip()
77
  if len(t) < min_chars:
78
  return True, 0.0
79
  lang, prob = ID.classify(t) # prob ∈ [0,1]
80
  return (lang == "en" and prob >= threshold), float(prob)
81
 
82
+ def _translate_en(text: str, target_lang: str = "EN-GB"):
83
  deepl_client = deepl.Translator(DEEPL_API_KEY)
84
  result = deepl_client.translate_text(text, target_lang=target_lang)
85
  return result.text
 
98
  return "Please enter a review.", {}
99
 
100
  # Check for lang of text
101
+ review_is_eng, review_lang_prob = _is_eng(review)
102
 
103
  # Abort if text is not english
104
  if not review_is_eng:
105
+ review = _translate_en(review)
106
  html_out += f"""<div style='border-radius: 2px; padding: 1px 5px; background-color: rgb(255, 237, 213);'>Your text has been automatically translated</div>
107
  <p>{review}</p>
108
  """
 
142
  def random_text():
143
  return random.choice(EXAMPLES)
144
 
145
+ def _get_device_info():
146
  if torch.cuda.is_available():
147
  return f"<span style='border-radius: 2px; padding: 1px 5px; background-color: rgb(220, 252, 231);'>Runs on GPU: {torch.cuda.get_device_name(0)}</span>"
148
  else:
 
163
  <h3>Automatically turns Whisky Tasting Notes into Flavour Wheels.</h3>
164
  <p>This model is a fine-tuned version of <a href='https://huggingface.co/microsoft/deberta-v3-base'>microsoft/deberta-v3-base</a> designed to analyze English whisky tasting notes. It predicts the intensity of eight sensory categories — <strong>grainy</strong>, <strong>grassy</strong>, <strong>fragrant</strong>, <strong>fruity</strong>, <strong>peated</strong>, <strong>woody</strong>, <strong>winey</strong> and <strong>off-notes</strong> — on a continuous scale from 0 (none) to 4 (extreme).</p>
165
  """)
166
+ gr.HTML(f"<span style='color: var(--block-title-text-color)'>{_get_device_info()}</span>")
167
 
168
  with gr.Row(): # alles nebeneinander
169
  with gr.Column(scale=1): # linke Seite: Input