iimran commited on
Commit
f4e0a55
·
verified ·
1 Parent(s): a549729

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -16
app.py CHANGED
@@ -12,20 +12,12 @@ class ONNXInferencePipeline:
12
  self.onnx_path = hf_hub_download(repo_id=repo_id, filename="mindmeter.onnx")
13
  self.tokenizer_path = hf_hub_download(repo_id=repo_id, filename="train_bpe_tokenizer.json")
14
  self.config_path = hf_hub_download(repo_id=repo_id, filename="hyperparameters.json")
15
-
16
- # Load configuration from hyperparameters.json.
17
- # This file should have a key "MAX_LEN" specifying the maximum token sequence length.
18
  with open(self.config_path, "r") as f:
19
  self.config = json.load(f)
20
 
21
- # Initialize the tokenizer from file.
22
  self.tokenizer = Tokenizer.from_file(self.tokenizer_path)
23
- # Use the maximum sequence length from the configuration.
24
  self.max_len = self.config["MAX_LEN"]
25
-
26
- # Initialize the ONNX runtime session.
27
  self.session = ort.InferenceSession(self.onnx_path)
28
- # Use CUDA if available, otherwise default to CPU.
29
  self.providers = ['CPUExecutionProvider']
30
  if 'CUDAExecutionProvider' in ort.get_available_providers():
31
  self.providers = ['CUDAExecutionProvider']
@@ -54,24 +46,19 @@ class ONNXInferencePipeline:
54
  outputs = self.session.run(None, {"input": input_array})
55
  logits = outputs[0]
56
 
57
- # Compute softmax probabilities from the logits.
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
 
62
- # Map the predicted index to a label.
63
  class_labels = ["Not Stressed", "Stressed"]
64
  predicted_label = class_labels[predicted_class]
65
 
66
- # Return only the predicted stress label.
67
  return {"stress_level": predicted_label}
68
 
69
 
70
  if __name__ == "__main__":
71
- # Initialize the pipeline with the Hugging Face repository ID.
72
  pipeline = ONNXInferencePipeline(repo_id="iimran/MindMeter")
73
 
74
- # Example input texts for local testing.
75
  text1 = "Yay! what a happy life"
76
  text2 = "I’ve missed another loan payment, and I don’t know how I’m going to catch up. The pressure is unbearable."
77
  text3 = "I am upset about how badly life is trating me these days, its shit."
@@ -84,12 +71,10 @@ if __name__ == "__main__":
84
  print(f"Prediction for text 2: {result2}")
85
  print(f"Prediction for text 3: {result3}")
86
 
87
- # Define a function for Gradio to use.
88
  def gradio_predict(text):
89
  result = pipeline.predict(text)
90
  return result["stress_level"]
91
 
92
- # Create the Gradio interface.
93
  iface = gr.Interface(
94
  fn=gradio_predict,
95
  inputs=gr.Textbox(lines=7, placeholder="Enter your text here..."),
@@ -106,5 +91,4 @@ if __name__ == "__main__":
106
  ]
107
  )
108
 
109
- # Launch the Gradio app.
110
  iface.launch()
 
12
  self.onnx_path = hf_hub_download(repo_id=repo_id, filename="mindmeter.onnx")
13
  self.tokenizer_path = hf_hub_download(repo_id=repo_id, filename="train_bpe_tokenizer.json")
14
  self.config_path = hf_hub_download(repo_id=repo_id, filename="hyperparameters.json")
 
 
 
15
  with open(self.config_path, "r") as f:
16
  self.config = json.load(f)
17
 
 
18
  self.tokenizer = Tokenizer.from_file(self.tokenizer_path)
 
19
  self.max_len = self.config["MAX_LEN"]
 
 
20
  self.session = ort.InferenceSession(self.onnx_path)
 
21
  self.providers = ['CPUExecutionProvider']
22
  if 'CUDAExecutionProvider' in ort.get_available_providers():
23
  self.providers = ['CUDAExecutionProvider']
 
46
  outputs = self.session.run(None, {"input": input_array})
47
  logits = outputs[0]
48
 
 
49
  exp_logits = np.exp(logits)
50
  probabilities = exp_logits / np.sum(exp_logits, axis=1, keepdims=True)
51
  predicted_class = int(np.argmax(probabilities))
52
 
 
53
  class_labels = ["Not Stressed", "Stressed"]
54
  predicted_label = class_labels[predicted_class]
55
 
 
56
  return {"stress_level": predicted_label}
57
 
58
 
59
  if __name__ == "__main__":
 
60
  pipeline = ONNXInferencePipeline(repo_id="iimran/MindMeter")
61
 
 
62
  text1 = "Yay! what a happy life"
63
  text2 = "I’ve missed another loan payment, and I don’t know how I’m going to catch up. The pressure is unbearable."
64
  text3 = "I am upset about how badly life is trating me these days, its shit."
 
71
  print(f"Prediction for text 2: {result2}")
72
  print(f"Prediction for text 3: {result3}")
73
 
 
74
  def gradio_predict(text):
75
  result = pipeline.predict(text)
76
  return result["stress_level"]
77
 
 
78
  iface = gr.Interface(
79
  fn=gradio_predict,
80
  inputs=gr.Textbox(lines=7, placeholder="Enter your text here..."),
 
91
  ]
92
  )
93
 
 
94
  iface.launch()