|
import json |
|
import numpy as np |
|
from tokenizers import Tokenizer |
|
import onnxruntime as ort |
|
from huggingface_hub import hf_hub_download |
|
import gradio as gr |
|
|
|
|
|
class ONNXInferencePipeline: |
|
def __init__(self, repo_id): |
|
|
|
|
|
self.onnx_path = hf_hub_download(repo_id=repo_id, filename="minddmeter.onnx") |
|
self.tokenizer_path = hf_hub_download(repo_id=repo_id, filename="train_bpe_tokenizer.json") |
|
self.config_path = hf_hub_download(repo_id=repo_id, filename="hyperparameters.json") |
|
|
|
|
|
|
|
with open(self.config_path, "r") as f: |
|
self.config = json.load(f) |
|
|
|
|
|
self.tokenizer = Tokenizer.from_file(self.tokenizer_path) |
|
self.max_len = self.config["tokenizer"]["max_len"] |
|
|
|
|
|
self.session = ort.InferenceSession(self.onnx_path) |
|
|
|
self.providers = ['CPUExecutionProvider'] |
|
if 'CUDAExecutionProvider' in ort.get_available_providers(): |
|
self.providers = ['CUDAExecutionProvider'] |
|
self.session.set_providers(self.providers) |
|
|
|
def preprocess(self, text): |
|
""" |
|
Tokenize the input text, truncate or pad it to the maximum length, and return a numpy array. |
|
""" |
|
encoding = self.tokenizer.encode(text) |
|
|
|
ids = encoding.ids[:self.max_len] |
|
|
|
padding = [0] * (self.max_len - len(ids)) |
|
return np.array(ids + padding, dtype=np.int64).reshape(1, -1) |
|
|
|
def predict(self, text): |
|
""" |
|
Given an input text string, run inference and return: |
|
- The predicted label (mapped from the model output), |
|
- The confidence of that prediction, |
|
- The full list of probabilities. |
|
""" |
|
|
|
input_array = self.preprocess(text) |
|
|
|
|
|
results = self.session.run(None, {"input": input_array}) |
|
|
|
|
|
logits = results[0] |
|
exp_logits = np.exp(logits) |
|
probabilities = exp_logits / np.sum(exp_logits, axis=1, keepdims=True) |
|
predicted_class = int(np.argmax(probabilities)) |
|
|
|
|
|
|
|
label_mapping = {'neg': 'Negative', 'pos': 'Positive'} |
|
class_labels = ['neg', 'pos'] |
|
predicted_label = label_mapping[class_labels[predicted_class]] |
|
confidence = float(probabilities[0][predicted_class]) |
|
|
|
return { |
|
'label': predicted_label, |
|
'confidence': confidence, |
|
'probabilities': probabilities[0].tolist() |
|
} |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
pipeline = ONNXInferencePipeline(repo_id="iimran/MindMeter") |
|
|
|
|
|
example_texts = [ |
|
"I feel the constant strain of financial hardship, and the stress of it all leaves me emotionally drained.", |
|
"I am stressed by the thought of unexpected expenses derailing my already fragile budget. Can I request council to make an installment plan for me?", |
|
"The burden of my debt is immense, and the stress from my financial hardships is unbearable. Can I request council to waive my rates fee?" |
|
] |
|
|
|
for text in example_texts: |
|
result = pipeline.predict(text) |
|
print(f"Input: {text}") |
|
print(f"Prediction: {result['label']} ({result['confidence']:.2%})") |
|
print(f"Confidence Scores: Negative={result['probabilities'][0]:.2%}, Positive={result['probabilities'][1]:.2%}") |
|
print("-" * 80) |
|
|
|
|
|
def gradio_predict(text): |
|
result = pipeline.predict(text) |
|
return ( |
|
f"Prediction: {result['label']} ({result['confidence']:.2%})\n" |
|
f"Confidence Scores: Negative={result['probabilities'][0]:.2%}, Positive={result['probabilities'][1]:.2%}" |
|
) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=gradio_predict, |
|
inputs=gr.Textbox(lines=7, placeholder="Enter text here..."), |
|
outputs="text", |
|
title="MindMeter – Service-Focused Stress Identification Agent", |
|
description=( |
|
"MindMeter evaluates the mental and emotional tone of your text input. " |
|
"It analyzes the content to provide an indication of the overall stress element in the input, " |
|
"displaying a Low Stressed, Medium Stressed, High Stressed or not stressed scores. " |
|
"Enter your text below to see the analysis." |
|
), |
|
examples=[ |
|
"I feel the constant strain of financial hardship, and the stress of it all leaves me emotionally drained.", |
|
"I am stressed by the thought of unexpected expenses derailing my already fragile budget. Can I request council to make an installment plan for me?", |
|
"The burden of my debt is immense, and the stress from my financial hardships is unbearable. Can I request council to waive my rates fee?" |
|
] |
|
) |
|
|
|
|
|
iface.launch() |
|
|