Learto commited on
Commit
dba8da4
·
1 Parent(s): a2cddb8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -79
app.py CHANGED
@@ -1,79 +1,27 @@
1
- import torch
2
- from transformers import AutoTokenizer, AutoModelForTokenClassification, AutoModelForSequenceClassification
3
- import gradio as gr
4
-
5
- # Initialize sentiment analysis model and tokenizer
6
- sentiment_tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
7
- sentiment_model = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")
8
- sentiment_model.eval()
9
-
10
- # Initialize stock identification model and tokenizer
11
- ner_tokenizer = AutoTokenizer.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
12
- ner_model = AutoModelForTokenClassification.from_pretrained("dbmdz/bert-large-cased-finetuned-conll03-english")
13
- ner_model.eval()
14
-
15
- def get_advice(sentiment_label, stocks_mentioned):
16
- # Add your own logic for providing advice based on sentiment and stocks mentioned
17
- if sentiment_label == "Positive":
18
- advice = "Positive sentiment. Consider taking advantage of positive market trends."
19
- elif sentiment_label == "Negative":
20
- if stocks_mentioned:
21
- advice = f"Negative sentiment. Consider re-evaluating your position on stocks: {', '.join(stocks_mentioned)}."
22
- else:
23
- advice = "Negative sentiment. Consider monitoring the market for potential impacts."
24
- else:
25
- advice = "Neutral sentiment. The market may not be strongly influenced. Monitor for changes."
26
-
27
- return advice
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()