Upload 12 files
Browse filesmodelasdfasdfsad
- sentimentAnalysis/.DS_Store +0 -0
- sentimentAnalysis/__pycache__/main.cpython-312.pyc +0 -0
- sentimentAnalysis/app/.DS_Store +0 -0
- sentimentAnalysis/app/__init__.py +0 -0
- sentimentAnalysis/app/__pycache__/__init__.cpython-312.pyc +0 -0
- sentimentAnalysis/app/__pycache__/sentiment_analysis.cpython-312.pyc +0 -0
- sentimentAnalysis/app/__pycache__/service.cpython-312.pyc +0 -0
- sentimentAnalysis/app/model (2).pkl +3 -0
- sentimentAnalysis/app/sentiment_analysis.py +66 -0
- sentimentAnalysis/app/service.py +13 -0
- sentimentAnalysis/app/tokenizer (2).pkl +3 -0
- sentimentAnalysis/main.py +13 -0
sentimentAnalysis/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
sentimentAnalysis/__pycache__/main.cpython-312.pyc
ADDED
|
Binary file (654 Bytes). View file
|
|
|
sentimentAnalysis/app/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
sentimentAnalysis/app/__init__.py
ADDED
|
File without changes
|
sentimentAnalysis/app/__pycache__/__init__.cpython-312.pyc
ADDED
|
Binary file (167 Bytes). View file
|
|
|
sentimentAnalysis/app/__pycache__/sentiment_analysis.cpython-312.pyc
ADDED
|
Binary file (3.21 kB). View file
|
|
|
sentimentAnalysis/app/__pycache__/service.cpython-312.pyc
ADDED
|
Binary file (893 Bytes). View file
|
|
|
sentimentAnalysis/app/model (2).pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bb112aa1b00439676911e46781a363e89c53908be22c829844b8ad5ae2dbd5a4
|
| 3 |
+
size 438041938
|
sentimentAnalysis/app/sentiment_analysis.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import joblib
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
import nltk
|
| 5 |
+
from nltk.tokenize import word_tokenize
|
| 6 |
+
from nltk.corpus import stopwords
|
| 7 |
+
from nltk.stem import PorterStemmer, WordNetLemmatizer
|
| 8 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 9 |
+
from sklearn.naive_bayes import MultinomialNB
|
| 10 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 11 |
+
import torch
|
| 12 |
+
|
| 13 |
+
nltk.download('punkt_tab')
|
| 14 |
+
nltk.download('stopwords')
|
| 15 |
+
nltk.download('wordnet')
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# Get the directory of this file
|
| 19 |
+
current_dir = os.path.dirname(os.path.abspath(__file__))
|
| 20 |
+
|
| 21 |
+
# Load the trained model and vectorizer
|
| 22 |
+
model_path = os.path.join(current_dir, "model (2).pkl")
|
| 23 |
+
vectorizer_path = os.path.join(current_dir, "tokenizer (2).pkl")
|
| 24 |
+
|
| 25 |
+
model = joblib.load(model_path)
|
| 26 |
+
vectorizer = joblib.load(vectorizer_path)
|
| 27 |
+
|
| 28 |
+
# Preprocessing function
|
| 29 |
+
def preprocess_text(text, use_stemming=False, use_lemmatization=True):
|
| 30 |
+
text = text.lower()
|
| 31 |
+
text = re.sub(r'\W', ' ', text)
|
| 32 |
+
words = word_tokenize(text)
|
| 33 |
+
|
| 34 |
+
stop_words = set(stopwords.words('english'))
|
| 35 |
+
stop_words.discard('not') # Keep 'not' for sentiment analysis
|
| 36 |
+
words = [word for word in words if word not in stop_words]
|
| 37 |
+
|
| 38 |
+
stemmer = PorterStemmer()
|
| 39 |
+
lemmatizer = WordNetLemmatizer()
|
| 40 |
+
|
| 41 |
+
if use_stemming:
|
| 42 |
+
words = [stemmer.stem(word) for word in words]
|
| 43 |
+
elif use_lemmatization:
|
| 44 |
+
words = [lemmatizer.lemmatize(word) for word in words]
|
| 45 |
+
|
| 46 |
+
return " ".join(words)
|
| 47 |
+
|
| 48 |
+
# Prediction function
|
| 49 |
+
def predict_sentiment(analyser):
|
| 50 |
+
"""Predicts sentiment using the trained BERT model."""
|
| 51 |
+
processed_text = preprocess_text(analyser.sentence) # ✅ Preprocess the text
|
| 52 |
+
|
| 53 |
+
# ✅ Tokenize input text (Replacing vectorizer.transform)
|
| 54 |
+
inputs = vectorizer(processed_text, truncation=True, padding="max_length", max_length=256, return_tensors="pt")
|
| 55 |
+
|
| 56 |
+
# ✅ Move inputs to the correct device
|
| 57 |
+
#inputs = {key: val.to(device) for key, val in inputs.items()}
|
| 58 |
+
|
| 59 |
+
# ✅ Get model prediction
|
| 60 |
+
with torch.no_grad():
|
| 61 |
+
outputs = model(**inputs)
|
| 62 |
+
prediction = torch.argmax(outputs.logits, dim=1).item()
|
| 63 |
+
|
| 64 |
+
# ✅ Convert prediction to sentiment label
|
| 65 |
+
sentiment_labels = ["Negative", "Neutral", "Positive"]
|
| 66 |
+
return sentiment_labels[prediction]
|
sentimentAnalysis/app/service.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import APIRouter
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from app.sentiment_analysis import predict_sentiment
|
| 4 |
+
|
| 5 |
+
class SentimentAnalyser(BaseModel):
|
| 6 |
+
sentence: str
|
| 7 |
+
|
| 8 |
+
router = APIRouter()
|
| 9 |
+
|
| 10 |
+
@router.post("/sentiment-analyser/post/")
|
| 11 |
+
async def create_grade(analyser: SentimentAnalyser):
|
| 12 |
+
result = predict_sentiment(analyser)
|
| 13 |
+
return {"sentiment": result}
|
sentimentAnalysis/app/tokenizer (2).pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:43338c2179e9acd6b91517313d27f6de37937464fda7f47d11c1eb2c6134839c
|
| 3 |
+
size 629587
|
sentimentAnalysis/main.py
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from app.service import router as sentiment_analysis_router
|
| 3 |
+
|
| 4 |
+
# Initialize FastAPI app
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Include both APIs
|
| 8 |
+
app.include_router(sentiment_analysis_router, prefix="/api", tags=["SentimentAnalyser"])
|
| 9 |
+
|
| 10 |
+
@app.get("/")
|
| 11 |
+
async def root():
|
| 12 |
+
return {"message": "Welcome to backend"}
|
| 13 |
+
|