Spaces:
Runtime error
Runtime error
Abineshkumar77 commited on
Commit ·
642ed96
1
Parent(s): b33c4b3
Add application file
Browse files
app.py
CHANGED
|
@@ -1,9 +1,11 @@
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
-
from transformers import
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
| 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 |
-
#
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
}
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 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 |
+
}
|