File size: 1,296 Bytes
e7273e1
986217d
e7273e1
986217d
e7273e1
986217d
 
e7273e1
986217d
 
5b3cb2f
e7273e1
efda388
986217d
efda388
 
 
 
986217d
efda388
 
 
 
e7273e1
efda388
e7273e1
5b3cb2f
 
 
 
 
 
 
e7273e1
 
644fc5a
5b3cb2f
e431b7a
5b3cb2f
 
986217d
e4be09f
e7273e1
986217d
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
44
import gradio as gr
import torch
from transformers import BertTokenizer, BertForSequenceClassification

# Load pre-trained model tokenizer (vocabulary)
tokenizer = BertTokenizer.from_pretrained('ProsusAI/finbert')

# Load pre-trained model
model = BertForSequenceClassification.from_pretrained('ProsusAI/finbert')

def get_sentiment(sec_text):  # Ensure the parameter name matches the placeholder name.
    # Encode the text
    tokens = tokenizer.encode_plus(sec_text, add_special_tokens=True, return_tensors="pt")
    
    # Make prediction
    with torch.no_grad():
        outputs = model(**tokens)
        predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)

    # Convert predictions to sentiment labels
    labels = ['Positive', 'Neutral', 'Negative']
    sentiment = labels[torch.argmax(predictions)]
    
    # Return the sentiment analysis result
    return f"{sentiment} Sentiment"

# Custom CSS to center the title
custom_css = """
    .title {
        text-align: center;
    }
"""

# Define the Gradio interface
gr_interface = gr.Interface(
    fn=get_sentiment,
    inputs=gr.Textbox(lines=1, placeholder=""),
    outputs="text",
    title="Sentiment Analysis",
    css=custom_css  # Add the custom CSS to the Interface
)

# Launch the interface
gr_interface.launch()