Tomรกs F commited on
Commit
8ad6cb1
โ€ข
1 Parent(s): 653dde1
Files changed (3) hide show
  1. README.md +3 -3
  2. app.py +88 -0
  3. requirements.txt +4 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Fin Perceiver Demo
3
- emoji: ๐Ÿฆ€
4
  colorFrom: blue
5
- colorTo: pink
6
  sdk: streamlit
7
  app_file: app.py
8
  pinned: false
 
1
  ---
2
+ title: FINPerceiver Demo
3
+ emoji: ๐Ÿ’ธ
4
  colorFrom: blue
5
+ colorTo: green
6
  sdk: streamlit
7
  app_file: app.py
8
  pinned: false
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import feedparser
2
+ import streamlit as st
3
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
4
+
5
+
6
+ @st.cache(allow_output_mutation=True, show_spinner=False)
7
+ def load_model():
8
+ return AutoModelForSequenceClassification.from_pretrained("warwickai/fin-perceiver")
9
+
10
+
11
+ @st.cache(show_spinner=False)
12
+ def load_news(feed):
13
+ return feedparser.parse(feed).get('entries')
14
+
15
+
16
+ def filter_with_sentiment(articles, sentiments):
17
+ return filter(
18
+ lambda article: article[1].get('label') in sentiments,
19
+ articles
20
+ )
21
+
22
+
23
+ tokenizer = AutoTokenizer.from_pretrained("warwickai/fin-perceiver")
24
+
25
+ with st.spinner('๐Ÿ“ˆ Loading model...'):
26
+ model = load_model()
27
+ pipe = pipeline('text-classification', model=model, tokenizer=tokenizer)
28
+
29
+
30
+ def classify_articles(articles, target_pipeline):
31
+ headlines = [article.title for article in articles]
32
+ sentiment = target_pipeline(headlines)
33
+
34
+ return list(zip(articles, sentiment))
35
+
36
+
37
+ rss_feeds = {
38
+ 'yahoo': 'https://finance.yahoo.com/news/rssindex',
39
+ 'reuters': 'https://www.reutersagency.com/feed/?best-topics=business-finance&post_type=best'
40
+ }
41
+
42
+ sentiment_distribution = {
43
+ 'positive': 0,
44
+ 'negative': 0,
45
+ 'neutral': 0
46
+ }
47
+
48
+ st.title('FINPerceiver')
49
+
50
+ target_source = st.sidebar.selectbox(
51
+ 'Select a financial news source',
52
+ rss_feeds.keys())
53
+
54
+ target_sentiments = st.sidebar.multiselect(
55
+ label='Select the target sentiments',
56
+ options=sentiment_distribution.keys(),
57
+ default=sentiment_distribution.keys())
58
+
59
+ with st.spinner('๐Ÿ“ฐ Loading articles...'):
60
+ target_articles = load_news(rss_feeds.get(target_source))
61
+
62
+ with st.spinner('โš™๏ธ Analysing articles...'):
63
+ classified_articles = classify_articles(target_articles, pipe)
64
+
65
+ total_articles = 0
66
+
67
+ for article, sentiment in classified_articles:
68
+ total_articles += 1
69
+ sentiment_distribution[sentiment.get('label')] += 1
70
+
71
+ for sentiment in sentiment_distribution.keys():
72
+ sentiment_distribution[sentiment] /= total_articles * 0.01
73
+
74
+ st.sidebar.subheader('Summary')
75
+ st.sidebar.metric("Positive", f"๐Ÿ‘ {sentiment_distribution.get('positive'):.2f}%")
76
+ st.sidebar.metric("Neutral", f"๐Ÿ˜ {sentiment_distribution.get('neutral'):.2f}%")
77
+ st.sidebar.metric("Negative", f"๐Ÿ‘Ž {sentiment_distribution.get('negative'):.2f}%")
78
+
79
+ for article, sentiment in filter_with_sentiment(classified_articles, target_sentiments):
80
+ img_url = article.media_content[0].get('url')
81
+
82
+ st.image(img_url, width=300)
83
+ st.markdown(
84
+ f'''
85
+ #### {article.title}
86
+ **Sentiment:** {sentiment.get('label').capitalize()}
87
+ '''
88
+ )
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ streamlit
4
+ feedparser