Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import requests
|
|
6 |
import time
|
7 |
import os
|
8 |
from datetime import datetime
|
|
|
9 |
|
10 |
# Initialize hotdog classification pipeline with error handling
|
11 |
hotdog_pipeline = None
|
@@ -30,7 +31,7 @@ def parse_params(params):
|
|
30 |
sorted_keys = sorted(params.keys())
|
31 |
param_pairs = [f"{key}={params[key]}" for key in sorted_keys]
|
32 |
params_str = "&".join(param_pairs)
|
33 |
-
return params_str + "×tamp=" + str(int(time.time() * 1000))
|
34 |
|
35 |
# Fetch from BingX API
|
36 |
def fetch_from_api(endpoint, params=None, api_key=api_key, api_secret=api_secret):
|
@@ -150,26 +151,27 @@ def update_performance_chart(trades):
|
|
150 |
|
151 |
def update_allocation_chart(allocation):
|
152 |
chart_data = {
|
153 |
-
'
|
154 |
'values': allocation['data'],
|
155 |
-
'
|
156 |
'title': 'Portfolio Allocation'
|
157 |
}
|
|
|
158 |
legend_html = "\n".join(f"""
|
159 |
<div class="mb-3">
|
160 |
<div class="flex justify-between mb-1">
|
161 |
<span class="text-gray-500 dark:text-gray-400 flex items-center">
|
162 |
-
<span class="h-3 w-3 bg-[{chart_data['
|
163 |
{label}
|
164 |
</span>
|
165 |
<span class="font-medium dark:text-white">{data:.1f}%</span>
|
166 |
</div>
|
167 |
<div class="w-full bg-gray-200 rounded-full h-2 dark:bg-gray-700">
|
168 |
-
<div class="bg-[{chart_data['
|
169 |
</div>
|
170 |
</div>
|
171 |
""" for i, (label, data) in enumerate(zip(allocation['labels'], allocation['data'])))
|
172 |
-
return gr.
|
173 |
|
174 |
# Main Data Fetch and Update Function
|
175 |
def update_dashboard(api_key, api_secret):
|
@@ -199,7 +201,7 @@ def update_dashboard(api_key, api_secret):
|
|
199 |
return (
|
200 |
"$Loading...", 0, "0 Long β’ 0 Short", "$0.00", "Low", "0%", gr.HTML("<tr><td colspan='8' class='py-4 text-center'>Error: Failed to sync with BingX API - Check credentials</td></tr>"),
|
201 |
gr.HTML("<div>Error loading stats</div>"), gr.LinePlot(value={'x': [], 'y': [], 'color': [], 'title': 'Monthly Performance'}),
|
202 |
-
(gr.
|
203 |
)
|
204 |
|
205 |
# Save Credentials Function
|
@@ -256,7 +258,6 @@ with gr.Blocks(title="Nakhoda4X Pro") as demo:
|
|
256 |
gr.Markdown("Connected to BingX via API")
|
257 |
with gr.Row():
|
258 |
refresh_btn = gr.Button("Refresh", elem_classes="px-4 py-2 bg-white dark:bg-darkCard rounded-lg border border-gray-200 dark:border-gray-700 text-gray-700 dark:text-gray-300 flex items-center")
|
259 |
-
new_trade_btn = gr.Button("New Trade", elem_classes="px-4 py-2 bg-primary text-white rounded-lg flex items-center")
|
260 |
|
261 |
# Stats Cards
|
262 |
with gr.Row(elem_classes="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6"):
|
@@ -295,7 +296,7 @@ with gr.Blocks(title="Nakhoda4X Pro") as demo:
|
|
295 |
portfolio_allocation = gr.Blocks()
|
296 |
with portfolio_allocation:
|
297 |
gr.HTML("<div class='bg-white dark:bg-darkCard rounded-2xl shadow-md p-6'><div class='flex justify-between items-center mb-6'><h3 class='text-lg font-bold text-gray-800 dark:text-white'>Portfolio Allocation</h3><div><span id='allocation-update' class='text-gray-500 dark:text-gray-400 text-sm'>Last updated: Just now</span></div></div>")
|
298 |
-
allocation_chart, allocation_legend = gr.
|
299 |
|
300 |
# Event Handlers
|
301 |
toggle_btn.click(fn=toggle_api_form, inputs=[show_form], outputs=[api_form], _js="() => !document.querySelector('.api-form').classList.contains('open')")
|
@@ -315,7 +316,6 @@ with gr.Blocks(title="Nakhoda4X Pro") as demo:
|
|
315 |
risk_exposure, exposure_percent, trading_activity, advanced_stats,
|
316 |
performance_chart, (allocation_chart, allocation_legend), gr.State(value=None), gr.State(value=None)
|
317 |
])
|
318 |
-
new_trade_btn.click(None, _js="() => alert('New Trade functionality not implemented yet.')")
|
319 |
|
320 |
# Hot Dog Classifier Tab
|
321 |
with gr.Tab("Hot Dog Classifier"):
|
|
|
6 |
import time
|
7 |
import os
|
8 |
from datetime import datetime
|
9 |
+
import plotly.express as px
|
10 |
|
11 |
# Initialize hotdog classification pipeline with error handling
|
12 |
hotdog_pipeline = None
|
|
|
31 |
sorted_keys = sorted(params.keys())
|
32 |
param_pairs = [f"{key}={params[key]}" for key in sorted_keys]
|
33 |
params_str = "&".join(param_pairs)
|
34 |
+
return params_str + "×tamp=" + str(int(time.time() * 1000))
|
35 |
|
36 |
# Fetch from BingX API
|
37 |
def fetch_from_api(endpoint, params=None, api_key=api_key, api_secret=api_secret):
|
|
|
151 |
|
152 |
def update_allocation_chart(allocation):
|
153 |
chart_data = {
|
154 |
+
'labels': allocation['labels'],
|
155 |
'values': allocation['data'],
|
156 |
+
'color': ['#1E90FF', '#10b981', '#8b5cf6', '#f59e0b'],
|
157 |
'title': 'Portfolio Allocation'
|
158 |
}
|
159 |
+
fig = px.pie(names=chart_data['labels'], values=chart_data['values'], color_discrete_sequence=chart_data['color'], title=chart_data['title'])
|
160 |
legend_html = "\n".join(f"""
|
161 |
<div class="mb-3">
|
162 |
<div class="flex justify-between mb-1">
|
163 |
<span class="text-gray-500 dark:text-gray-400 flex items-center">
|
164 |
+
<span class="h-3 w-3 bg-[{chart_data['color'][i % 4]}] rounded-full mr-2"></span>
|
165 |
{label}
|
166 |
</span>
|
167 |
<span class="font-medium dark:text-white">{data:.1f}%</span>
|
168 |
</div>
|
169 |
<div class="w-full bg-gray-200 rounded-full h-2 dark:bg-gray-700">
|
170 |
+
<div class="bg-[{chart_data['color'][i % 4]}] h-2 rounded-full" style="width: {data}%"></div>
|
171 |
</div>
|
172 |
</div>
|
173 |
""" for i, (label, data) in enumerate(zip(allocation['labels'], allocation['data'])))
|
174 |
+
return gr.Plot(value=fig), gr.HTML(legend_html)
|
175 |
|
176 |
# Main Data Fetch and Update Function
|
177 |
def update_dashboard(api_key, api_secret):
|
|
|
201 |
return (
|
202 |
"$Loading...", 0, "0 Long β’ 0 Short", "$0.00", "Low", "0%", gr.HTML("<tr><td colspan='8' class='py-4 text-center'>Error: Failed to sync with BingX API - Check credentials</td></tr>"),
|
203 |
gr.HTML("<div>Error loading stats</div>"), gr.LinePlot(value={'x': [], 'y': [], 'color': [], 'title': 'Monthly Performance'}),
|
204 |
+
(gr.Plot(value=px.pie(names=[], values=[], title='Portfolio Allocation')), gr.HTML("")), "Just now", "Just now"
|
205 |
)
|
206 |
|
207 |
# Save Credentials Function
|
|
|
258 |
gr.Markdown("Connected to BingX via API")
|
259 |
with gr.Row():
|
260 |
refresh_btn = gr.Button("Refresh", elem_classes="px-4 py-2 bg-white dark:bg-darkCard rounded-lg border border-gray-200 dark:border-gray-700 text-gray-700 dark:text-gray-300 flex items-center")
|
|
|
261 |
|
262 |
# Stats Cards
|
263 |
with gr.Row(elem_classes="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6"):
|
|
|
296 |
portfolio_allocation = gr.Blocks()
|
297 |
with portfolio_allocation:
|
298 |
gr.HTML("<div class='bg-white dark:bg-darkCard rounded-2xl shadow-md p-6'><div class='flex justify-between items-center mb-6'><h3 class='text-lg font-bold text-gray-800 dark:text-white'>Portfolio Allocation</h3><div><span id='allocation-update' class='text-gray-500 dark:text-gray-400 text-sm'>Last updated: Just now</span></div></div>")
|
299 |
+
allocation_chart, allocation_legend = gr.Plot(), gr.HTML()
|
300 |
|
301 |
# Event Handlers
|
302 |
toggle_btn.click(fn=toggle_api_form, inputs=[show_form], outputs=[api_form], _js="() => !document.querySelector('.api-form').classList.contains('open')")
|
|
|
316 |
risk_exposure, exposure_percent, trading_activity, advanced_stats,
|
317 |
performance_chart, (allocation_chart, allocation_legend), gr.State(value=None), gr.State(value=None)
|
318 |
])
|
|
|
319 |
|
320 |
# Hot Dog Classifier Tab
|
321 |
with gr.Tab("Hot Dog Classifier"):
|