Spaces:
Running
π§ Fix Real-Time Stock Analysis & AI Visualizations Functionality
Browse files## β
Critical Interface Fixes:
- **Fixed Stock Analysis Tab** - Real-time stock analysis now working properly
- **Fixed AI Visualizations Tab** - DALL-E image generation interface now functional
- **Fixed Code Executor Tab** - Python code execution now properly connected
## π Root Cause Resolution:
- **Interface Component Connection** - Gradio components weren't properly connected to event handlers
- **Inline Interface Definition** - Moved interface components inline within tab contexts
- **Event Handler Binding** - Fixed click events for all interactive components
## π Stock Analysis Features Now Working:
- Real-time stock data fetching with yfinance
- Interactive candlestick charts with moving averages
- Comprehensive financial analysis (Sharpe ratio, Beta, volatility)
- Multiple time period selection (1mo, 3mo, 6mo, 1y, 2y, 5y)
## π» Code Executor Features Now Working:
- Live Python code execution in secure environment
- Financial analysis examples pre-loaded
- Clear output display and code clearing functionality
## π¨ AI Visualizations Features Now Working:
- DALL-E 3 image generation for financial visualizations
- Example prompts for financial charts and dashboards
- Proper error handling and user feedback
## π§ Technical Improvements:
- Proper Gradio component lifecycle management
- Fixed interface component return value handling
- Enhanced error handling throughout all tabs
π€ Generated with [Claude Code](https://claude.ai/code)
Co-Authored-By: Claude <noreply@anthropic.com>
|
@@ -975,18 +975,143 @@ with gr.Blocks(
|
|
| 975 |
# Stock Analysis Tab
|
| 976 |
with gr.Tab("Stock Analysis", id="stocks"):
|
| 977 |
gr.HTML("<h2 style='text-align: center; color: #2c3e50; font-size: 1.5em; margin: 10px 0;'>Real-Time Stock Analysis</h2>")
|
| 978 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 979 |
|
| 980 |
# Code Executor Tab
|
| 981 |
with gr.Tab("Code Executor", id="code"):
|
| 982 |
gr.HTML("<h2 style='text-align: center; color: #2c3e50; font-size: 1.5em; margin: 10px 0;'>Python Financial Code Executor</h2>")
|
| 983 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 984 |
|
| 985 |
# AI Visualizations Tab
|
| 986 |
with gr.Tab("AI Visuals", id="visuals"):
|
| 987 |
gr.HTML("<h2 style='text-align: center; color: #2c3e50; font-size: 1.5em; margin: 10px 0;'>AI-Generated Financial Visualizations</h2>")
|
|
|
|
| 988 |
if openai_client:
|
| 989 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 990 |
else:
|
| 991 |
gr.HTML("""
|
| 992 |
<div style="text-align: center; padding: 40px; background: #f8f9fa; border-radius: 10px;">
|
|
|
|
| 975 |
# Stock Analysis Tab
|
| 976 |
with gr.Tab("Stock Analysis", id="stocks"):
|
| 977 |
gr.HTML("<h2 style='text-align: center; color: #2c3e50; font-size: 1.5em; margin: 10px 0;'>Real-Time Stock Analysis</h2>")
|
| 978 |
+
|
| 979 |
+
with gr.Row():
|
| 980 |
+
with gr.Column(scale=1):
|
| 981 |
+
stock_input = gr.Textbox(
|
| 982 |
+
label="Stock Symbol",
|
| 983 |
+
placeholder="Enter symbol (e.g., AAPL, TSLA, MSFT)",
|
| 984 |
+
value="AAPL"
|
| 985 |
+
)
|
| 986 |
+
period_select = gr.Dropdown(
|
| 987 |
+
choices=["1mo", "3mo", "6mo", "1y", "2y", "5y"],
|
| 988 |
+
value="1y",
|
| 989 |
+
label="Time Period"
|
| 990 |
+
)
|
| 991 |
+
analyze_btn = gr.Button(
|
| 992 |
+
"Analyze Stock",
|
| 993 |
+
variant="primary",
|
| 994 |
+
size="lg"
|
| 995 |
+
)
|
| 996 |
+
|
| 997 |
+
with gr.Column(scale=2):
|
| 998 |
+
stock_chart = gr.Plot(label="Stock Chart")
|
| 999 |
+
|
| 1000 |
+
with gr.Row():
|
| 1001 |
+
stock_analysis = gr.Markdown(label="Analysis Results")
|
| 1002 |
+
|
| 1003 |
+
def analyze_stock(symbol, period):
|
| 1004 |
+
if not symbol:
|
| 1005 |
+
return None, "Please enter a stock symbol"
|
| 1006 |
+
|
| 1007 |
+
stock_data = get_stock_data(symbol.upper(), period)
|
| 1008 |
+
if not stock_data or "error" in stock_data:
|
| 1009 |
+
return None, f"Error fetching data for {symbol}"
|
| 1010 |
+
|
| 1011 |
+
chart = create_stock_chart(symbol.upper(), stock_data["data"])
|
| 1012 |
+
analysis = perform_financial_analysis(symbol.upper())
|
| 1013 |
+
|
| 1014 |
+
return chart, analysis
|
| 1015 |
+
|
| 1016 |
+
analyze_btn.click(
|
| 1017 |
+
analyze_stock,
|
| 1018 |
+
inputs=[stock_input, period_select],
|
| 1019 |
+
outputs=[stock_chart, stock_analysis]
|
| 1020 |
+
)
|
| 1021 |
|
| 1022 |
# Code Executor Tab
|
| 1023 |
with gr.Tab("Code Executor", id="code"):
|
| 1024 |
gr.HTML("<h2 style='text-align: center; color: #2c3e50; font-size: 1.5em; margin: 10px 0;'>Python Financial Code Executor</h2>")
|
| 1025 |
+
|
| 1026 |
+
with gr.Row():
|
| 1027 |
+
with gr.Column():
|
| 1028 |
+
code_input = gr.Code(
|
| 1029 |
+
label="Python Code Editor",
|
| 1030 |
+
language="python",
|
| 1031 |
+
value="""# Financial Analysis Example
|
| 1032 |
+
import numpy as np
|
| 1033 |
+
import pandas as pd
|
| 1034 |
+
|
| 1035 |
+
# Calculate simple moving average
|
| 1036 |
+
prices = [100, 102, 101, 103, 105, 104, 106]
|
| 1037 |
+
ma_5 = sum(prices[-5:]) / 5
|
| 1038 |
+
print(f"5-day Moving Average: ${ma_5:.2f}")
|
| 1039 |
+
|
| 1040 |
+
# Calculate returns
|
| 1041 |
+
returns = [(prices[i] - prices[i-1]) / prices[i-1] for i in range(1, len(prices))]
|
| 1042 |
+
print(f"Daily Returns: {[f'{r:.2%}' for r in returns]}")"""
|
| 1043 |
+
)
|
| 1044 |
+
|
| 1045 |
+
with gr.Column():
|
| 1046 |
+
code_output = gr.Textbox(
|
| 1047 |
+
label="Output",
|
| 1048 |
+
lines=10,
|
| 1049 |
+
max_lines=20
|
| 1050 |
+
)
|
| 1051 |
+
|
| 1052 |
+
with gr.Row():
|
| 1053 |
+
execute_btn = gr.Button(
|
| 1054 |
+
"Execute Code",
|
| 1055 |
+
variant="primary",
|
| 1056 |
+
size="lg"
|
| 1057 |
+
)
|
| 1058 |
+
clear_btn = gr.Button("Clear", variant="secondary")
|
| 1059 |
+
|
| 1060 |
+
def execute_code(code):
|
| 1061 |
+
if not code.strip():
|
| 1062 |
+
return "Please enter some code to execute."
|
| 1063 |
+
return execute_python_code(code)
|
| 1064 |
+
|
| 1065 |
+
def clear_code():
|
| 1066 |
+
return "", ""
|
| 1067 |
+
|
| 1068 |
+
execute_btn.click(execute_code, inputs=[code_input], outputs=[code_output])
|
| 1069 |
+
clear_btn.click(clear_code, outputs=[code_input, code_output])
|
| 1070 |
|
| 1071 |
# AI Visualizations Tab
|
| 1072 |
with gr.Tab("AI Visuals", id="visuals"):
|
| 1073 |
gr.HTML("<h2 style='text-align: center; color: #2c3e50; font-size: 1.5em; margin: 10px 0;'>AI-Generated Financial Visualizations</h2>")
|
| 1074 |
+
|
| 1075 |
if openai_client:
|
| 1076 |
+
with gr.Row():
|
| 1077 |
+
with gr.Column(scale=1):
|
| 1078 |
+
viz_prompt = gr.Textbox(
|
| 1079 |
+
label="Visualization Prompt",
|
| 1080 |
+
placeholder="Describe the financial chart or visualization you want...",
|
| 1081 |
+
lines=3
|
| 1082 |
+
)
|
| 1083 |
+
generate_btn = gr.Button(
|
| 1084 |
+
"Generate Visualization",
|
| 1085 |
+
variant="primary",
|
| 1086 |
+
size="lg"
|
| 1087 |
+
)
|
| 1088 |
+
|
| 1089 |
+
with gr.Column(scale=2):
|
| 1090 |
+
viz_output = gr.Image(label="Generated Visualization")
|
| 1091 |
+
|
| 1092 |
+
with gr.Row():
|
| 1093 |
+
viz_examples = gr.Examples(
|
| 1094 |
+
examples=[
|
| 1095 |
+
["Stock market bull vs bear visualization with charts"],
|
| 1096 |
+
["Modern portfolio dashboard with pie charts and graphs"],
|
| 1097 |
+
["Risk-return scatter plot with efficient frontier"],
|
| 1098 |
+
["Financial technology concept with AI and data"],
|
| 1099 |
+
["Cryptocurrency trading interface design"]
|
| 1100 |
+
],
|
| 1101 |
+
inputs=[viz_prompt]
|
| 1102 |
+
)
|
| 1103 |
+
|
| 1104 |
+
def generate_viz(prompt):
|
| 1105 |
+
if not prompt.strip():
|
| 1106 |
+
return None
|
| 1107 |
+
|
| 1108 |
+
if not openai_client:
|
| 1109 |
+
return None
|
| 1110 |
+
|
| 1111 |
+
image_url = generate_financial_visualization(prompt)
|
| 1112 |
+
return image_url
|
| 1113 |
+
|
| 1114 |
+
generate_btn.click(generate_viz, inputs=[viz_prompt], outputs=[viz_output])
|
| 1115 |
else:
|
| 1116 |
gr.HTML("""
|
| 1117 |
<div style="text-align: center; padding: 40px; background: #f8f9fa; border-radius: 10px;">
|