Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,37 @@
|
|
1 |
import gradio as gr
|
2 |
-
from transformers import pipeline
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
|
3 |
+
|
4 |
+
# Load the fine-tuned model and tokenizer
|
5 |
+
model_name = "ethanrom/a2"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
7 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
8 |
+
|
9 |
+
# Load the pretrained model and tokenizer
|
10 |
+
pretrained_model_name = "roberta-large-mnli"
|
11 |
+
pretrained_tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name)
|
12 |
+
pretrained_model = pipeline("zero-shot-classification", model=pretrained_model_name, tokenizer=pretrained_tokenizer)
|
13 |
+
candidate_labels = ["negative", "positive", "no impact", "mixed"]
|
14 |
+
|
15 |
+
|
16 |
+
def predict_sentiment(text_input, model_selection):
|
17 |
+
if model_selection == "Fine-tuned":
|
18 |
+
# Use the fine-tuned model
|
19 |
+
inputs = tokenizer.encode_plus(text_input, return_tensors='pt')
|
20 |
+
outputs = model(**inputs)
|
21 |
+
logits = outputs.logits.detach().cpu().numpy()[0]
|
22 |
+
predicted_class = int(logits.argmax())
|
23 |
+
return candidate_labels[predicted_class]
|
24 |
+
else:
|
25 |
+
# Use the pretrained model
|
26 |
+
result = pretrained_model(text_input, candidate_labels)
|
27 |
+
predicted_class = result["labels"][0]
|
28 |
+
return predicted_class
|
29 |
+
|
30 |
+
inputs = [
|
31 |
+
gr.inputs.Textbox("Enter text"),
|
32 |
+
gr.inputs.Dropdown(["Pretrained", "Fine-tuned"], label="Select model"),
|
33 |
+
]
|
34 |
+
|
35 |
+
outputs = gr.outputs.Textbox(label="Predicted Sentiment")
|
36 |
+
|
37 |
+
gr.Interface(fn=predict_sentiment, inputs=inputs, outputs=outputs, title="Sentiment Analysis", description="Compare the output of two models").launch();
|