Spaces:
Sleeping
Sleeping
Shankar
commited on
Commit
·
d48fa9e
1
Parent(s):
8c8945a
feat: add dashboard
Browse files- app.py +204 -0
- assets/profile_pic.png +0 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,204 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import plotly.express as px
|
4 |
+
import requests
|
5 |
+
|
6 |
+
|
7 |
+
def get_financial_summary():
|
8 |
+
try:
|
9 |
+
response = requests.get("http://localhost:8000/api/financial_summary")
|
10 |
+
response.raise_for_status()
|
11 |
+
data = response.json()
|
12 |
+
except Exception:
|
13 |
+
data = {"revenue": 1000, "expenses": 500, "profit": 500}
|
14 |
+
return data
|
15 |
+
|
16 |
+
|
17 |
+
def display_financial_charts():
|
18 |
+
df = pd.DataFrame(
|
19 |
+
{
|
20 |
+
"Month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
|
21 |
+
"Revenue": [1000, 1200, 900, 1300, 1500, 1700],
|
22 |
+
"Expenses": [400, 600, 500, 700, 800, 900],
|
23 |
+
}
|
24 |
+
)
|
25 |
+
df["Profit"] = df["Revenue"] - df["Expenses"]
|
26 |
+
|
27 |
+
fig1 = px.line(df, x="Month", y="Revenue", title="Monthly Revenue")
|
28 |
+
fig2 = px.bar(df, x="Month", y="Expenses", title="Monthly Expenses")
|
29 |
+
fig3 = px.area(df, x="Month", y="Profit", title="Monthly Profit")
|
30 |
+
latest = df.iloc[-1]
|
31 |
+
fig4 = px.pie(
|
32 |
+
names=["Revenue", "Expenses", "Profit"],
|
33 |
+
values=[latest["Revenue"], latest["Expenses"], latest["Profit"]],
|
34 |
+
title="Latest Financial Distribution",
|
35 |
+
)
|
36 |
+
return fig1, fig2, fig3, fig4
|
37 |
+
|
38 |
+
|
39 |
+
def chatbot_respond(user_message, history):
|
40 |
+
history = history or []
|
41 |
+
|
42 |
+
history.append({"role": "user", "content": user_message})
|
43 |
+
|
44 |
+
try:
|
45 |
+
response = requests.post(
|
46 |
+
"https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/bot/query",
|
47 |
+
json={"messages": history},
|
48 |
+
)
|
49 |
+
response.raise_for_status()
|
50 |
+
bot_reply = (
|
51 |
+
response.json().get("data", {}).get("raw", "Sorry, I didn't understand.")
|
52 |
+
)
|
53 |
+
except Exception as e:
|
54 |
+
import traceback
|
55 |
+
|
56 |
+
traceback.print_exc()
|
57 |
+
bot_reply = "Server unavailable. This is a mocked reply."
|
58 |
+
|
59 |
+
history.append({"role": "assistant", "content": bot_reply})
|
60 |
+
|
61 |
+
return "", history, history
|
62 |
+
|
63 |
+
|
64 |
+
def reset_chat():
|
65 |
+
"""Reset chat history and clear the chatbot text box."""
|
66 |
+
return "", [], []
|
67 |
+
|
68 |
+
|
69 |
+
def minimize_chat():
|
70 |
+
return gr.update(visible=False), gr.update(visible=True)
|
71 |
+
|
72 |
+
|
73 |
+
def restore_chat():
|
74 |
+
return gr.update(visible=True), gr.update(visible=False)
|
75 |
+
|
76 |
+
|
77 |
+
# Frontend
|
78 |
+
|
79 |
+
with gr.Blocks(
|
80 |
+
css="""
|
81 |
+
#profile-pic-wrapper {
|
82 |
+
width: 100px;
|
83 |
+
height: 100px;
|
84 |
+
border-radius: 50%;
|
85 |
+
overflow: hidden;
|
86 |
+
display: flex;
|
87 |
+
align-items: center;
|
88 |
+
justify-content: center;
|
89 |
+
margin: 0 auto 10px auto;
|
90 |
+
background: #fff;
|
91 |
+
}
|
92 |
+
#profile-icon {
|
93 |
+
width: 100%;
|
94 |
+
height: 100%;
|
95 |
+
object-fit: cover;
|
96 |
+
border-radius: 50%;
|
97 |
+
display: block;
|
98 |
+
margin: 0;
|
99 |
+
}
|
100 |
+
.header h1 {
|
101 |
+
margin: 0;
|
102 |
+
font-family: 'Arial', sans-serif;
|
103 |
+
color: #333;
|
104 |
+
}
|
105 |
+
.left-navbar {
|
106 |
+
background-color: #fff;
|
107 |
+
padding: 20px;
|
108 |
+
border-right: 1px solid #ddd;
|
109 |
+
min-height: 80vh;
|
110 |
+
}
|
111 |
+
.chat-panel {
|
112 |
+
position: fixed;
|
113 |
+
bottom: 20px;
|
114 |
+
right: 20px;
|
115 |
+
width: 350px;
|
116 |
+
background: #fff;
|
117 |
+
border: 1px solid #ddd;
|
118 |
+
box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
|
119 |
+
border-radius: 8px;
|
120 |
+
padding: 10px;
|
121 |
+
}
|
122 |
+
#open-chat-btn {
|
123 |
+
position: fixed;
|
124 |
+
bottom: 20px;
|
125 |
+
right: 20px;
|
126 |
+
z-index: 1000;
|
127 |
+
}
|
128 |
+
"""
|
129 |
+
) as demo:
|
130 |
+
|
131 |
+
with gr.Row():
|
132 |
+
# gr.HTML(
|
133 |
+
# '<div id="profile-pic-wrapper"><img id="profile-icon" src="assets/profile_pic.png" /></div>'
|
134 |
+
# )
|
135 |
+
gr.Markdown("<h1>Financial Health Dashboard</h1>", elem_classes="header")
|
136 |
+
|
137 |
+
with gr.Row():
|
138 |
+
with gr.Column(scale=1, elem_classes="left-navbar"):
|
139 |
+
gr.Markdown("## Navigation")
|
140 |
+
for nav in ["Dashboard", "Reports", "Analytics", "Settings"]:
|
141 |
+
gr.Button(nav)
|
142 |
+
|
143 |
+
with gr.Column(scale=3):
|
144 |
+
with gr.Tabs():
|
145 |
+
with gr.TabItem("Overview"):
|
146 |
+
summary = get_financial_summary()
|
147 |
+
gr.Markdown(
|
148 |
+
f"""
|
149 |
+
### Summary
|
150 |
+
**Revenue:** {summary['revenue']}
|
151 |
+
**Expenses:** {summary['expenses']}
|
152 |
+
**Profit:** {summary['profit']}
|
153 |
+
"""
|
154 |
+
)
|
155 |
+
with gr.TabItem("Charts"):
|
156 |
+
fig1, fig2, fig3, fig4 = display_financial_charts()
|
157 |
+
gr.Plot(fig1)
|
158 |
+
gr.Plot(fig2)
|
159 |
+
gr.Plot(fig3)
|
160 |
+
gr.Plot(fig4)
|
161 |
+
|
162 |
+
minimized_state = gr.State(False)
|
163 |
+
|
164 |
+
chat_panel = gr.Column(visible=True, elem_classes="chat-panel")
|
165 |
+
with chat_panel:
|
166 |
+
gr.Markdown("### Chatbot Support")
|
167 |
+
chatbot_state = gr.State([])
|
168 |
+
chatbot_ui = gr.Chatbot(type="messages")
|
169 |
+
chatbot_input = gr.Textbox(
|
170 |
+
placeholder="Type your message...", label="Your Message"
|
171 |
+
)
|
172 |
+
with gr.Row():
|
173 |
+
send_btn = gr.Button("Send")
|
174 |
+
reset_btn = gr.Button("Reset Chat")
|
175 |
+
minimize_btn = gr.Button("Minimize", elem_id="minimize-chat-btn")
|
176 |
+
|
177 |
+
open_btn = gr.Button("Open Chat", elem_id="open-chat-btn", visible=False)
|
178 |
+
|
179 |
+
send_btn.click(
|
180 |
+
fn=chatbot_respond,
|
181 |
+
inputs=[chatbot_input, chatbot_state],
|
182 |
+
outputs=[chatbot_input, chatbot_state, chatbot_ui],
|
183 |
+
queue=False,
|
184 |
+
)
|
185 |
+
reset_btn.click(
|
186 |
+
fn=reset_chat,
|
187 |
+
inputs=None,
|
188 |
+
outputs=[chatbot_input, chatbot_state, chatbot_ui],
|
189 |
+
queue=False,
|
190 |
+
)
|
191 |
+
minimize_btn.click(
|
192 |
+
fn=minimize_chat,
|
193 |
+
inputs=None,
|
194 |
+
outputs=[chat_panel, open_btn],
|
195 |
+
queue=False,
|
196 |
+
)
|
197 |
+
open_btn.click(
|
198 |
+
fn=restore_chat,
|
199 |
+
inputs=None,
|
200 |
+
outputs=[chat_panel, open_btn],
|
201 |
+
queue=False,
|
202 |
+
)
|
203 |
+
|
204 |
+
demo.launch()
|
assets/profile_pic.png
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
pandas
|
2 |
+
requests
|
3 |
+
plotly
|
4 |
+
gradio
|