Update app.py
Browse files
app.py
CHANGED
|
@@ -8,116 +8,109 @@ import gradio as gr
|
|
| 8 |
|
| 9 |
class ONNXInferencePipeline:
|
| 10 |
def __init__(self, repo_id):
|
| 11 |
-
|
| 12 |
-
# Note: The ONNX model file is now "moodmeter.onnx"
|
| 13 |
-
self.onnx_path = hf_hub_download(repo_id=repo_id, filename="minddmeter.onnx")
|
| 14 |
self.tokenizer_path = hf_hub_download(repo_id=repo_id, filename="train_bpe_tokenizer.json")
|
| 15 |
self.config_path = hf_hub_download(repo_id=repo_id, filename="hyperparameters.json")
|
| 16 |
|
| 17 |
-
# Load configuration from
|
| 18 |
-
#
|
| 19 |
with open(self.config_path, "r") as f:
|
| 20 |
self.config = json.load(f)
|
| 21 |
|
| 22 |
-
# Initialize the tokenizer
|
| 23 |
self.tokenizer = Tokenizer.from_file(self.tokenizer_path)
|
| 24 |
-
|
|
|
|
| 25 |
|
| 26 |
-
# Initialize the ONNX runtime session
|
| 27 |
self.session = ort.InferenceSession(self.onnx_path)
|
| 28 |
-
# Use CUDA if available
|
| 29 |
self.providers = ['CPUExecutionProvider']
|
| 30 |
if 'CUDAExecutionProvider' in ort.get_available_providers():
|
| 31 |
self.providers = ['CUDAExecutionProvider']
|
| 32 |
self.session.set_providers(self.providers)
|
| 33 |
|
| 34 |
def preprocess(self, text):
|
| 35 |
-
"""
|
| 36 |
-
Tokenize the input text, truncate or pad it to the maximum length, and return a numpy array.
|
| 37 |
-
"""
|
| 38 |
encoding = self.tokenizer.encode(text)
|
| 39 |
-
# Truncate
|
| 40 |
ids = encoding.ids[:self.max_len]
|
| 41 |
-
# Pad with zeros if
|
| 42 |
padding = [0] * (self.max_len - len(ids))
|
| 43 |
return np.array(ids + padding, dtype=np.int64).reshape(1, -1)
|
| 44 |
|
| 45 |
def predict(self, text):
|
| 46 |
"""
|
| 47 |
-
Given an input text string, run inference and return
|
| 48 |
-
|
| 49 |
-
-
|
| 50 |
-
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
"""
|
| 52 |
-
# Preprocess the text
|
| 53 |
input_array = self.preprocess(text)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
# Compute softmax probabilities from the logits.
|
| 59 |
-
logits = results[0]
|
| 60 |
exp_logits = np.exp(logits)
|
| 61 |
probabilities = exp_logits / np.sum(exp_logits, axis=1, keepdims=True)
|
| 62 |
predicted_class = int(np.argmax(probabilities))
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
# Here we assume the model outputs: 0 -> "neg" and 1 -> "pos"
|
| 66 |
-
label_mapping = {'neg': 'Negative', 'pos': 'Positive'}
|
| 67 |
-
class_labels = ['neg', 'pos']
|
| 68 |
-
predicted_label = label_mapping[class_labels[predicted_class]]
|
| 69 |
confidence = float(probabilities[0][predicted_class])
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
|
| 78 |
-
# Example usage and Gradio Interface
|
| 79 |
if __name__ == "__main__":
|
| 80 |
-
# Initialize the pipeline with the correct Hugging Face repository ID.
|
| 81 |
pipeline = ONNXInferencePipeline(repo_id="iimran/MindMeter")
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
print(f"Confidence Scores: Negative={result['probabilities'][0]:.2%}, Positive={result['probabilities'][1]:.2%}")
|
| 95 |
-
print("-" * 80)
|
| 96 |
|
| 97 |
-
# Define a function for Gradio to call.
|
| 98 |
def gradio_predict(text):
|
| 99 |
result = pipeline.predict(text)
|
| 100 |
-
return
|
| 101 |
-
f"Prediction: {result['label']} ({result['confidence']:.2%})\n"
|
| 102 |
-
f"Confidence Scores: Negative={result['probabilities'][0]:.2%}, Positive={result['probabilities'][1]:.2%}"
|
| 103 |
-
)
|
| 104 |
|
| 105 |
-
# Create the Gradio interface.
|
| 106 |
iface = gr.Interface(
|
| 107 |
fn=gradio_predict,
|
| 108 |
-
inputs=gr.Textbox(lines=7, placeholder="Enter text here..."),
|
| 109 |
outputs="text",
|
| 110 |
-
title="MindMeter –
|
| 111 |
description=(
|
| 112 |
-
"MindMeter
|
| 113 |
-
"
|
| 114 |
-
"displaying a Low Stressed, Medium Stressed, High Stressed or not stressed scores. "
|
| 115 |
-
"Enter your text below to see the analysis."
|
| 116 |
),
|
| 117 |
examples=[
|
| 118 |
-
"
|
| 119 |
-
"I
|
| 120 |
-
"
|
| 121 |
]
|
| 122 |
)
|
| 123 |
|
|
|
|
| 8 |
|
| 9 |
class ONNXInferencePipeline:
|
| 10 |
def __init__(self, repo_id):
|
| 11 |
+
self.onnx_path = hf_hub_download(repo_id=repo_id, filename="mindmeter.onnx")
|
|
|
|
|
|
|
| 12 |
self.tokenizer_path = hf_hub_download(repo_id=repo_id, filename="train_bpe_tokenizer.json")
|
| 13 |
self.config_path = hf_hub_download(repo_id=repo_id, filename="hyperparameters.json")
|
| 14 |
|
| 15 |
+
# Load configuration from hyperparameters.json.
|
| 16 |
+
# This file should have a key "MAX_LEN" specifying the maximum token sequence length.
|
| 17 |
with open(self.config_path, "r") as f:
|
| 18 |
self.config = json.load(f)
|
| 19 |
|
| 20 |
+
# Initialize the tokenizer from file.
|
| 21 |
self.tokenizer = Tokenizer.from_file(self.tokenizer_path)
|
| 22 |
+
# Use the maximum sequence length from the hyperparameters.
|
| 23 |
+
self.max_len = self.config["MAX_LEN"]
|
| 24 |
|
| 25 |
+
# Initialize the ONNX runtime session.
|
| 26 |
self.session = ort.InferenceSession(self.onnx_path)
|
| 27 |
+
# Use CUDA if available, otherwise default to CPU.
|
| 28 |
self.providers = ['CPUExecutionProvider']
|
| 29 |
if 'CUDAExecutionProvider' in ort.get_available_providers():
|
| 30 |
self.providers = ['CUDAExecutionProvider']
|
| 31 |
self.session.set_providers(self.providers)
|
| 32 |
|
| 33 |
def preprocess(self, text):
|
|
|
|
|
|
|
|
|
|
| 34 |
encoding = self.tokenizer.encode(text)
|
| 35 |
+
# Truncate to self.max_len tokens
|
| 36 |
ids = encoding.ids[:self.max_len]
|
| 37 |
+
# Pad with zeros if necessary
|
| 38 |
padding = [0] * (self.max_len - len(ids))
|
| 39 |
return np.array(ids + padding, dtype=np.int64).reshape(1, -1)
|
| 40 |
|
| 41 |
def predict(self, text):
|
| 42 |
"""
|
| 43 |
+
Given an input text string, run inference and return only the granular stress level.
|
| 44 |
+
The model outputs:
|
| 45 |
+
0 -> "Not Stressed"
|
| 46 |
+
1 -> "Stressed"
|
| 47 |
+
When the model predicts "Stressed", a confidence-based thresholding is applied:
|
| 48 |
+
- confidence < 0.40: "Not Stressed" (fallback)
|
| 49 |
+
- 0.40 ≤ confidence < 0.65: "Low Stress"
|
| 50 |
+
- 0.65 ≤ confidence < 0.90: "Moderate Stress"
|
| 51 |
+
- 0.90 ≤ confidence: "High Stress"
|
| 52 |
"""
|
|
|
|
| 53 |
input_array = self.preprocess(text)
|
| 54 |
|
| 55 |
+
outputs = self.session.run(None, {"input": input_array})
|
| 56 |
+
logits = outputs[0]
|
| 57 |
|
|
|
|
|
|
|
| 58 |
exp_logits = np.exp(logits)
|
| 59 |
probabilities = exp_logits / np.sum(exp_logits, axis=1, keepdims=True)
|
| 60 |
predicted_class = int(np.argmax(probabilities))
|
| 61 |
+
class_labels = ["Not Stressed", "Stressed"]
|
| 62 |
+
predicted_label = class_labels[predicted_class]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
confidence = float(probabilities[0][predicted_class])
|
| 64 |
|
| 65 |
+
if predicted_label == "Stressed":
|
| 66 |
+
# Use the confidence of the "Stressed" class
|
| 67 |
+
stress_confidence = confidence
|
| 68 |
+
if stress_confidence < 0.40:
|
| 69 |
+
stress_level = "Not Stressed" # Fallback (unlikely)
|
| 70 |
+
elif 0.40 <= stress_confidence < 0.65:
|
| 71 |
+
stress_level = "Low Stress"
|
| 72 |
+
elif 0.65 <= stress_confidence < 0.90:
|
| 73 |
+
stress_level = "Moderate Stress"
|
| 74 |
+
else: # 0.90 ≤ stress_confidence ≤ 1.00
|
| 75 |
+
stress_level = "High Stress"
|
| 76 |
+
else:
|
| 77 |
+
stress_level = "Not Stressed"
|
| 78 |
+
|
| 79 |
+
return {"stress_level": stress_level}
|
| 80 |
|
| 81 |
|
|
|
|
| 82 |
if __name__ == "__main__":
|
|
|
|
| 83 |
pipeline = ONNXInferencePipeline(repo_id="iimran/MindMeter")
|
| 84 |
|
| 85 |
+
text1 = "Yay! what a happy life"
|
| 86 |
+
text2 = "I’ve missed another loan payment, and I don’t know how I’m going to catch up. The pressure is unbearable."
|
| 87 |
+
text3 = "I am upset about how badly life is trating me these days, its shit and i wanna end it"
|
| 88 |
+
|
| 89 |
+
result1 = pipeline.predict(text1)
|
| 90 |
+
result2 = pipeline.predict(text2)
|
| 91 |
+
result3 = pipeline.predict(text3)
|
| 92 |
+
|
| 93 |
+
print(f"Prediction for text 1: {result1}")
|
| 94 |
+
print(f"Prediction for text 2: {result2}")
|
| 95 |
+
print(f"Prediction for text 3: {result3}")
|
|
|
|
|
|
|
| 96 |
|
|
|
|
| 97 |
def gradio_predict(text):
|
| 98 |
result = pipeline.predict(text)
|
| 99 |
+
return result["stress_level"]
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
| 101 |
iface = gr.Interface(
|
| 102 |
fn=gradio_predict,
|
| 103 |
+
inputs=gr.Textbox(lines=7, placeholder="Enter your text here..."),
|
| 104 |
outputs="text",
|
| 105 |
+
title="MindMeter – Granular Stress Level Predictor",
|
| 106 |
description=(
|
| 107 |
+
"MindMeter predicts the granular stress level from your text input. "
|
| 108 |
+
"The possible outputs are: Not Stressed, Low Stress, Moderate Stress, or High Stress."
|
|
|
|
|
|
|
| 109 |
),
|
| 110 |
examples=[
|
| 111 |
+
"Yay! what a happy life",
|
| 112 |
+
"I’ve missed another loan payment, and I don’t know how I’m going to catch up. The pressure is unbearable.",
|
| 113 |
+
"I am upset about how badly life is trating me these days, its shit and i wanna end it"
|
| 114 |
]
|
| 115 |
)
|
| 116 |
|