ethanrom commited on
Commit
aaf9b07
1 Parent(s): 6d46f17

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -13
app.py CHANGED
@@ -1,14 +1,37 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
-
4
- def classify_sentiment(text_input):
5
- candidate_labels = ["negative", "positive", "no impact", "mixed"]
6
- classifier = pipeline("zero-shot-classification", model="roberta-large-mnli")
7
- result = classifier(text_input, candidate_labels)
8
- predicted_class = result["labels"][0]
9
- return predicted_class.capitalize()
10
-
11
- iface = gr.Interface(fn=classify_sentiment, inputs="text", outputs="text",
12
- title="Sentiment Analysis",
13
- description="Enter a sentence to predict the sentiment.")
14
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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();