openfree commited on
Commit
4eb9228
·
verified ·
1 Parent(s): 1f36874

Delete app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +0 -141
app-backup.py DELETED
@@ -1,141 +0,0 @@
1
- import logging
2
- import gradio as gr
3
- import pandas as pd
4
- import torch
5
- from GoogleNews import GoogleNews
6
- from transformers import pipeline
7
-
8
- # Set up logging
9
- logging.basicConfig(
10
- level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
11
- )
12
-
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(
55
- f"Error while searching articles for query: '{query}'. Error: {e}"
56
- )
57
- raise gr.Error(
58
- f"Unable to search articles for query: '{query}'. Try again later...",
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(
80
- lambda row: f'<a href="{row["link"]}" target="_blank">{row["title"]}</a>',
81
- axis=1,
82
- )
83
- df["Description"] = df["desc"]
84
- df["Date"] = df["date"]
85
-
86
- def sentiment_badge(sentiment):
87
- colors = {
88
- "negative": "red",
89
- "neutral": "gray",
90
- "positive": "green",
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",
117
- "Tesla",
118
- "Apple",
119
- "Amazon",
120
- ],
121
- inputs=input_asset,
122
- )
123
-
124
- with gr.Row():
125
- with gr.Column():
126
- with gr.Blocks():
127
- gr.Markdown("## Articles and Sentiment Analysis")
128
- articles_output = gr.Dataframe(
129
- headers=["Sentiment", "Title", "Description", "Date"],
130
- datatype=["markdown", "html", "markdown", "markdown"],
131
- wrap=False,
132
- )
133
-
134
- analyze_button.click(
135
- analyze_asset_sentiment,
136
- inputs=[input_asset],
137
- outputs=[articles_output],
138
- )
139
-
140
- logging.info("Launching Gradio interface")
141
- iface.queue().launch()