Abineshkumar77 commited on
Commit
32659b4
·
1 Parent(s): f4b7a2b

Add application file

Browse files
Files changed (2) hide show
  1. app.py +20 -25
  2. requirements.txt +3 -1
app.py CHANGED
@@ -1,15 +1,9 @@
1
- import onnxruntime as ort
2
- from transformers import AutoTokenizer
3
- import numpy as np
4
- import time
5
  from fastapi import FastAPI
 
 
6
 
7
- # Load the ONNX model
8
- onnx_model_path = "model_quantized.onnx"
9
- session = ort.InferenceSession(onnx_model_path)
10
-
11
- # Load the tokenizer
12
- tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
13
 
14
  app = FastAPI()
15
 
@@ -23,12 +17,6 @@ def preprocess_tweet(tweet: str) -> str:
23
  tweet_words.append(word)
24
  return " ".join(tweet_words)
25
 
26
- def run_inference(tweet: str):
27
- inputs = tokenizer(tweet, return_tensors="np", padding=True, truncation=True)
28
- ort_inputs = {k: v for k, v in inputs.items()}
29
- ort_outs = session.run(None, ort_inputs)
30
- return ort_outs[0]
31
-
32
  @app.get("/")
33
  def home():
34
  return {"message": "Welcome to the sentiment analysis API"}
@@ -41,21 +29,28 @@ def analyze_sentiment(tweet: str):
41
  # Measure the time taken for the inference
42
  start_time = time.time()
43
 
44
- # Run inference using ONNX model
45
- logits = run_inference(tweet_proc)
46
-
47
  # Calculate the inference time
48
  inference_time = time.time() - start_time
49
 
 
 
 
 
 
 
 
50
  # Find the label with the highest score
51
- label_map = ["Negative", "Neutral", "Positive"]
52
- highest_label_idx = np.argmax(logits)
53
- highest_score = np.max(logits)
54
 
55
  # Return the original tweet, the label with the highest score, and the inference time
56
  return {
57
  "text": tweet,
58
- "label": label_map[highest_label_idx],
59
- "score": round(float(highest_score), 4),
60
  "inference_time": round(inference_time, 4) # In seconds
61
- }
 
 
 
 
 
1
  from fastapi import FastAPI
2
+ from transformers import pipeline
3
+ import time
4
 
5
+ # Initialize the sentiment analysis pipeline
6
+ pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
 
 
 
 
7
 
8
  app = FastAPI()
9
 
 
17
  tweet_words.append(word)
18
  return " ".join(tweet_words)
19
 
 
 
 
 
 
 
20
  @app.get("/")
21
  def home():
22
  return {"message": "Welcome to the sentiment analysis API"}
 
29
  # Measure the time taken for the inference
30
  start_time = time.time()
31
 
32
+ # Use the pipeline to get the sentiment analysis result
33
+ results = pipe(tweet_proc, return_all_scores=True)
34
+
35
  # Calculate the inference time
36
  inference_time = time.time() - start_time
37
 
38
+ # Map the labels to desired names
39
+ label_map = {
40
+ "LABEL_0": "Negative",
41
+ "LABEL_1": "Neutral",
42
+ "LABEL_2": "Positive"
43
+ }
44
+
45
  # Find the label with the highest score
46
+ highest_score_result = max(results[0], key=lambda x: x['score'])
47
+ highest_label = label_map[highest_score_result['label']]
48
+ highest_score = round(highest_score_result['score'], 4)
49
 
50
  # Return the original tweet, the label with the highest score, and the inference time
51
  return {
52
  "text": tweet,
53
+ "label": highest_label,
54
+ "score": highest_score,
55
  "inference_time": round(inference_time, 4) # In seconds
56
+ }
requirements.txt CHANGED
@@ -5,6 +5,8 @@ torch
5
  scipy
6
  onnx
7
  onnxruntime
8
-
 
 
9
 
10
 
 
5
  scipy
6
  onnx
7
  onnxruntime
8
+ onnxruntime-gpu
9
+ numpy
10
+ time
11
 
12