Abineshkumar77 commited on
Commit
642ed96
·
1 Parent(s): b33c4b3

Add application file

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -1,9 +1,11 @@
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
 
@@ -28,24 +30,33 @@ def analyze_sentiment(tweet: str):
28
 
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 {
@@ -53,4 +64,4 @@ def analyze_sentiment(tweet: str):
53
  "label": highest_label,
54
  "score": highest_score,
55
  "inference_time": round(inference_time, 4) # In seconds
56
- }
 
1
  from fastapi import FastAPI
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
+ import torch
4
  import time
5
 
6
+ # Load the tokenizer and model directly
7
+ tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
8
+ model = AutoModelForSequenceClassification.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
9
 
10
  app = FastAPI()
11
 
 
30
 
31
  # Measure the time taken for the inference
32
  start_time = time.time()
33
+
34
+ # Tokenize the input tweet
35
+ inputs = tokenizer(tweet_proc, return_tensors="pt")
36
+
37
+ # Perform the inference
38
+ with torch.no_grad():
39
+ outputs = model(**inputs)
40
 
41
  # Calculate the inference time
42
  inference_time = time.time() - start_time
43
 
44
+ # Get the probabilities from the logits
45
+ probabilities = torch.softmax(outputs.logits, dim=1)
46
+
47
+ # Get the label with the highest probability
48
+ max_prob, max_index = torch.max(probabilities, dim=1)
49
+
50
  # Map the labels to desired names
51
  label_map = {
52
+ 0: "Negative",
53
+ 1: "Neutral",
54
+ 2: "Positive"
55
  }
56
 
57
+ # Get the highest label and its corresponding score
58
+ highest_label = label_map[max_index.item()]
59
+ highest_score = round(max_prob.item(), 4)
 
60
 
61
  # Return the original tweet, the label with the highest score, and the inference time
62
  return {
 
64
  "label": highest_label,
65
  "score": highest_score,
66
  "inference_time": round(inference_time, 4) # In seconds
67
+ }