cutechicken commited on
Commit
3cb584a
·
verified ·
1 Parent(s): da7a4ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -26
app.py CHANGED
@@ -1,5 +1,4 @@
1
  import logging
2
-
3
  import gradio as gr
4
  import pandas as pd
5
  import torch
@@ -14,24 +13,42 @@ logging.basicConfig(
14
  SENTIMENT_ANALYSIS_MODEL = (
15
  "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
16
  )
17
-
18
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
19
  logging.info(f"Using device: {DEVICE}")
20
-
21
  logging.info("Initializing sentiment analysis model...")
22
  sentiment_analyzer = pipeline(
23
  "sentiment-analysis", model=SENTIMENT_ANALYSIS_MODEL, device=DEVICE
24
  )
25
  logging.info("Model initialized successfully")
26
 
27
-
28
- def fetch_articles(query):
29
  try:
30
- logging.info(f"Fetching articles for query: '{query}'")
31
  googlenews = GoogleNews(lang="en")
32
  googlenews.search(query)
 
 
33
  articles = googlenews.result()
34
- logging.info(f"Fetched {len(articles)} articles")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
  return articles
36
  except Exception as e:
37
  logging.error(
@@ -42,28 +59,21 @@ def fetch_articles(query):
42
  duration=5,
43
  )
44
 
45
-
46
  def analyze_article_sentiment(article):
47
  logging.info(f"Analyzing sentiment for article: {article['title']}")
48
  sentiment = sentiment_analyzer(article["desc"])[0]
49
  article["sentiment"] = sentiment
50
  return article
51
 
52
-
53
  def analyze_asset_sentiment(asset_name):
54
  logging.info(f"Starting sentiment analysis for asset: {asset_name}")
55
-
56
- logging.info("Fetching articles")
57
- articles = fetch_articles(asset_name)
58
-
59
  logging.info("Analyzing sentiment of each article")
60
  analyzed_articles = [analyze_article_sentiment(article) for article in articles]
61
-
62
  logging.info("Sentiment analysis completed")
63
-
64
  return convert_to_dataframe(analyzed_articles)
65
 
66
-
67
  def convert_to_dataframe(analyzed_articles):
68
  df = pd.DataFrame(analyzed_articles)
69
  df["Title"] = df.apply(
@@ -72,7 +82,7 @@ def convert_to_dataframe(analyzed_articles):
72
  )
73
  df["Description"] = df["desc"]
74
  df["Date"] = df["date"]
75
-
76
  def sentiment_badge(sentiment):
77
  colors = {
78
  "negative": "red",
@@ -81,27 +91,26 @@ def convert_to_dataframe(analyzed_articles):
81
  }
82
  color = colors.get(sentiment, "grey")
83
  return f'<span style="background-color: {color}; color: white; padding: 2px 6px; border-radius: 4px;">{sentiment}</span>'
84
-
85
  df["Sentiment"] = df["sentiment"].apply(lambda x: sentiment_badge(x["label"]))
86
  return df[["Sentiment", "Title", "Description", "Date"]]
87
 
88
-
89
  with gr.Blocks() as iface:
90
  gr.Markdown("# Trading Asset Sentiment Analysis")
91
  gr.Markdown(
92
  "Enter the name of a trading asset, and I'll fetch recent articles and analyze their sentiment!"
93
  )
94
-
95
  with gr.Row():
96
  input_asset = gr.Textbox(
97
  label="Asset Name",
98
  lines=1,
99
  placeholder="Enter the name of the trading asset...",
100
  )
101
-
102
  with gr.Row():
103
  analyze_button = gr.Button("Analyze Sentiment", size="sm")
104
-
105
  gr.Examples(
106
  examples=[
107
  "Bitcoin",
@@ -111,7 +120,7 @@ with gr.Blocks() as iface:
111
  ],
112
  inputs=input_asset,
113
  )
114
-
115
  with gr.Row():
116
  with gr.Column():
117
  with gr.Blocks():
@@ -121,7 +130,7 @@ with gr.Blocks() as iface:
121
  datatype=["markdown", "html", "markdown", "markdown"],
122
  wrap=False,
123
  )
124
-
125
  analyze_button.click(
126
  analyze_asset_sentiment,
127
  inputs=[input_asset],
@@ -129,5 +138,4 @@ with gr.Blocks() as iface:
129
  )
130
 
131
  logging.info("Launching Gradio interface")
132
- iface.queue().launch()
133
-
 
1
  import logging
 
2
  import gradio as gr
3
  import pandas as pd
4
  import torch
 
13
  SENTIMENT_ANALYSIS_MODEL = (
14
  "mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
15
  )
 
16
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
17
  logging.info(f"Using device: {DEVICE}")
 
18
  logging.info("Initializing sentiment analysis model...")
19
  sentiment_analyzer = pipeline(
20
  "sentiment-analysis", model=SENTIMENT_ANALYSIS_MODEL, device=DEVICE
21
  )
22
  logging.info("Model initialized successfully")
23
 
24
+ def fetch_articles(query, max_articles=30):
 
25
  try:
26
+ logging.info(f"Fetching up to {max_articles} articles for query: '{query}'")
27
  googlenews = GoogleNews(lang="en")
28
  googlenews.search(query)
29
+
30
+ # 첫 페이지 결과 가져오기
31
  articles = googlenews.result()
32
+
33
+ # 목표 기사 수에 도달할 때까지 추가 페이지 가져오기
34
+ page = 2
35
+ while len(articles) < max_articles and page <= 10: # 최대 10페이지까지만 시도
36
+ logging.info(f"Fetched {len(articles)} articles so far. Getting page {page}...")
37
+ googlenews.get_page(page)
38
+ page_results = googlenews.result()
39
+
40
+ # 새 결과가 없으면 중단
41
+ if not page_results:
42
+ logging.info(f"No more results found after page {page-1}")
43
+ break
44
+
45
+ articles.extend(page_results)
46
+ page += 1
47
+
48
+ # 최대 기사 수로 제한
49
+ articles = articles[:max_articles]
50
+
51
+ logging.info(f"Successfully fetched {len(articles)} articles")
52
  return articles
53
  except Exception as e:
54
  logging.error(
 
59
  duration=5,
60
  )
61
 
 
62
  def analyze_article_sentiment(article):
63
  logging.info(f"Analyzing sentiment for article: {article['title']}")
64
  sentiment = sentiment_analyzer(article["desc"])[0]
65
  article["sentiment"] = sentiment
66
  return article
67
 
 
68
  def analyze_asset_sentiment(asset_name):
69
  logging.info(f"Starting sentiment analysis for asset: {asset_name}")
70
+ logging.info("Fetching up to 30 articles")
71
+ articles = fetch_articles(asset_name, max_articles=30)
 
 
72
  logging.info("Analyzing sentiment of each article")
73
  analyzed_articles = [analyze_article_sentiment(article) for article in articles]
 
74
  logging.info("Sentiment analysis completed")
 
75
  return convert_to_dataframe(analyzed_articles)
76
 
 
77
  def convert_to_dataframe(analyzed_articles):
78
  df = pd.DataFrame(analyzed_articles)
79
  df["Title"] = df.apply(
 
82
  )
83
  df["Description"] = df["desc"]
84
  df["Date"] = df["date"]
85
+
86
  def sentiment_badge(sentiment):
87
  colors = {
88
  "negative": "red",
 
91
  }
92
  color = colors.get(sentiment, "grey")
93
  return f'<span style="background-color: {color}; color: white; padding: 2px 6px; border-radius: 4px;">{sentiment}</span>'
94
+
95
  df["Sentiment"] = df["sentiment"].apply(lambda x: sentiment_badge(x["label"]))
96
  return df[["Sentiment", "Title", "Description", "Date"]]
97
 
 
98
  with gr.Blocks() as iface:
99
  gr.Markdown("# Trading Asset Sentiment Analysis")
100
  gr.Markdown(
101
  "Enter the name of a trading asset, and I'll fetch recent articles and analyze their sentiment!"
102
  )
103
+
104
  with gr.Row():
105
  input_asset = gr.Textbox(
106
  label="Asset Name",
107
  lines=1,
108
  placeholder="Enter the name of the trading asset...",
109
  )
110
+
111
  with gr.Row():
112
  analyze_button = gr.Button("Analyze Sentiment", size="sm")
113
+
114
  gr.Examples(
115
  examples=[
116
  "Bitcoin",
 
120
  ],
121
  inputs=input_asset,
122
  )
123
+
124
  with gr.Row():
125
  with gr.Column():
126
  with gr.Blocks():
 
130
  datatype=["markdown", "html", "markdown", "markdown"],
131
  wrap=False,
132
  )
133
+
134
  analyze_button.click(
135
  analyze_asset_sentiment,
136
  inputs=[input_asset],
 
138
  )
139
 
140
  logging.info("Launching Gradio interface")
141
+ iface.queue().launch()