Commit
·
c5182bf
1
Parent(s):
edbe376
feat: Added application file
Browse files- src/api.py +26 -1
src/api.py
CHANGED
|
@@ -2,8 +2,33 @@ import configparser
|
|
| 2 |
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from SentimentAndIntentionAnalysis import ZeroShotClassifier
|
| 6 |
from data_loader import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Initialize FastAPI app
|
| 9 |
app = FastAPI()
|
|
|
|
| 2 |
|
| 3 |
from fastapi import FastAPI
|
| 4 |
from pydantic import BaseModel
|
|
|
|
| 5 |
from data_loader import *
|
| 6 |
+
from transformers import pipeline, BartTokenizer, BartForSequenceClassification
|
| 7 |
+
class ZeroShotClassifier:
|
| 8 |
+
|
| 9 |
+
def __init__(self, model_name, sentiment_labels, intention_labels):
|
| 10 |
+
self.model = self.create_model(model_name)
|
| 11 |
+
self.model_name = model_name
|
| 12 |
+
self.sentiment_labels = sentiment_labels
|
| 13 |
+
self.intention_labels = intention_labels
|
| 14 |
+
|
| 15 |
+
def create_model(self, model_name):
|
| 16 |
+
# Create Model
|
| 17 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 18 |
+
model = BartForSequenceClassification.from_pretrained(model_name)
|
| 19 |
+
classifier = pipeline("zero-shot-classification", model=model, tokenizer=tokenizer)
|
| 20 |
+
return classifier
|
| 21 |
+
|
| 22 |
+
def analyze_text(self, text):
|
| 23 |
+
# Sentiment analysis
|
| 24 |
+
sentiment_result = self.model(text, self.sentiment_labels)
|
| 25 |
+
sentiment = sentiment_result["labels"][0]
|
| 26 |
+
|
| 27 |
+
# Intention analysis
|
| 28 |
+
intention_result = self.model(text, self.intention_labels)
|
| 29 |
+
intention = intention_result["labels"][0]
|
| 30 |
+
|
| 31 |
+
return {"sentiment": sentiment, "intention": intention}
|
| 32 |
|
| 33 |
# Initialize FastAPI app
|
| 34 |
app = FastAPI()
|