dami1996 commited on
Commit
dc3659d
β€’
1 Parent(s): 142fcd8

Init commit

Browse files
Files changed (3) hide show
  1. README.md +3 -3
  2. app.py +123 -0
  3. requirements.txt +6 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
  title: Trading Analyst
3
- emoji: πŸ¦€
4
- colorFrom: pink
5
- colorTo: yellow
6
  sdk: gradio
7
  sdk_version: 4.39.0
8
  app_file: app.py
 
1
  ---
2
  title: Trading Analyst
3
+ emoji: πŸ“ˆ
4
+ colorFrom: green
5
+ colorTo: green
6
  sdk: gradio
7
  sdk_version: 4.39.0
8
  app_file: app.py
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
+
3
+ import gradio as gr
4
+ import pandas as pd
5
+ import torch
6
+ from GoogleNews import GoogleNews
7
+ from transformers import pipeline
8
+
9
+ # Set up logging
10
+ logging.basicConfig(
11
+ level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
12
+ )
13
+
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(asset_name):
29
+ logging.info(f"Fetching articles for asset: {asset_name}")
30
+ googlenews = GoogleNews(lang="en")
31
+ googlenews.search(asset_name)
32
+ articles = googlenews.result()
33
+ logging.info(f"Fetched {len(articles)} articles")
34
+ return articles
35
+
36
+
37
+ def analyze_article_sentiment(article):
38
+ logging.info(f"Analyzing sentiment for article: {article['title']}")
39
+ sentiment = sentiment_analyzer(article["desc"])[0]
40
+ article["sentiment"] = sentiment
41
+ return article
42
+
43
+
44
+ def analyze_asset_sentiment(asset_name):
45
+ logging.info(f"Starting sentiment analysis for asset: {asset_name}")
46
+
47
+ logging.info("Fetching articles")
48
+ articles = fetch_articles(asset_name)
49
+
50
+ logging.info("Analyzing sentiment of each article")
51
+ analyzed_articles = [analyze_article_sentiment(article) for article in articles]
52
+
53
+ logging.info("Sentiment analysis completed")
54
+
55
+ return convert_to_dataframe(analyzed_articles)
56
+
57
+
58
+ def convert_to_dataframe(analyzed_articles):
59
+ df = pd.DataFrame(analyzed_articles)
60
+ df["Title"] = df.apply(
61
+ lambda row: f'<a href="{row["link"]}" target="_blank">{row["title"]}</a>',
62
+ axis=1,
63
+ )
64
+ df["Description"] = df["desc"]
65
+ df["Date"] = df["date"]
66
+
67
+ def sentiment_badge(sentiment):
68
+ colors = {
69
+ "negative": "red",
70
+ "neutral": "gray",
71
+ "positive": "green",
72
+ }
73
+ color = colors.get(sentiment, "grey")
74
+ return f'<span style="background-color: {color}; color: white; padding: 2px 6px; border-radius: 4px;">{sentiment}</span>'
75
+
76
+ df["Sentiment"] = df["sentiment"].apply(lambda x: sentiment_badge(x["label"]))
77
+ return df[["Sentiment", "Title", "Description", "Date"]]
78
+
79
+
80
+ with gr.Blocks() as iface:
81
+ gr.Markdown("# Trading Asset Sentiment Analysis")
82
+ gr.Markdown(
83
+ "Enter the name of a trading asset, and I'll fetch recent articles and analyze their sentiment!"
84
+ )
85
+
86
+ with gr.Row():
87
+ input_asset = gr.Textbox(
88
+ label="Asset Name",
89
+ lines=1,
90
+ placeholder="Enter the name of the trading asset...",
91
+ )
92
+
93
+ with gr.Row():
94
+ analyze_button = gr.Button("Analyze Sentiment", size="sm")
95
+
96
+ gr.Examples(
97
+ examples=[
98
+ "Bitcoin",
99
+ "Tesla",
100
+ "Apple",
101
+ "Amazon",
102
+ ],
103
+ inputs=input_asset,
104
+ )
105
+
106
+ with gr.Row():
107
+ with gr.Column():
108
+ with gr.Blocks():
109
+ gr.Markdown("## Articles and Sentiment Analysis")
110
+ articles_output = gr.Dataframe(
111
+ headers=["Sentiment", "Title", "Description", "Date"],
112
+ datatype=["markdown", "html", "markdown", "markdown"],
113
+ wrap=False,
114
+ )
115
+
116
+ analyze_button.click(
117
+ analyze_asset_sentiment,
118
+ inputs=[input_asset],
119
+ outputs=[articles_output],
120
+ )
121
+
122
+ logging.info("Launching Gradio interface")
123
+ iface.queue().launch()
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio==4.39.0
2
+ transformers==4.43.2
3
+ diffusers==0.29.2
4
+ accelerate==0.33.0
5
+ sentencepiece==0.2.0
6
+ GoogleNews==1.6.14