Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,44 +10,24 @@ model = BertForSequenceClassification.from_pretrained('ProsusAI/finbert')
|
|
10 |
|
11 |
def analyze_sentiment(sec_text):
|
12 |
# Encode the text
|
13 |
-
tokens = tokenizer.encode_plus(sec_text, add_special_tokens=
|
14 |
-
input_ids = tokens['input_ids']
|
15 |
-
total_len = len(input_ids)
|
16 |
-
attention_mask = tokens['attention_mask']
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
start = 0
|
23 |
-
while start < total_len:
|
24 |
-
end = start + 510
|
25 |
-
if end > total_len:
|
26 |
-
end = total_len
|
27 |
-
input_ids_chunk = [101] + input_ids[start:end] + [102] # Add [CLS] and [SEP] tokens
|
28 |
-
attention_mask_chunk = [1] + attention_mask[start:end] + [1]
|
29 |
-
with torch.no_grad():
|
30 |
-
outputs = model(torch.tensor([input_ids_chunk]), attention_mask=torch.tensor([attention_mask_chunk]))
|
31 |
-
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)[0].tolist()
|
32 |
-
proba_list.append(probabilities)
|
33 |
-
start = end
|
34 |
-
|
35 |
-
# Calculate the mean of the probabilities
|
36 |
-
sentiment_scores = [sum(col) / len(col) for col in zip(*proba_list)]
|
37 |
-
sentiment = sentiment_scores.index(max(sentiment_scores))
|
38 |
|
|
|
|
|
|
|
|
|
39 |
# Return the sentiment analysis result
|
40 |
-
|
41 |
-
return "Positive Sentiment"
|
42 |
-
elif sentiment == 1:
|
43 |
-
return "Negative Sentiment"
|
44 |
-
else:
|
45 |
-
return "Neutral"
|
46 |
|
47 |
# Define the Gradio interface
|
48 |
gr_interface = gr.Interface(
|
49 |
fn=analyze_sentiment,
|
50 |
-
inputs=gr.
|
51 |
outputs="text",
|
52 |
title="Sentiment Analysis of SEC Filings",
|
53 |
description="This tool predicts the sentiment of text excerpts from SEC filings."
|
|
|
10 |
|
11 |
def analyze_sentiment(sec_text):
|
12 |
# Encode the text
|
13 |
+
tokens = tokenizer.encode_plus(sec_text, add_special_tokens=True, return_tensors="pt")
|
|
|
|
|
|
|
14 |
|
15 |
+
# Make prediction
|
16 |
+
with torch.no_grad():
|
17 |
+
outputs = model(**tokens)
|
18 |
+
predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
# Convert predictions to sentiment labels
|
21 |
+
labels = ['Positive', 'Neutral', 'Negative']
|
22 |
+
sentiment = labels[torch.argmax(predictions)]
|
23 |
+
|
24 |
# Return the sentiment analysis result
|
25 |
+
return f"{sentiment} Sentiment"
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Define the Gradio interface
|
28 |
gr_interface = gr.Interface(
|
29 |
fn=analyze_sentiment,
|
30 |
+
inputs=gr.Textbox(lines=10, placeholder="Enter SEC filing excerpt here..."),
|
31 |
outputs="text",
|
32 |
title="Sentiment Analysis of SEC Filings",
|
33 |
description="This tool predicts the sentiment of text excerpts from SEC filings."
|