Spaces:
Runtime error
Runtime error
File size: 1,991 Bytes
0f1c341 aaf9b07 cbb33a8 aaf9b07 e1eeba3 65c729a e1eeba3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
# Load the fine-tuned model and tokenizer
model_name = "ethanrom/a2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
# Load the pretrained model and tokenizer
pretrained_model_name = "roberta-large-mnli"
pretrained_tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name)
pretrained_model = pipeline("zero-shot-classification", model=pretrained_model_name, tokenizer=pretrained_tokenizer)
candidate_labels = ["negative", "positive", "no impact", "mixed"]
def predict_sentiment(text_input, model_selection):
if model_selection == "Fine-tuned":
# Use the fine-tuned model
inputs = tokenizer.encode_plus(text_input, return_tensors='pt')
outputs = model(**inputs)
logits = outputs.logits.detach().cpu().numpy()[0]
predicted_class = int(logits.argmax())
return candidate_labels[predicted_class]
else:
# Use the pretrained model
result = pretrained_model(text_input, candidate_labels)
predicted_class = result["labels"][0]
return predicted_class
inputs = [
gr.inputs.Textbox("Enter text"),
gr.inputs.Dropdown(["Pretrained", "Fine-tuned"], label="Select model"),
]
outputs = gr.outputs.Textbox(label="Predicted Sentiment")
gr.Interface(fn=predict_sentiment, inputs=inputs, outputs=outputs, title="Sentiment Analysis", description="Compare the output of two models", examples=[
["max laid his hand upon the old man's arm", "Pretrained Model"],
["the red sword sealed their vows!", "Fine-tuned Model"],
["and that is why, the lonesome day,", "Pretrained Model"],
["it flows so long as falls the rain", "Fine-tuned Model"],
["thy hands all cunning arts that women prize", "Pretrained Model"],
["on us lift up the light", "Fine-tuned Model"],
],).launch();
|