Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,11 @@ MODEL_ID = "edaUsha/Fine_Tuning_Bert_For_Sentiment_Anaysis"
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 9 |
|
| 10 |
-
id2label = {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def predict_sentiment(text):
|
| 13 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
@@ -18,15 +22,4 @@ def predict_sentiment(text):
|
|
| 18 |
pred_id = int(torch.argmax(probs))
|
| 19 |
label = id2label[pred_id]
|
| 20 |
confidence = float(probs[pred_id])
|
| 21 |
-
return f"{label} ({confidence:.2f})"
|
| 22 |
-
|
| 23 |
-
demo = gr.Interface(
|
| 24 |
-
fn=predict_sentiment,
|
| 25 |
-
inputs=gr.Textbox(lines=3, label="Input text"),
|
| 26 |
-
outputs=gr.Textbox(label="Prediction"),
|
| 27 |
-
title="Fine-tuned BERT Sentiment Analysis",
|
| 28 |
-
description="Enter a sentence to see its predicted sentiment."
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
if __name__ == "__main__":
|
| 32 |
-
demo.launch()
|
|
|
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_ID)
|
| 9 |
|
| 10 |
+
id2label = {
|
| 11 |
+
0: "NEGATIVE", # change to your actual label name
|
| 12 |
+
1: "NEUTRAL", # change if needed
|
| 13 |
+
2: "POSITIVE" # change if needed
|
| 14 |
+
}
|
| 15 |
|
| 16 |
def predict_sentiment(text):
|
| 17 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
| 22 |
pred_id = int(torch.argmax(probs))
|
| 23 |
label = id2label[pred_id]
|
| 24 |
confidence = float(probs[pred_id])
|
| 25 |
+
return f"{label} ({confidence:.2f})"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|