hipnologo commited on
Commit
5dc0980
1 Parent(s): 480555b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -1,17 +1,27 @@
1
  import gradio as gr
2
- sentiment_analysis = gr.Interface.load("models/hipnologo/gpt2-imdb-finetune").launch()
3
-
4
- def predict(review_text):
5
- # Run the review text through the pipeline
6
- result = sentiment_analysis(review_text)[0]
7
-
8
- # Map the LABEL_0/LABEL_1 to Negative/Positive
9
- if result['label'] == 'LABEL_0':
10
- result['label'] = 'Negative'
11
- elif result['label'] == 'LABEL_1':
12
- result['label'] = 'Positive'
13
-
14
- return result['label'], float(result['score'])
15
-
16
- iface = gr.Interface(fn=predict, inputs="textbox", outputs=["label", "number"])
 
 
 
 
 
 
 
 
 
 
17
  iface.launch()
 
1
  import gradio as gr
2
+ #gr.Interface.load("models/hipnologo/gpt2-imdb-finetune").launch()
3
+
4
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
5
+
6
+ def predict_review(text):
7
+ # Specify the model name or path
8
+ model_name = "models/hipnologo/gpt2-imdb-finetune"
9
+
10
+ # Load your model and tokenizer
11
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
12
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
13
+
14
+ # encoding the input text
15
+ input_ids = tokenizer.encode(text, return_tensors="pt")
16
+
17
+ # getting the logits
18
+ output = model(input_ids)
19
+ logits = output.logits
20
+
21
+ # getting the predicted class
22
+ predicted_class = logits.argmax(-1).item()
23
+
24
+ return f"The sentiment predicted by the model is: {'Positive' if predicted_class == 1 else 'Negative'}"
25
+
26
+ iface = gr.Interface(fn=predict_review, inputs="textbox", outputs="text")
27
  iface.launch()