Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- app.py +76 -0
- best_model.pth +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import T5Tokenizer, T5EncoderModel
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import re
|
| 6 |
+
import nltk
|
| 7 |
+
from nltk.tokenize import word_tokenize
|
| 8 |
+
from nltk.corpus import stopwords
|
| 9 |
+
from nltk.stem import WordNetLemmatizer
|
| 10 |
+
|
| 11 |
+
# === NLTK Downloads ===
|
| 12 |
+
nltk.download('punkt')
|
| 13 |
+
nltk.download('stopwords')
|
| 14 |
+
nltk.download('wordnet')
|
| 15 |
+
nltk.download('omw-1.4')
|
| 16 |
+
|
| 17 |
+
# === Preprocessing Function ===
|
| 18 |
+
stop_words = set(stopwords.words('english'))
|
| 19 |
+
lemmatizer = WordNetLemmatizer()
|
| 20 |
+
|
| 21 |
+
def preprocess_text(text):
|
| 22 |
+
text = re.sub(r'[^A-Za-z\s]', '', text)
|
| 23 |
+
text = re.sub(r'\s+', ' ', text).strip()
|
| 24 |
+
text = text.lower()
|
| 25 |
+
tokens = word_tokenize(text)
|
| 26 |
+
tokens = [word for word in tokens if word not in stop_words]
|
| 27 |
+
tokens = [lemmatizer.lemmatize(word) for word in tokens]
|
| 28 |
+
return ' '.join(tokens)
|
| 29 |
+
|
| 30 |
+
# === Model Definition ===
|
| 31 |
+
class T5Classifier(nn.Module):
|
| 32 |
+
def __init__(self, model_name='t5-small', num_labels=2):
|
| 33 |
+
super(T5Classifier, self).__init__()
|
| 34 |
+
self.encoder = T5EncoderModel.from_pretrained(model_name)
|
| 35 |
+
self.classifier = nn.Linear(self.encoder.config.d_model, num_labels)
|
| 36 |
+
|
| 37 |
+
def forward(self, input_ids, attention_mask):
|
| 38 |
+
encoder_output = self.encoder(input_ids=input_ids, attention_mask=attention_mask)
|
| 39 |
+
cls_representation = encoder_output.last_hidden_state[:, 0, :]
|
| 40 |
+
logits = self.classifier(cls_representation)
|
| 41 |
+
return logits
|
| 42 |
+
|
| 43 |
+
# === Load Model & Tokenizer ===
|
| 44 |
+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 45 |
+
tokenizer = T5Tokenizer.from_pretrained("t5-small")
|
| 46 |
+
|
| 47 |
+
model = T5Classifier(model_name='t5-small', num_labels=2)
|
| 48 |
+
model.load_state_dict(torch.load("best_model.pth", map_location=device))
|
| 49 |
+
model.to(device)
|
| 50 |
+
model.eval()
|
| 51 |
+
|
| 52 |
+
# === Prediction Function ===
|
| 53 |
+
label_map = {0: "Negative", 1: "Positive"}
|
| 54 |
+
|
| 55 |
+
def predict_sentiment(text):
|
| 56 |
+
cleaned = preprocess_text(text)
|
| 57 |
+
inputs = tokenizer(cleaned, return_tensors="pt", padding="max_length", truncation=True, max_length=128)
|
| 58 |
+
input_ids = inputs["input_ids"].to(device)
|
| 59 |
+
attention_mask = inputs["attention_mask"].to(device)
|
| 60 |
+
|
| 61 |
+
with torch.no_grad():
|
| 62 |
+
logits = model(input_ids=input_ids, attention_mask=attention_mask)
|
| 63 |
+
pred = torch.argmax(logits, dim=1).item()
|
| 64 |
+
|
| 65 |
+
return label_map[pred]
|
| 66 |
+
|
| 67 |
+
# === Gradio Interface ===
|
| 68 |
+
demo = gr.Interface(
|
| 69 |
+
fn=predict_sentiment,
|
| 70 |
+
inputs=gr.Textbox(label="Enter Movie Review"),
|
| 71 |
+
outputs=gr.Text(label="Predicted Sentiment"),
|
| 72 |
+
title="🎬 T5 Movie Review Classifier",
|
| 73 |
+
description="Enter a movie review, and the model will predict whether the sentiment is Positive or Negative."
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
demo.launch()
|
best_model.pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8a008c909b7fdb52a57fc29896f7035ec95c40bd4826ff1ac87a42f19c672140
|
| 3 |
+
size 141353634
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch>=2.0.0
|
| 2 |
+
transformers>=4.40.0
|
| 3 |
+
nltk>=3.8.1
|
| 4 |
+
gradio>=4.0.0
|