Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from transformers import AutoModelForSequenceClassification, AutoTokenizer, Auto
|
|
| 4 |
from scipy.special import softmax
|
| 5 |
|
| 6 |
# Load fine-tuned model and tokenizer
|
| 7 |
-
MODEL_PATH = "ktr008/sentiment"
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 10 |
config = AutoConfig.from_pretrained(MODEL_PATH)
|
|
@@ -26,16 +26,17 @@ def predict_sentiment(text):
|
|
| 26 |
scores = output[0][0].detach().numpy()
|
| 27 |
scores = softmax(scores)
|
| 28 |
|
| 29 |
-
# Get sentiment labels and scores
|
| 30 |
ranking = np.argsort(scores)[::-1]
|
| 31 |
-
result = {config.id2label[ranking[i]]:
|
|
|
|
| 32 |
return result
|
| 33 |
|
| 34 |
# Gradio Interface
|
| 35 |
interface = gr.Interface(
|
| 36 |
fn=predict_sentiment,
|
| 37 |
inputs=gr.Textbox(lines=3, placeholder="Enter text..."),
|
| 38 |
-
outputs=gr.
|
| 39 |
title="Fine-Tuned Sentiment Analysis",
|
| 40 |
description="Enter a sentence to analyze its sentiment (Positive, Neutral, Negative).",
|
| 41 |
)
|
|
|
|
| 4 |
from scipy.special import softmax
|
| 5 |
|
| 6 |
# Load fine-tuned model and tokenizer
|
| 7 |
+
MODEL_PATH = "ktr008/sentiment"
|
| 8 |
model = AutoModelForSequenceClassification.from_pretrained(MODEL_PATH)
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 10 |
config = AutoConfig.from_pretrained(MODEL_PATH)
|
|
|
|
| 26 |
scores = output[0][0].detach().numpy()
|
| 27 |
scores = softmax(scores)
|
| 28 |
|
| 29 |
+
# Get sentiment labels and scores (floating-point values)
|
| 30 |
ranking = np.argsort(scores)[::-1]
|
| 31 |
+
result = {config.id2label[ranking[i]]: float(scores[ranking[i]]) for i in range(scores.shape[0])}
|
| 32 |
+
|
| 33 |
return result
|
| 34 |
|
| 35 |
# Gradio Interface
|
| 36 |
interface = gr.Interface(
|
| 37 |
fn=predict_sentiment,
|
| 38 |
inputs=gr.Textbox(lines=3, placeholder="Enter text..."),
|
| 39 |
+
outputs=gr.JSON(),
|
| 40 |
title="Fine-Tuned Sentiment Analysis",
|
| 41 |
description="Enter a sentence to analyze its sentiment (Positive, Neutral, Negative).",
|
| 42 |
)
|