iimran commited on
Commit
dc1e335
·
verified ·
1 Parent(s): c9572f2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +112 -0
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import numpy as np
4
+ from tokenizers import Tokenizer
5
+ import onnxruntime as ort
6
+ from huggingface_hub import hf_hub_download
7
+ import gradio as gr
8
+
9
+ class ONNXInferencePipeline:
10
+ def __init__(self, repo_id):
11
+ # Retrieve the Hugging Face token from the environment variable
12
+ hf_token = os.getenv("HF_TOKEN")
13
+ if hf_token is None:
14
+ raise ValueError("HF_TOKEN environment variable is not set.")
15
+
16
+ # Download files from Hugging Face Hub using the token
17
+ self.onnx_path = hf_hub_download(repo_id=repo_id, filename="moodlens.onnx", use_auth_token=hf_token)
18
+ self.tokenizer_path = hf_hub_download(repo_id=repo_id, filename="train_bpe_tokenizer.json", use_auth_token=hf_token)
19
+ self.config_path = hf_hub_download(repo_id=repo_id, filename="hyperparameters.json", use_auth_token=hf_token)
20
+
21
+ # Load configuration
22
+ with open(self.config_path) as f:
23
+ self.config = json.load(f)
24
+
25
+ # Initialize tokenizer
26
+ self.tokenizer = Tokenizer.from_file(self.tokenizer_path)
27
+ self.max_len = self.config["tokenizer"]["max_len"]
28
+
29
+ # Initialize ONNX runtime session
30
+ self.session = ort.InferenceSession(self.onnx_path)
31
+ self.providers = ['CPUExecutionProvider'] # Use CUDA if available
32
+ if 'CUDAExecutionProvider' in ort.get_available_providers():
33
+ self.providers = ['CUDAExecutionProvider']
34
+ self.session.set_providers(self.providers)
35
+
36
+ def preprocess(self, text):
37
+ encoding = self.tokenizer.encode(text)
38
+ ids = encoding.ids[:self.max_len]
39
+ padding = [0] * (self.max_len - len(ids))
40
+ return np.array(ids + padding, dtype=np.int64).reshape(1, -1)
41
+
42
+ def predict(self, text):
43
+ # Preprocess
44
+ input_array = self.preprocess(text)
45
+
46
+ # Run inference
47
+ results = self.session.run(
48
+ None,
49
+ {'input': input_array}
50
+ )
51
+
52
+ # Post-process
53
+ logits = results[0]
54
+ probabilities = np.exp(logits) / np.sum(np.exp(logits), axis=1, keepdims=True)
55
+ predicted_class = int(np.argmax(probabilities))
56
+
57
+ # Map to labels
58
+ label_mapping = {'neg': 'Negative', 'pos': 'Positive'}
59
+ class_labels = ['neg', 'pos']
60
+ return {
61
+ 'label': label_mapping[class_labels[predicted_class]],
62
+ 'confidence': float(probabilities[0][predicted_class]),
63
+ 'probabilities': probabilities[0].tolist()
64
+ }
65
+
66
+ # Example usage
67
+ if __name__ == "__main__":
68
+ # Initialize the pipeline with the Hugging Face repository ID
69
+ pipeline = ONNXInferencePipeline(repo_id="iimran/Moodlens")
70
+
71
+ # Example texts for testing
72
+ example_texts = [
73
+ "This product is absolutely amazing! I love how efficient and easy it is to use.",
74
+ "I am very disappointed with this service. The experience was terrible and frustrating."
75
+ ]
76
+
77
+ for text in example_texts:
78
+ result = pipeline.predict(text)
79
+ print(f"Input: {text}")
80
+ print(f"Prediction: {result['label']} ({result['confidence']:.2%})")
81
+ print(f"Confidence Scores: Negative={result['probabilities'][0]:.2%}, Positive={result['probabilities'][1]:.2%}")
82
+ print("-" * 80)
83
+
84
+ # Define a function for Gradio to use
85
+ def gradio_predict(text):
86
+ result = pipeline.predict(text)
87
+ return (
88
+ f"Prediction: {result['label']} ({result['confidence']:.2%})\n"
89
+ f"Confidence Scores: Negative={result['probabilities'][0]:.2%}, Positive={result['probabilities'][1]:.2%}"
90
+ )
91
+
92
+ # Create a Gradio interface
93
+ iface = gr.Interface(
94
+ fn=gradio_predict, # Function to call
95
+ inputs=gr.Textbox(lines=7, placeholder="Enter text here..."),
96
+ outputs="text", # Output type
97
+ title="MoodLens – Service-Focused Sentiment Analysis Agent",
98
+ description="Moodlens is designed to evaluate the quality of service provided by the council. It looks directly at the content of the communication to identify service issues. For instance, when a resident reports a problem — such as a missing bin— Moodlens interprets that as a clear signal that something is wrong. Enter an email/chat to analyze its sentiment.",
99
+ examples=[
100
+ "The new Customer service portal is fantastic! The new images, categories and website data model is outstanding.",
101
+ "I had a horrible experience with the new council website upgrade. It keeps crashing and the customer support is unhelpful.",
102
+ "The public library had great ambiance but the food was mediocre at best.",
103
+ "I'm extremely dissatisfied with the coumcil services, contact center rep wasn't very friendly and wasn't enough knowledgeable. ",
104
+ "This was the worst customer service experience I've ever had.",
105
+ "Thanks for the prompt response! My complaint has been open for three days, and my SLA is already past due. Really impressive turnaround time, you guys are at least faster than a crippled snail. Thumbs up!",
106
+ "I'm extremely frustrated by the constant loud noises from my neighborhood—it's unbearable and needs urgent attention.",
107
+ "Bravo on the swift response! Three days later and my issue still hasn't been touched it’s amazing how you manage to turn every complaint into a prolonged waiting experience."
108
+ ]
109
+ )
110
+
111
+ # Launch the Gradio app
112
+ iface.launch()