Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,37 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, pipeline, AutoModelForSequenceClassification
|
| 3 |
|
| 4 |
+
# load the tokenizer and model from Hugging Face
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained("ethanrom/a2")
|
| 6 |
+
model = AutoModelForSequenceClassification.from_pretrained("ethanrom/a2")
|
| 7 |
+
|
| 8 |
+
# define the classification labels
|
| 9 |
+
class_labels = ["Negative", "Positive", "No Impact", "Mixed"]
|
| 10 |
+
|
| 11 |
+
# create the zero-shot classification pipeline
|
| 12 |
+
classifier = pipeline("zero-shot-classification", model=model, tokenizer=tokenizer, device=0)
|
| 13 |
+
|
| 14 |
+
# define the Gradio interface
|
| 15 |
+
def predict_sentiment(text, model_choice):
|
| 16 |
+
if model_choice == "bert":
|
| 17 |
+
# use the default BERT sentiment analysis pipeline
|
| 18 |
+
sentiment_classifier = pipeline("sentiment-analysis", device=0)
|
| 19 |
+
result = sentiment_classifier(text)[0]
|
| 20 |
+
label = result["label"]
|
| 21 |
+
score = result["score"]
|
| 22 |
+
return f"{label} ({score:.2f})"
|
| 23 |
+
else:
|
| 24 |
+
# use the fine-tuned RoBERTa model for multi-class classification
|
| 25 |
+
labels = class_labels
|
| 26 |
+
hypothesis_template = "This text is about {}."
|
| 27 |
+
result = classifier(text, hypothesis_template=hypothesis_template, multi_class=True, labels=labels)
|
| 28 |
+
scores = result["scores"]
|
| 29 |
+
predicted_label = result["labels"][0]
|
| 30 |
+
return f"{predicted_label} ({scores[0]:.2f})"
|
| 31 |
+
|
| 32 |
+
# define the Gradio interface inputs and outputs
|
| 33 |
+
inputs = [gr.inputs.Textbox(label="Input Text"), gr.inputs.Radio(["bert", "fine-tuned RoBERTa"], label="Model Choice")]
|
| 34 |
+
outputs = gr.outputs.Textbox(label="Sentiment Prediction")
|
| 35 |
+
|
| 36 |
+
# create the Gradio interface
|
| 37 |
+
gr.Interface(predict_sentiment, inputs, outputs, title="Sentiment Analysis App").launch()
|