Toymakerftw commited on
Commit
c5a48ec
·
1 Parent(s): d07bded
Files changed (1) hide show
  1. app.py +55 -19
app.py CHANGED
@@ -244,24 +244,62 @@ def _format_number(num):
244
 
245
  def convert_to_dataframe(analyzed_articles):
246
  df = pd.DataFrame(analyzed_articles)
247
- df["Title"] = df.apply(
248
- lambda row: f'<a href="{row["link"]}" target="_blank">{row["title"]}</a>',
249
- axis=1,
250
- )
251
- df["Description"] = df["desc"]
252
- df["Date"] = df["date"]
253
-
254
  def sentiment_badge(sentiment):
255
  colors = {
256
- "negative": "red",
257
- "neutral": "gray",
258
- "positive": "green",
259
  }
260
  color = colors.get(sentiment, "grey")
261
- return f'<span style="background-color: {color}; color: white; padding: 2px 6px; border-radius: 4px;">{sentiment}</span>'
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
- df["Sentiment"] = df["sentiment"].apply(lambda x: sentiment_badge(x["label"]))
264
- return df[["Sentiment", "Title", "Description", "Date"]]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
 
266
  def generate_stock_recommendation(articles, finance_data):
267
  """Enhanced recommendation system with technical analysis"""
@@ -358,7 +396,7 @@ def analyze_asset_sentiment(asset_input):
358
  None
359
  )
360
 
361
- # Update Gradio interface with new components
362
  with gr.Blocks(theme=gr.themes.Default()) as iface:
363
  gr.Markdown("# Advanced Trading Analytics Suite")
364
 
@@ -372,9 +410,8 @@ with gr.Blocks(theme=gr.themes.Default()) as iface:
372
 
373
  with gr.Tabs():
374
  with gr.TabItem("Sentiment Analysis"):
375
- articles_output = gr.Dataframe(render=False) # Temporary hidden
376
- gr.Markdown("## News Sentiment Analysis", render=False)
377
- articles_output.render()
378
 
379
  with gr.TabItem("Technical Analysis"):
380
  price_chart = gr.Plot(label="Price Analysis")
@@ -387,12 +424,11 @@ with gr.Blocks(theme=gr.themes.Default()) as iface:
387
  interactive=False
388
  )
389
 
390
- # Move the click handler inside the Blocks context
391
  analyze_btn.click(
392
  analyze_asset_sentiment,
393
  inputs=[input_asset],
394
  outputs=[articles_output, ta_json, recommendation_output, price_chart]
395
  )
396
-
397
  logging.info("Launching enhanced Gradio interface")
398
  iface.queue().launch()
 
244
 
245
  def convert_to_dataframe(analyzed_articles):
246
  df = pd.DataFrame(analyzed_articles)
247
+
 
 
 
 
 
 
248
  def sentiment_badge(sentiment):
249
  colors = {
250
+ "negative": "#ef4444",
251
+ "neutral": "#64748b",
252
+ "positive": "#22c55e",
253
  }
254
  color = colors.get(sentiment, "grey")
255
+ return (
256
+ f'<div style="display: inline-flex; align-items: center; gap: 0.5rem;">'
257
+ f'<div style="width: 0.75rem; height: 0.75rem; background-color: {color}; border-radius: 50%;"></div>'
258
+ f'<span style="text-transform: capitalize; font-weight: 500; color: {color}">{sentiment}</span>'
259
+ f'</div>'
260
+ )
261
+
262
+ df["Sentiment"] = df["sentiment"].apply(lambda x: sentiment_badge(x["label"].lower()))
263
+ df["Title"] = df.apply(
264
+ lambda row: f'<a href="{row["link"]}" target="_blank" style="text-decoration: none; color: #2563eb;">{row["title"]}</a>',
265
+ axis=1,
266
+ )
267
+ df["Description"] = df["desc"].apply(lambda x: f'<div style="font-size: 0.9rem; color: #4b5563;">{x}</div>')
268
+ df["Date"] = df["date"].apply(lambda x: f'<div style="font-size: 0.8rem; color: #6b7280;">{x}</div>')
269
 
270
+ # Convert to HTML table
271
+ html_table = df[["Sentiment", "Title", "Description", "Date"]].to_html(
272
+ escape=False,
273
+ index=False,
274
+ border=0,
275
+ classes="gradio-table",
276
+ justify="start"
277
+ )
278
+
279
+ # Add custom styling
280
+ styled_html = f"""
281
+ <style>
282
+ .gradio-table {{
283
+ width: 100%;
284
+ border-collapse: collapse;
285
+ }}
286
+ .gradio-table th {{
287
+ text-align: left;
288
+ padding: 0.75rem;
289
+ background-color: #f8fafc;
290
+ border-bottom: 2px solid #e2e8f0;
291
+ }}
292
+ .gradio-table td {{
293
+ padding: 0.75rem;
294
+ border-bottom: 1px solid #f1f5f9;
295
+ }}
296
+ .gradio-table tr:hover td {{
297
+ background-color: #f8fafc;
298
+ }}
299
+ </style>
300
+ {html_table}
301
+ """
302
+ return styled_html
303
 
304
  def generate_stock_recommendation(articles, finance_data):
305
  """Enhanced recommendation system with technical analysis"""
 
396
  None
397
  )
398
 
399
+ # Update the Gradio interface (change the output component type)
400
  with gr.Blocks(theme=gr.themes.Default()) as iface:
401
  gr.Markdown("# Advanced Trading Analytics Suite")
402
 
 
410
 
411
  with gr.Tabs():
412
  with gr.TabItem("Sentiment Analysis"):
413
+ gr.Markdown("## News Sentiment Analysis")
414
+ articles_output = gr.HTML(label="Analyzed News Articles") # Changed to HTML component
 
415
 
416
  with gr.TabItem("Technical Analysis"):
417
  price_chart = gr.Plot(label="Price Analysis")
 
424
  interactive=False
425
  )
426
 
 
427
  analyze_btn.click(
428
  analyze_asset_sentiment,
429
  inputs=[input_asset],
430
  outputs=[articles_output, ta_json, recommendation_output, price_chart]
431
  )
432
+
433
  logging.info("Launching enhanced Gradio interface")
434
  iface.queue().launch()