File size: 3,542 Bytes
32dfb54
 
 
 
 
 
 
 
 
 
 
 
 
 
b4b125a
 
 
 
 
 
 
 
 
 
 
32dfb54
e50b966
 
32dfb54
 
 
 
 
 
b4b125a
32dfb54
b4b125a
32dfb54
 
 
 
 
 
 
b4b125a
 
 
 
 
32dfb54
b4b125a
 
32dfb54
c9da226
 
32dfb54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification, AutoModelForSequenceClassification
import gradio as gr

# Initialize sentiment analysis model and tokenizer
sentiment_tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
sentiment_model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")
sentiment_model.eval()

# Initialize stock identification model and tokenizer
ner_tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
ner_model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
ner_model.eval()

def get_advice(sentiment_label, stocks_mentioned):
    # Add your own logic for providing advice based on sentiment and stocks mentioned
    if sentiment_label == "Positive":
        advice = "Positive sentiment. Consider taking advantage of positive market trends."
    elif sentiment_label == "Negative":
        if stocks_mentioned:
            advice = f"Negative sentiment. Consider re-evaluating your position on stocks: {', '.join(stocks_mentioned)}."
        else:
            advice = "Negative sentiment. Consider monitoring the market for potential impacts."
    else:
        advice = "Neutral sentiment. The market may not be strongly influenced. Monitor for changes."

    return sentiment_label
        # return advice

def predict_sentiment_and_stock_info(headline):
    # Sentiment Analysis
    sentiment_inputs = sentiment_tokenizer(headline, padding=True, truncation=True, return_tensors='pt')
    with torch.no_grad():
        sentiment_outputs = sentiment_model(**sentiment_inputs)
        sentiment_prediction = torch.nn.functional.softmax(sentiment_outputs.logits, dim=-1)

    pos, neg, neutr = sentiment_prediction[:, 0].item(), sentiment_prediction[:, 1].item(), sentiment_prediction[:, 2].item()
    sentiment_label = "Positive" if pos > neg and pos > neutr else "Negative" if neg > pos and neg > neutr else "Neutral"

    # Named Entity Recognition (NER)
    ner_inputs = ner_tokenizer(headline, return_tensors="pt")
    with torch.no_grad():
        ner_outputs = ner_model(**ner_inputs)

    # Identify stocks mentioned in the headline
    ner_predictions = torch.nn.functional.softmax(ner_outputs.logits, dim=-1).argmax(2)
    tokens = ner_tokenizer.convert_ids_to_tokens(ner_inputs['input_ids'][0].tolist())  # Use ner_inputs here
    entities = ner_tokenizer.convert_ids_to_tokens(ner_predictions[0].tolist())
    stocks_mentioned = [tokens[i] for i, entity in enumerate(entities) if entity.startswith("B")]

    # Advice based on sentiment and identified stocks
    advice = get_advice(sentiment_label, stocks_mentioned)

    # return sentiment_label, advice
    return sentiment_label

# Gradio Interface
'''iface = gr.Interface(
    fn=predict_sentiment_and_stock_info,
    inputs="text",
    outputs=["text", "text"],
    live=True,
    title="Financial News Sentiment and Stock Analysis",
    description="Enter a financial news headline to analyze its sentiment, identify mentioned stocks, and get advice on how to proceed."
)
iface.launch()'''

# Gradio Interface
iface = gr.Interface(
    fn=predict_sentiment_and_stock_info,
    inputs=[gr.Textbox(lines=2, label="Financial Statement")],
    outputs=[
        gr.Textbox(label="Sentiment"),
        # gr.Textbox(label="Advice")
    ],
    live=True,
    title="Financial Content Sentiment Analysis",
    description="Enter a financial statement to analyze its sentiment."
)

iface.launch()