Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,7 +5,6 @@ import yfinance as yf
|
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
from datetime import datetime, timedelta
|
| 8 |
-
import threading
|
| 9 |
import time
|
| 10 |
import warnings
|
| 11 |
warnings.filterwarnings('ignore')
|
|
@@ -109,13 +108,14 @@ class RealTimeMarketData:
|
|
| 109 |
|
| 110 |
def get_single_stock_data(self, symbol):
|
| 111 |
"""Get data for a single stock with real-time updates"""
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
self._update_all_data()
|
| 116 |
|
| 117 |
data = self.get_live_data()
|
| 118 |
-
return data.get(symbol
|
| 119 |
|
| 120 |
class AI_TradingAgents:
|
| 121 |
"""
|
|
@@ -468,11 +468,12 @@ def generate_agent_analysis(symbol, market_data_dict):
|
|
| 468 |
|
| 469 |
else:
|
| 470 |
# Analyze specific symbol
|
| 471 |
-
|
|
|
|
| 472 |
|
| 473 |
-
analysis_text = f"# π― {symbol
|
| 474 |
-
analysis_text += f"**Current Price:** ${market_data_dict.get(symbol
|
| 475 |
-
analysis_text += f"**24h Change:** {market_data_dict.get(symbol
|
| 476 |
analysis_text += f"**Last Updated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
| 477 |
|
| 478 |
for agent_type, agent_analysis in analysis.items():
|
|
@@ -481,18 +482,38 @@ def generate_agent_analysis(symbol, market_data_dict):
|
|
| 481 |
|
| 482 |
return analysis_text
|
| 483 |
|
| 484 |
-
def update_interface(symbol_input):
|
| 485 |
"""Main function to update the entire interface"""
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 496 |
|
| 497 |
# Create Gradio Interface
|
| 498 |
with gr.Blocks(
|
|
@@ -500,17 +521,7 @@ with gr.Blocks(
|
|
| 500 |
primary_hue="blue",
|
| 501 |
secondary_hue="slate"
|
| 502 |
),
|
| 503 |
-
title="π€ AI Trading Agents - Real-Time System"
|
| 504 |
-
css="""
|
| 505 |
-
.gradio-container {
|
| 506 |
-
max-width: 1200px !important;
|
| 507 |
-
}
|
| 508 |
-
.analysis-box {
|
| 509 |
-
border-left: 4px solid #4ECDC4;
|
| 510 |
-
padding-left: 15px;
|
| 511 |
-
margin: 10px 0;
|
| 512 |
-
}
|
| 513 |
-
"""
|
| 514 |
) as demo:
|
| 515 |
|
| 516 |
gr.Markdown("""
|
|
@@ -536,7 +547,7 @@ with gr.Blocks(
|
|
| 536 |
gr.Markdown("""
|
| 537 |
**Tracked Stocks:** AAPL, GOOGL, MSFT, TSLA
|
| 538 |
|
| 539 |
-
*Data updates
|
| 540 |
""")
|
| 541 |
|
| 542 |
with gr.Column(scale=2):
|
|
@@ -553,12 +564,11 @@ with gr.Blocks(
|
|
| 553 |
with gr.TabItem("π€ AI Analysis"):
|
| 554 |
with gr.Row():
|
| 555 |
analysis_output = gr.Markdown(
|
| 556 |
-
label="Multi-Agent Analysis Report"
|
| 557 |
-
show_label=True
|
| 558 |
)
|
| 559 |
|
| 560 |
with gr.Row():
|
| 561 |
-
gr.Markdown("""
|
| 562 |
---
|
| 563 |
**π System Features:**
|
| 564 |
- π **Research Agent**: Fundamental analysis & market sentiment
|
|
@@ -566,8 +576,8 @@ with gr.Blocks(
|
|
| 566 |
- π‘οΈ **Risk Agent**: Position sizing & risk management
|
| 567 |
- π― **Decision Engine**: Final trading recommendations
|
| 568 |
|
| 569 |
-
*Last System Update: {
|
| 570 |
-
"""
|
| 571 |
|
| 572 |
# Event handlers
|
| 573 |
analyze_btn.click(
|
|
@@ -576,12 +586,11 @@ with gr.Blocks(
|
|
| 576 |
outputs=[chart_output, analysis_output]
|
| 577 |
)
|
| 578 |
|
| 579 |
-
#
|
| 580 |
demo.load(
|
| 581 |
-
fn=update_interface,
|
| 582 |
-
inputs=[
|
| 583 |
-
outputs=[chart_output, analysis_output]
|
| 584 |
-
every=30 # Refresh every 30 seconds
|
| 585 |
)
|
| 586 |
|
| 587 |
# Launch for Hugging Face Spaces
|
|
@@ -589,6 +598,5 @@ if __name__ == "__main__":
|
|
| 589 |
demo.launch(
|
| 590 |
server_name="0.0.0.0",
|
| 591 |
server_port=7860,
|
| 592 |
-
share=True
|
| 593 |
-
show_error=True
|
| 594 |
)
|
|
|
|
| 5 |
import plotly.graph_objects as go
|
| 6 |
from plotly.subplots import make_subplots
|
| 7 |
from datetime import datetime, timedelta
|
|
|
|
| 8 |
import time
|
| 9 |
import warnings
|
| 10 |
warnings.filterwarnings('ignore')
|
|
|
|
| 108 |
|
| 109 |
def get_single_stock_data(self, symbol):
|
| 110 |
"""Get data for a single stock with real-time updates"""
|
| 111 |
+
symbol = symbol.upper().strip()
|
| 112 |
+
if symbol not in self.symbols and symbol:
|
| 113 |
+
# Add new symbol temporarily for this session
|
| 114 |
+
self.symbols.append(symbol)
|
| 115 |
self._update_all_data()
|
| 116 |
|
| 117 |
data = self.get_live_data()
|
| 118 |
+
return data.get(symbol, self._generate_simulated_data(symbol))
|
| 119 |
|
| 120 |
class AI_TradingAgents:
|
| 121 |
"""
|
|
|
|
| 468 |
|
| 469 |
else:
|
| 470 |
# Analyze specific symbol
|
| 471 |
+
symbol = symbol.upper().strip()
|
| 472 |
+
analysis = trading_agents.analyze_market(symbol, market_data_dict)
|
| 473 |
|
| 474 |
+
analysis_text = f"# π― {symbol} - AI Trading Analysis\n\n"
|
| 475 |
+
analysis_text += f"**Current Price:** ${market_data_dict.get(symbol, {}).get('current_price', 'N/A'):.2f}\n"
|
| 476 |
+
analysis_text += f"**24h Change:** {market_data_dict.get(symbol, {}).get('change', 'N/A'):+.2f}%\n"
|
| 477 |
analysis_text += f"**Last Updated:** {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
|
| 478 |
|
| 479 |
for agent_type, agent_analysis in analysis.items():
|
|
|
|
| 482 |
|
| 483 |
return analysis_text
|
| 484 |
|
| 485 |
+
def update_interface(symbol_input=""):
|
| 486 |
"""Main function to update the entire interface"""
|
| 487 |
+
try:
|
| 488 |
+
# Get fresh market data
|
| 489 |
+
current_market_data = market_data.get_live_data()
|
| 490 |
+
|
| 491 |
+
# Create visualizations
|
| 492 |
+
dashboard = create_real_time_dashboard(current_market_data, symbol_input)
|
| 493 |
+
|
| 494 |
+
# Generate analysis
|
| 495 |
+
analysis = generate_agent_analysis(symbol_input, current_market_data)
|
| 496 |
+
|
| 497 |
+
return dashboard, analysis
|
| 498 |
+
|
| 499 |
+
except Exception as e:
|
| 500 |
+
# Return error state
|
| 501 |
+
error_fig = go.Figure()
|
| 502 |
+
error_fig.add_annotation(
|
| 503 |
+
text="β οΈ System temporarily unavailable<br>Please try again in a moment",
|
| 504 |
+
xref="paper", yref="paper",
|
| 505 |
+
x=0.5, y=0.5,
|
| 506 |
+
showarrow=False,
|
| 507 |
+
font=dict(size=20)
|
| 508 |
+
)
|
| 509 |
+
error_fig.update_layout(
|
| 510 |
+
height=400,
|
| 511 |
+
template='plotly_dark'
|
| 512 |
+
)
|
| 513 |
+
|
| 514 |
+
error_analysis = "# β System Error\n\nWe're experiencing temporary technical difficulties. Please refresh the page and try again."
|
| 515 |
+
|
| 516 |
+
return error_fig, error_analysis
|
| 517 |
|
| 518 |
# Create Gradio Interface
|
| 519 |
with gr.Blocks(
|
|
|
|
| 521 |
primary_hue="blue",
|
| 522 |
secondary_hue="slate"
|
| 523 |
),
|
| 524 |
+
title="π€ AI Trading Agents - Real-Time System"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 525 |
) as demo:
|
| 526 |
|
| 527 |
gr.Markdown("""
|
|
|
|
| 547 |
gr.Markdown("""
|
| 548 |
**Tracked Stocks:** AAPL, GOOGL, MSFT, TSLA
|
| 549 |
|
| 550 |
+
*Data updates automatically on each analysis*
|
| 551 |
""")
|
| 552 |
|
| 553 |
with gr.Column(scale=2):
|
|
|
|
| 564 |
with gr.TabItem("π€ AI Analysis"):
|
| 565 |
with gr.Row():
|
| 566 |
analysis_output = gr.Markdown(
|
| 567 |
+
label="Multi-Agent Analysis Report"
|
|
|
|
| 568 |
)
|
| 569 |
|
| 570 |
with gr.Row():
|
| 571 |
+
gr.Markdown(f"""
|
| 572 |
---
|
| 573 |
**π System Features:**
|
| 574 |
- π **Research Agent**: Fundamental analysis & market sentiment
|
|
|
|
| 576 |
- π‘οΈ **Risk Agent**: Position sizing & risk management
|
| 577 |
- π― **Decision Engine**: Final trading recommendations
|
| 578 |
|
| 579 |
+
*Last System Update: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}*
|
| 580 |
+
""")
|
| 581 |
|
| 582 |
# Event handlers
|
| 583 |
analyze_btn.click(
|
|
|
|
| 586 |
outputs=[chart_output, analysis_output]
|
| 587 |
)
|
| 588 |
|
| 589 |
+
# Load initial data when app starts
|
| 590 |
demo.load(
|
| 591 |
+
fn=lambda: update_interface(""),
|
| 592 |
+
inputs=[],
|
| 593 |
+
outputs=[chart_output, analysis_output]
|
|
|
|
| 594 |
)
|
| 595 |
|
| 596 |
# Launch for Hugging Face Spaces
|
|
|
|
| 598 |
demo.launch(
|
| 599 |
server_name="0.0.0.0",
|
| 600 |
server_port=7860,
|
| 601 |
+
share=True
|
|
|
|
| 602 |
)
|