Update app.py
Browse files
app.py
CHANGED
@@ -1,12 +1,23 @@
|
|
1 |
-
|
|
|
2 |
|
3 |
-
|
4 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
-
|
9 |
-
inputs = tokenizer(text, return_tensors="pt")
|
10 |
|
11 |
-
|
12 |
-
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
3 |
|
4 |
+
# Load pre-trained tokenizer and model
|
5 |
+
tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased')
|
6 |
+
model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2)
|
7 |
|
8 |
+
def predict_sentiment(input_text):
|
9 |
+
# Tokenization
|
10 |
+
inputs = tokenizer(input_text, return_tensors='pt')
|
11 |
+
|
12 |
+
# Prediction
|
13 |
+
outputs = model(**inputs)
|
14 |
+
probabilities = outputs[0][0].detach().numpy()
|
15 |
+
labels = ['Negative', 'Positive']
|
16 |
+
predicted_label = labels[probabilities.argmax()]
|
17 |
|
18 |
+
return {"Text": input_text, "Sentiment": predicted_label}
|
|
|
19 |
|
20 |
+
iface = gr.Interface(predict_sentiment, input_type="text", output_types=["text"],
|
21 |
+
input_label="Enter Text", output_label="Predicted Sentiment")
|
22 |
+
|
23 |
+
iface.launch()
|