Spaces:
Runtime error
Runtime error
Abineshkumar77 commited on
Commit ·
c306328
1
Parent(s): 6c4ef78
Add application file
Browse files
app.py
CHANGED
|
@@ -1,11 +1,18 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def preprocess_tweet(tweet: str) -> str:
|
| 11 |
tweet_words = []
|
|
@@ -17,35 +24,48 @@ def preprocess_tweet(tweet: str) -> str:
|
|
| 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"}
|
| 23 |
|
| 24 |
@app.get("/analyze")
|
| 25 |
-
def analyze_sentiment(tweet: str):
|
| 26 |
# Preprocess the tweet
|
| 27 |
tweet_proc = preprocess_tweet(tweet)
|
| 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 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
#
|
| 46 |
-
|
| 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 +73,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, Query
|
| 2 |
+
from transformers import pipeline, AutoTokenizer
|
| 3 |
+
import onnxruntime as ort
|
| 4 |
+
import numpy as np
|
| 5 |
import time
|
| 6 |
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
# Initialize the Hugging Face pipeline for sentiment analysis
|
| 10 |
pipe = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-sentiment")
|
| 11 |
|
| 12 |
+
# Load the ONNX model and tokenizer
|
| 13 |
+
onnx_model_path = "sentiment_model.onnx"
|
| 14 |
+
session = ort.InferenceSession(onnx_model_path)
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained("cardiffnlp/twitter-roberta-base-sentiment")
|
| 16 |
|
| 17 |
def preprocess_tweet(tweet: str) -> str:
|
| 18 |
tweet_words = []
|
|
|
|
| 24 |
tweet_words.append(word)
|
| 25 |
return " ".join(tweet_words)
|
| 26 |
|
| 27 |
+
def run_inference_onnx(tweet: str):
|
| 28 |
+
inputs = tokenizer(tweet, return_tensors="np", padding=True, truncation=True)
|
| 29 |
+
ort_inputs = {k: v for k, v in inputs.items()}
|
| 30 |
+
ort_outs = session.run(None, ort_inputs)
|
| 31 |
+
return ort_outs[0]
|
| 32 |
+
|
| 33 |
@app.get("/")
|
| 34 |
def home():
|
| 35 |
return {"message": "Welcome to the sentiment analysis API"}
|
| 36 |
|
| 37 |
@app.get("/analyze")
|
| 38 |
+
def analyze_sentiment(tweet: str, method: str = Query("pipeline", enum=["pipeline", "onnx"])):
|
| 39 |
# Preprocess the tweet
|
| 40 |
tweet_proc = preprocess_tweet(tweet)
|
| 41 |
|
| 42 |
# Measure the time taken for the inference
|
| 43 |
start_time = time.time()
|
| 44 |
|
| 45 |
+
if method == "pipeline":
|
| 46 |
+
# Use the Hugging Face pipeline to get the sentiment analysis result
|
| 47 |
+
results = pipe(tweet_proc, return_all_scores=True)
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# Find the label with the highest score
|
| 50 |
+
highest_score_result = max(results[0], key=lambda x: x['score'])
|
| 51 |
+
label_map = {
|
| 52 |
+
"LABEL_0": "Negative",
|
| 53 |
+
"LABEL_1": "Neutral",
|
| 54 |
+
"LABEL_2": "Positive"
|
| 55 |
+
}
|
| 56 |
+
highest_label = label_map[highest_score_result['label']]
|
| 57 |
+
highest_score = round(highest_score_result['score'], 4)
|
| 58 |
+
|
| 59 |
+
elif method == "onnx":
|
| 60 |
+
# Run inference using the ONNX model
|
| 61 |
+
logits = run_inference_onnx(tweet_proc)
|
| 62 |
+
label_map = ["Negative", "Neutral", "Positive"]
|
| 63 |
+
highest_label_idx = np.argmax(logits)
|
| 64 |
+
highest_label = label_map[highest_label_idx]
|
| 65 |
+
highest_score = round(float(np.max(logits)), 4)
|
| 66 |
|
| 67 |
+
# Calculate the inference time
|
| 68 |
+
inference_time = time.time() - start_time
|
|
|
|
|
|
|
| 69 |
|
| 70 |
# Return the original tweet, the label with the highest score, and the inference time
|
| 71 |
return {
|
|
|
|
| 73 |
"label": highest_label,
|
| 74 |
"score": highest_score,
|
| 75 |
"inference_time": round(inference_time, 4) # In seconds
|
| 76 |
+
}
|