Abineshkumar77 commited on
Commit
c5fdd87
·
1 Parent(s): b9ca7c2

Add application file

Browse files
Files changed (2) hide show
  1. app.py +26 -29
  2. requirements.txt +2 -5
app.py CHANGED
@@ -1,17 +1,19 @@
1
  from fastapi import FastAPI
2
- import onnxruntime as ort
3
- import numpy as np
 
 
4
  import time
5
- from transformers import RobertaTokenizer
6
 
7
  app = FastAPI()
8
 
9
- # Load the ONNX model
10
- onnx_model_path = "sentiment_model.onnx"
11
- session = ort.InferenceSession(onnx_model_path)
12
 
13
- # Initialize the tokenizer
14
- tokenizer = RobertaTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
 
15
 
16
  def preprocess_tweet(tweet: str) -> str:
17
  tweet_words = []
@@ -29,36 +31,31 @@ def home():
29
 
30
  @app.get("/analyze")
31
  def analyze_sentiment(tweet: str):
 
32
  tweet_proc = preprocess_tweet(tweet)
33
- inputs = tokenizer(tweet_proc, return_tensors="np")
34
-
35
  # Measure the time taken for the inference
36
  start_time = time.time()
37
 
38
- # Perform inference with ONNX
39
- ort_inputs = {k: v for k, v in inputs.items()}
40
- ort_outputs = session.run(None, ort_inputs)
41
-
42
- # Extract the output
43
- logits = ort_outputs[0]
44
 
45
- # Apply softmax to get probabilities
46
- probs = np.exp(logits) / np.sum(np.exp(logits), axis=1, keepdims=True)
47
-
48
- # Get the predicted label
49
  label_map = {
50
- 0: "Negative",
51
- 1: "Neutral",
52
- 2: "Positive"
53
  }
54
-
55
- highest_score_index = np.argmax(probs[0])
56
- highest_label = label_map[highest_score_index]
57
- highest_score = round(probs[0][highest_score_index], 4)
58
 
59
- # Calculate the inference time
60
- inference_time = time.time() - start_time
 
 
61
 
 
62
  return {
63
  "text": tweet,
64
  "label": highest_label,
 
1
  from fastapi import FastAPI
2
+ from transformers import AutoTokenizer
3
+ from optimum.onnxruntime import ORTModelForSequenceClassification
4
+ from optimum.onnxruntime import ORTQuantizer
5
+ from optimum.onnxruntime.configuration import AutoQuantizationConfig
6
  import time
 
7
 
8
  app = FastAPI()
9
 
10
+ # Load the ONNX model and tokenizer
11
+ model_path = "./model_onnx/model_quantized.onnx"
12
+ tokenizer_path = "./model_onnx"
13
 
14
+ model = ORTModelForSequenceClassification.from_pretrained(model_path)
15
+ tokenizer = AutoTokenizer.from_pretrained(tokenizer_path)
16
+ pipe = pipeline("text-classification", model=model, tokenizer=tokenizer)
17
 
18
  def preprocess_tweet(tweet: str) -> str:
19
  tweet_words = []
 
31
 
32
  @app.get("/analyze")
33
  def analyze_sentiment(tweet: str):
34
+ # Preprocess the tweet
35
  tweet_proc = preprocess_tweet(tweet)
36
+
 
37
  # Measure the time taken for the inference
38
  start_time = time.time()
39
 
40
+ # Use the pipeline to get the sentiment analysis result
41
+ results = pipe(tweet_proc, return_all_scores=True)
 
 
 
 
42
 
43
+ # Calculate the inference time
44
+ inference_time = time.time() - start_time
45
+
46
+ # Map the labels to desired names
47
  label_map = {
48
+ "LABEL_0": "Negative",
49
+ "LABEL_1": "Neutral",
50
+ "LABEL_2": "Positive"
51
  }
 
 
 
 
52
 
53
+ # Find the label with the highest score
54
+ highest_score_result = max(results[0], key=lambda x: x['score'])
55
+ highest_label = label_map[highest_score_result['label']]
56
+ highest_score = round(highest_score_result['score'], 4)
57
 
58
+ # Return the original tweet, the label with the highest score, and the inference time
59
  return {
60
  "text": tweet,
61
  "label": highest_label,
requirements.txt CHANGED
@@ -3,10 +3,7 @@ uvicorn
3
  transformers
4
  torch
5
  scipy
6
- fastapi==0.99.0
7
- onnxruntime==1.14.0
8
- transformers==4.33.0
9
- numpy==1.25.2
10
- uvicorn==0.23.1
11
 
12
 
 
3
  transformers
4
  torch
5
  scipy
6
+ optimum
7
+ onnxruntime
 
 
 
8
 
9