ethanrom-a2 / app.py
ethanrom's picture
Update app.py
6487a45
raw
history blame
2.01 kB
import gradio as gr
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
import time
model_name = "ethanrom/a2"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
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":
inputs = tokenizer.encode_plus(text_input, return_tensors='pt')
start_time = time.time()
outputs = model(**inputs)
end_time = time.time()
logits = outputs.logits.detach().cpu().numpy()[0]
predicted_class = int(logits.argmax())
inference_time = end_time - start_time
model_size = model.num_parameters()
return candidate_labels[predicted_class], inference_time, model_size
else:
start_time = time.time()
result = pretrained_model(text_input, candidate_labels)
end_time = time.time()
predicted_class = result["labels"][0]
inference_time = end_time - start_time
model_size = pretrained_tokenizer.model_max_length + pretrained_model.model.num_parameters()
return candidate_labels[predicted_class], inference_time, model_size
inputs = [
gr.inputs.Textbox("Enter text"),
gr.inputs.Dropdown(["Pretrained", "Fine-tuned"], label="Select model"),
]
outputs = [
gr.outputs.Textbox(label="Predicted Sentiment"),
gr.outputs.Textbox(label="Inference Time (s)"),
gr.outputs.Textbox(label="Model Size (params)"),
]
gr.Interface(fn=predict_sentiment, inputs=inputs, outputs=outputs, title="Sentiment Analysis", description="Compare the output, inference time, and model size of two models").launch();