File size: 1,355 Bytes
3bf8793
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import transformers
import gradio as gr
import torch
import csv

# Load a pre-trained model
model = transformers.AutoModel.from_pretrained("bert-base-uncased")
model.eval()

# Define a function to run the model on input text
def predict_sentiment(input_text):
    input_ids = transformers.BertTokenizer.encode(input_text, add_special_tokens=True)
    input_ids = torch.tensor(input_ids).unsqueeze(0)
    outputs = model(input_ids)
    logits = outputs[0]
    sentiment = "Positive" if logits[0][0] > 0 else "Negative"
    return sentiment

# Create a chat history to store previous inputs and outputs
chat_history = []

# Define a function to update the chat history
def update_history(input_text, sentiment):
    chat_history.append(f"User: {input_text}")
    chat_history.append(f"Model: {sentiment}")

# Read the prompts from a CSV file
prompts = []
with open("prompts.csv") as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        prompts.append(row[0])

# Create an input interface using Gradio
inputs = gr.inputs.Dropdown(prompts, default=prompts[0])

# Create an output interface using Gradio
outputs = gr.outputs.Chatbox(label="Sentiment", lines=1)

# Run the interface
interface = gr.Interface(predict_sentiment, inputs, outputs, title="Sentiment Analysis",
                         on_output=update_history)
interface.launch()