Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,27 @@
|
|
1 |
-
import
|
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 |
-
def predict_sentiment_and_stock_info(headline):
|
30 |
-
# Sentiment Analysis
|
31 |
-
sentiment_inputs = sentiment_tokenizer(headline, padding=True, truncation=True, return_tensors='pt')
|
32 |
-
with torch.no_grad():
|
33 |
-
sentiment_outputs = sentiment_model(**sentiment_inputs)
|
34 |
-
sentiment_prediction = torch.nn.functional.softmax(sentiment_outputs.logits, dim=-1)
|
35 |
-
|
36 |
-
pos, neg, neutr = sentiment_prediction[:, 0].item(), sentiment_prediction[:, 1].item(), sentiment_prediction[:, 2].item()
|
37 |
-
sentiment_label = "Positive" if pos > neg and pos > neutr else "Negative" if neg > pos and neg > neutr else "Neutral"
|
38 |
-
|
39 |
-
# Named Entity Recognition (NER)
|
40 |
-
ner_inputs = ner_tokenizer(headline, return_tensors="pt")
|
41 |
-
with torch.no_grad():
|
42 |
-
ner_outputs = ner_model(**ner_inputs)
|
43 |
-
|
44 |
-
# Identify stocks mentioned in the headline
|
45 |
-
ner_predictions = torch.nn.functional.softmax(ner_outputs.logits, dim=-1).argmax(2)
|
46 |
-
tokens = ner_tokenizer.convert_ids_to_tokens(ner_inputs['input_ids'][0].tolist()) # Use ner_inputs here
|
47 |
-
entities = ner_tokenizer.convert_ids_to_tokens(ner_predictions[0].tolist())
|
48 |
-
stocks_mentioned = [tokens[i] for i, entity in enumerate(entities) if entity.startswith("B")]
|
49 |
-
|
50 |
-
# Advice based on sentiment and identified stocks
|
51 |
-
advice = get_advice(sentiment_label, stocks_mentioned)
|
52 |
-
|
53 |
-
return sentiment_label, advice
|
54 |
-
|
55 |
-
# Gradio Interface
|
56 |
-
'''iface = gr.Interface(
|
57 |
-
fn=predict_sentiment_and_stock_info,
|
58 |
-
inputs="text",
|
59 |
-
outputs=["text", "text"],
|
60 |
-
live=True,
|
61 |
-
title="Financial News Sentiment and Stock Analysis",
|
62 |
-
description="Enter a financial news headline to analyze its sentiment, identify mentioned stocks, and get advice on how to proceed."
|
63 |
-
)
|
64 |
-
iface.launch()'''
|
65 |
-
|
66 |
-
# Gradio Interface
|
67 |
-
iface = gr.Interface(
|
68 |
-
fn=predict_sentiment_and_stock_info,
|
69 |
-
inputs=[gr.Textbox(lines=2, label="Financial Statement")],
|
70 |
-
outputs=[
|
71 |
-
gr.Textbox(label="Sentiment"),
|
72 |
-
gr.Textbox(label="Advice")
|
73 |
-
],
|
74 |
-
live=True,
|
75 |
-
title="Financial Content Sentiment Analysis",
|
76 |
-
description="Enter a financial statement to analyze its sentiment."
|
77 |
-
)
|
78 |
-
|
79 |
-
iface.launch()
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
API_URL = "https://api-inference.huggingface.co/models/ProsusAI/finbert"
|
4 |
+
headers = {"Authorization": "Bearer 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"}
|
5 |
+
|
6 |
+
def query(payload):
|
7 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
8 |
+
return response.json()
|
9 |
+
|
10 |
+
output = query({
|
11 |
+
"inputs": "I like you. I love you",
|
12 |
+
})
|
13 |
+
|
14 |
+
# # Gradio Interface
|
15 |
+
# iface = gr.Interface(
|
16 |
+
# fn=predict_sentiment_and_stock_info,
|
17 |
+
# inputs=[gr.Textbox(lines=2, label="Financial Statement")],
|
18 |
+
# outputs=[
|
19 |
+
# gr.Textbox(label="Sentiment"),
|
20 |
+
# gr.Textbox(label="Advice")
|
21 |
+
# ],
|
22 |
+
# live=True,
|
23 |
+
# title="Financial Content Sentiment Analysis",
|
24 |
+
# description="Enter a financial statement to analyze its sentiment."
|
25 |
+
# )
|
26 |
+
|
27 |
+
# iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|