baglada commited on
Commit
3bf8793
·
1 Parent(s): c7f3ce1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import transformers
2
+ import gradio as gr
3
+ import torch
4
+ import csv
5
+
6
+ # Load a pre-trained model
7
+ model = transformers.AutoModel.from_pretrained("bert-base-uncased")
8
+ model.eval()
9
+
10
+ # Define a function to run the model on input text
11
+ def predict_sentiment(input_text):
12
+ input_ids = transformers.BertTokenizer.encode(input_text, add_special_tokens=True)
13
+ input_ids = torch.tensor(input_ids).unsqueeze(0)
14
+ outputs = model(input_ids)
15
+ logits = outputs[0]
16
+ sentiment = "Positive" if logits[0][0] > 0 else "Negative"
17
+ return sentiment
18
+
19
+ # Create a chat history to store previous inputs and outputs
20
+ chat_history = []
21
+
22
+ # Define a function to update the chat history
23
+ def update_history(input_text, sentiment):
24
+ chat_history.append(f"User: {input_text}")
25
+ chat_history.append(f"Model: {sentiment}")
26
+
27
+ # Read the prompts from a CSV file
28
+ prompts = []
29
+ with open("prompts.csv") as csvfile:
30
+ reader = csv.reader(csvfile)
31
+ for row in reader:
32
+ prompts.append(row[0])
33
+
34
+ # Create an input interface using Gradio
35
+ inputs = gr.inputs.Dropdown(prompts, default=prompts[0])
36
+
37
+ # Create an output interface using Gradio
38
+ outputs = gr.outputs.Chatbox(label="Sentiment", lines=1)
39
+
40
+ # Run the interface
41
+ interface = gr.Interface(predict_sentiment, inputs, outputs, title="Sentiment Analysis",
42
+ on_output=update_history)
43
+ interface.launch()