Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
-
import time
|
3 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
|
|
4 |
|
5 |
model_name = "ethanrom/a2"
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
@@ -11,39 +11,26 @@ pretrained_tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name)
|
|
11 |
pretrained_model = pipeline("zero-shot-classification", model=pretrained_model_name, tokenizer=pretrained_tokenizer)
|
12 |
candidate_labels = ["negative", "positive", "no impact", "mixed"]
|
13 |
|
14 |
-
accuracy_scores = {"Fine-tuned": 0.0, "Pretrained": 0.0}
|
15 |
-
model_sizes = {"Fine-tuned": 0, "Pretrained": 0}
|
16 |
-
inference_times = {"Fine-tuned": 0.0, "Pretrained": 0.0}
|
17 |
|
18 |
def predict_sentiment(text_input, model_selection):
|
19 |
-
global accuracy_scores, model_sizes, inference_times
|
20 |
-
|
21 |
-
start_time = time.time()
|
22 |
if model_selection == "Fine-tuned":
|
23 |
inputs = tokenizer.encode_plus(text_input, return_tensors='pt')
|
|
|
24 |
outputs = model(**inputs)
|
|
|
25 |
logits = outputs.logits.detach().cpu().numpy()[0]
|
26 |
predicted_class = int(logits.argmax())
|
27 |
-
|
28 |
-
|
|
|
29 |
else:
|
|
|
30 |
result = pretrained_model(text_input, candidate_labels)
|
|
|
31 |
predicted_class = result["labels"][0]
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
inference_times[model_selection] = end_time - start_time
|
36 |
-
|
37 |
-
return candidate_labels[predicted_class]
|
38 |
-
|
39 |
-
def accuracy(model_selection):
|
40 |
-
return accuracy_scores[model_selection]/10
|
41 |
-
|
42 |
-
def model_size(model_selection):
|
43 |
-
return str(model_sizes[model_selection]//(1024*1024)) + " MB"
|
44 |
-
|
45 |
-
def inference_time(model_selection):
|
46 |
-
return str(inference_times[model_selection]*1000) + " ms"
|
47 |
|
48 |
inputs = [
|
49 |
gr.inputs.Textbox("Enter text"),
|
@@ -52,13 +39,8 @@ inputs = [
|
|
52 |
|
53 |
outputs = [
|
54 |
gr.outputs.Textbox(label="Predicted Sentiment"),
|
55 |
-
gr.outputs.
|
56 |
-
gr.outputs.
|
57 |
-
gr.outputs.Label(label="Inference Time:")
|
58 |
]
|
59 |
|
60 |
-
gr.Interface(fn=predict_sentiment, inputs=inputs, outputs=outputs,
|
61 |
-
title="Sentiment Analysis", description="Compare the output of two models",
|
62 |
-
live=True,
|
63 |
-
examples=[["on us lift up the light", "Fine-tuned"], ["max laid his hand upon the old man's arm", "Pretrained"]]
|
64 |
-
).launch();
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
import time
|
4 |
|
5 |
model_name = "ethanrom/a2"
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
|
11 |
pretrained_model = pipeline("zero-shot-classification", model=pretrained_model_name, tokenizer=pretrained_tokenizer)
|
12 |
candidate_labels = ["negative", "positive", "no impact", "mixed"]
|
13 |
|
|
|
|
|
|
|
14 |
|
15 |
def predict_sentiment(text_input, model_selection):
|
|
|
|
|
|
|
16 |
if model_selection == "Fine-tuned":
|
17 |
inputs = tokenizer.encode_plus(text_input, return_tensors='pt')
|
18 |
+
start_time = time.time()
|
19 |
outputs = model(**inputs)
|
20 |
+
end_time = time.time()
|
21 |
logits = outputs.logits.detach().cpu().numpy()[0]
|
22 |
predicted_class = int(logits.argmax())
|
23 |
+
inference_time = end_time - start_time
|
24 |
+
model_size = model.num_parameters()
|
25 |
+
return candidate_labels[predicted_class], inference_time, model_size
|
26 |
else:
|
27 |
+
start_time = time.time()
|
28 |
result = pretrained_model(text_input, candidate_labels)
|
29 |
+
end_time = time.time()
|
30 |
predicted_class = result["labels"][0]
|
31 |
+
inference_time = end_time - start_time
|
32 |
+
model_size = pretrained_tokenizer.model_max_length + pretrained_model.model.num_parameters()
|
33 |
+
return candidate_labels[predicted_class], inference_time, model_size
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
inputs = [
|
36 |
gr.inputs.Textbox("Enter text"),
|
|
|
39 |
|
40 |
outputs = [
|
41 |
gr.outputs.Textbox(label="Predicted Sentiment"),
|
42 |
+
gr.outputs.Textbox(label="Inference Time (s)"),
|
43 |
+
gr.outputs.Textbox(label="Model Size (params)"),
|
|
|
44 |
]
|
45 |
|
46 |
+
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();
|
|
|
|
|
|
|
|