Havoc999 commited on
Commit
a53d303
Β·
verified Β·
1 Parent(s): 3fae56e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -16
app.py CHANGED
@@ -5,13 +5,12 @@ import gradio as gr
5
  from transformers import AutoTokenizer
6
  from huggingface_hub import hf_hub_download
7
 
8
- # Silence background warning messages
9
  os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
10
 
11
- # Target model repository
12
  REPO_ID = "Havoc999/distilbert-imdb-sentiment-onnx"
13
 
14
- print("Loading model and tokenizer locally...")
15
  tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
16
  model_path = hf_hub_download(repo_id=REPO_ID, filename="model.onnx")
17
  session = ort.InferenceSession(model_path)
@@ -20,7 +19,7 @@ def predict_sentiment(text):
20
  if not text.strip():
21
  return {"Negative 🎬❌": 0.5, "Positive πŸΏβœ…": 0.5}
22
 
23
- # 1. Tokenize incoming text structure
24
  inputs = tokenizer(
25
  text,
26
  return_tensors="np",
@@ -34,26 +33,23 @@ def predict_sentiment(text):
34
  "attention_mask": inputs["attention_mask"].astype(np.int64)
35
  }
36
 
37
- # 2. Execute local ONNX inference
38
- onnx_outputs = session.run(None, onnx_inputs)
39
 
40
- # 3. Extract the logits array safely and flatten it to 1D
41
- logits = onnx_outputs.flatten()
42
 
43
- # 4. Standard Softmax conversion
44
  exp_logits = np.exp(logits - np.max(logits))
45
  probabilities = exp_logits / np.sum(exp_logits)
46
 
47
- # 5. Extract items individually to guarantee conversion to pure Python floats
48
- neg_score = float(probabilities)
49
- pos_score = float(probabilities)
50
-
51
  return {
52
- "Negative 🎬❌": neg_score,
53
- "Positive πŸΏβœ…": pos_score
54
  }
55
 
56
- # 6. Minimal Gradio layout configuration
57
  demo = gr.Interface(
58
  fn=predict_sentiment,
59
  inputs=gr.Textbox(lines=4, placeholder="Type an IMDB movie review here...", label="Your Movie Review"),
 
5
  from transformers import AutoTokenizer
6
  from huggingface_hub import hf_hub_download
7
 
8
+ # Silence background warning logs
9
  os.environ["TRANSFORMERS_NO_ADVISORY_WARNINGS"] = "1"
10
 
 
11
  REPO_ID = "Havoc999/distilbert-imdb-sentiment-onnx"
12
 
13
+ print("Loading model assets locally...")
14
  tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
15
  model_path = hf_hub_download(repo_id=REPO_ID, filename="model.onnx")
16
  session = ort.InferenceSession(model_path)
 
19
  if not text.strip():
20
  return {"Negative 🎬❌": 0.5, "Positive πŸΏβœ…": 0.5}
21
 
22
+ # 1. Tokenize text inputs
23
  inputs = tokenizer(
24
  text,
25
  return_tensors="np",
 
33
  "attention_mask": inputs["attention_mask"].astype(np.int64)
34
  }
35
 
36
+ # 2. Force ONNX to return ONLY the classification logits array
37
+ onnx_outputs = session.run(["logits"], onnx_inputs)
38
 
39
+ # 3. Safely convert the output to a flat 1D numpy array, regardless of nested lists
40
+ logits = np.asarray(onnx_outputs).flatten()
41
 
42
+ # 4. Softmax probability conversion
43
  exp_logits = np.exp(logits - np.max(logits))
44
  probabilities = exp_logits / np.sum(exp_logits)
45
 
46
+ # 5. Return pure Python float scalars to the Gradio UI
 
 
 
47
  return {
48
+ "Negative 🎬❌": float(probabilities),
49
+ "Positive πŸΏβœ…": float(probabilities)
50
  }
51
 
52
+ # 6. Initialize UI
53
  demo = gr.Interface(
54
  fn=predict_sentiment,
55
  inputs=gr.Textbox(lines=4, placeholder="Type an IMDB movie review here...", label="Your Movie Review"),