prath commited on
Commit
eed4fad
Β·
1 Parent(s): be7f198

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import AutoTokenizer
4
+ from model import SentimentClassifier
5
+
6
+ model_state_dict = torch.load('sentiment_model.pth')
7
+ model = SentimentClassifier(2)
8
+ model.load_state_dict(model_state_dict)
9
+ model.eval()
10
+
11
+ tokenizer = AutoTokenizer.from_pretrained('bert-base-uncased')
12
+
13
+
14
+ def preprocess(text):
15
+ inputs = tokenizer(text, padding='max_length',
16
+ truncation=True, max_length=512, return_tensors='pt')
17
+ return inputs
18
+ # Define a function to use the model to make predictions
19
+ def predict():
20
+ review = request.form['review']
21
+ inputs = preprocess(review)
22
+ with torch.no_grad():
23
+ outputs = model(inputs['input_ids'], inputs['attention_mask'])
24
+ predicted_class = torch.argmax(outputs[0]).item()
25
+ if(predicted_class==0):
26
+ return "It was a negative review"
27
+ return "It was a positive review"
28
+
29
+ # Create a Gradio interface
30
+ input_text = gr.inputs.Textbox(label="Input Text")
31
+ output_text = gr.outputs.Textbox(label="Output Text")
32
+ interface = gr.Interface(fn=predict, inputs=input_text, outputs=output_text)
33
+
34
+ # Run the interface
35
+ interface.test_launch()