Spaces:
Sleeping
Sleeping
Shankar
commited on
Commit
·
34c9847
1
Parent(s):
89e6f0a
improve layout
Browse files
app.py
CHANGED
@@ -11,7 +11,7 @@ def get_financial_summary():
|
|
11 |
response.raise_for_status()
|
12 |
data = response.json()
|
13 |
except Exception:
|
14 |
-
data = {"revenue": 1000, "expenses": 500, "
|
15 |
return data
|
16 |
|
17 |
|
@@ -19,29 +19,28 @@ def display_financial_charts():
|
|
19 |
df = pd.DataFrame(
|
20 |
{
|
21 |
"Month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
|
22 |
-
"
|
23 |
"Expenses": [400, 600, 500, 700, 800, 900],
|
24 |
}
|
25 |
)
|
26 |
-
df["
|
27 |
|
28 |
-
fig1 = px.line(df, x="Month", y="
|
29 |
fig2 = px.bar(df, x="Month", y="Expenses", title="Monthly Expenses")
|
30 |
-
fig3 = px.area(df, x="Month", y="
|
31 |
latest = df.iloc[-1]
|
32 |
fig4 = px.pie(
|
33 |
-
names=["
|
34 |
-
values=[latest["
|
35 |
title="Latest Financial Distribution",
|
36 |
)
|
37 |
-
return
|
38 |
|
39 |
|
40 |
def chatbot_respond(user_message, history):
|
41 |
history = history or []
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
try:
|
46 |
response = requests.post(
|
47 |
"https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/bot/query",
|
@@ -51,9 +50,8 @@ def chatbot_respond(user_message, history):
|
|
51 |
bot_reply = (
|
52 |
response.json().get("data", {}).get("raw", "Sorry, I didn't understand.")
|
53 |
)
|
54 |
-
except Exception
|
55 |
bot_reply = "Server unavailable. This is a mocked reply."
|
56 |
-
|
57 |
history.append({"role": "assistant", "content": bot_reply})
|
58 |
|
59 |
return "", history, history
|
@@ -112,11 +110,13 @@ with gr.Blocks(
|
|
112 |
bottom: 20px;
|
113 |
right: 20px;
|
114 |
width: 350px;
|
|
|
115 |
background: #fff;
|
116 |
border: 1px solid #ddd;
|
117 |
box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
|
118 |
border-radius: 8px;
|
119 |
padding: 10px;
|
|
|
120 |
}
|
121 |
#open-chat-btn {
|
122 |
position: fixed;
|
@@ -124,6 +124,9 @@ with gr.Blocks(
|
|
124 |
right: 20px;
|
125 |
z-index: 1000;
|
126 |
}
|
|
|
|
|
|
|
127 |
""",
|
128 |
) as demo:
|
129 |
|
@@ -134,39 +137,43 @@ with gr.Blocks(
|
|
134 |
gr.Markdown("<h1>Financial Health Dashboard</h1>", elem_classes="header")
|
135 |
|
136 |
with gr.Row():
|
137 |
-
with gr.Column(scale=1, elem_classes="left-navbar"):
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
|
142 |
with gr.Column(scale=3):
|
143 |
with gr.Tabs():
|
144 |
-
with gr.TabItem("Overview"):
|
145 |
-
summary = get_financial_summary()
|
146 |
-
gr.Markdown(
|
147 |
-
f"""
|
148 |
-
### Summary
|
149 |
-
**Revenue:** {summary['revenue']}
|
150 |
-
**Expenses:** {summary['expenses']}
|
151 |
-
**Profit:** {summary['profit']}
|
152 |
-
"""
|
153 |
-
)
|
154 |
with gr.TabItem("Charts"):
|
155 |
-
|
156 |
-
gr.Plot(fig1)
|
157 |
gr.Plot(fig2)
|
158 |
gr.Plot(fig3)
|
159 |
gr.Plot(fig4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
|
161 |
minimized_state = gr.State(False)
|
162 |
|
163 |
chat_panel = gr.Column(visible=True, elem_classes="chat-panel")
|
164 |
with chat_panel:
|
165 |
-
gr.Markdown("###
|
166 |
chatbot_state = gr.State([])
|
167 |
chatbot_ui = gr.Chatbot(type="messages")
|
168 |
chatbot_input = gr.Textbox(
|
169 |
-
placeholder="Type your message...",
|
|
|
|
|
170 |
)
|
171 |
with gr.Row():
|
172 |
send_btn = gr.Button("Send")
|
@@ -175,6 +182,12 @@ with gr.Blocks(
|
|
175 |
|
176 |
open_btn = gr.Button("Open Chat", elem_id="open-chat-btn", visible=False)
|
177 |
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
send_btn.click(
|
179 |
fn=chatbot_respond,
|
180 |
inputs=[chatbot_input, chatbot_state],
|
|
|
11 |
response.raise_for_status()
|
12 |
data = response.json()
|
13 |
except Exception:
|
14 |
+
data = {"revenue": 1000, "expenses": 500, "savings": 500}
|
15 |
return data
|
16 |
|
17 |
|
|
|
19 |
df = pd.DataFrame(
|
20 |
{
|
21 |
"Month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
|
22 |
+
"Credit": [1000, 1200, 900, 1300, 1500, 1700],
|
23 |
"Expenses": [400, 600, 500, 700, 800, 900],
|
24 |
}
|
25 |
)
|
26 |
+
df["Savings"] = df["Credit"] - df["Expenses"]
|
27 |
|
28 |
+
# fig1 = px.line(df, x="Month", y="Credit", title="Monthly Credit")
|
29 |
fig2 = px.bar(df, x="Month", y="Expenses", title="Monthly Expenses")
|
30 |
+
fig3 = px.area(df, x="Month", y="Savings", title="Monthly Savings")
|
31 |
latest = df.iloc[-1]
|
32 |
fig4 = px.pie(
|
33 |
+
names=["Food", "Retail", "Others"],
|
34 |
+
values=[latest["Credit"], latest["Expenses"], latest["Savings"]],
|
35 |
title="Latest Financial Distribution",
|
36 |
)
|
37 |
+
return fig2, fig3, fig4
|
38 |
|
39 |
|
40 |
def chatbot_respond(user_message, history):
|
41 |
history = history or []
|
42 |
+
if user_message:
|
43 |
+
history.append({"role": "user", "content": user_message})
|
|
|
44 |
try:
|
45 |
response = requests.post(
|
46 |
"https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/bot/query",
|
|
|
50 |
bot_reply = (
|
51 |
response.json().get("data", {}).get("raw", "Sorry, I didn't understand.")
|
52 |
)
|
53 |
+
except Exception:
|
54 |
bot_reply = "Server unavailable. This is a mocked reply."
|
|
|
55 |
history.append({"role": "assistant", "content": bot_reply})
|
56 |
|
57 |
return "", history, history
|
|
|
110 |
bottom: 20px;
|
111 |
right: 20px;
|
112 |
width: 350px;
|
113 |
+
height: 620px;
|
114 |
background: #fff;
|
115 |
border: 1px solid #ddd;
|
116 |
box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
|
117 |
border-radius: 8px;
|
118 |
padding: 10px;
|
119 |
+
overflow: visible;
|
120 |
}
|
121 |
#open-chat-btn {
|
122 |
position: fixed;
|
|
|
124 |
right: 20px;
|
125 |
z-index: 1000;
|
126 |
}
|
127 |
+
#chatbot-input {
|
128 |
+
height: 150px;
|
129 |
+
}
|
130 |
""",
|
131 |
) as demo:
|
132 |
|
|
|
137 |
gr.Markdown("<h1>Financial Health Dashboard</h1>", elem_classes="header")
|
138 |
|
139 |
with gr.Row():
|
140 |
+
# with gr.Column(scale=1, elem_classes="left-navbar"):
|
141 |
+
# gr.Markdown("## Navigation")
|
142 |
+
# for nav in ["Dashboard", "Reports", "Analytics", "Settings"]:
|
143 |
+
# gr.Button(nav)
|
144 |
|
145 |
with gr.Column(scale=3):
|
146 |
with gr.Tabs():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
with gr.TabItem("Charts"):
|
148 |
+
fig2, fig3, fig4 = display_financial_charts()
|
149 |
+
# gr.Plot(fig1)
|
150 |
gr.Plot(fig2)
|
151 |
gr.Plot(fig3)
|
152 |
gr.Plot(fig4)
|
153 |
+
with gr.TabItem("Overview"):
|
154 |
+
# summary = get_financial_summary()
|
155 |
+
summary = chatbot_respond("What is the financial summary?", [])[-1][
|
156 |
+
-1
|
157 |
+
]["content"]
|
158 |
+
advice = chatbot_respond(
|
159 |
+
"What financial advice can you give? Give general advice, do not give anything specific to my transactions",
|
160 |
+
[],
|
161 |
+
)[-1][-1]["content"]
|
162 |
+
gr.Markdown(
|
163 |
+
f"""### Summary\n{summary}\n\n### Financial Advice\n{advice}"""
|
164 |
+
)
|
165 |
|
166 |
minimized_state = gr.State(False)
|
167 |
|
168 |
chat_panel = gr.Column(visible=True, elem_classes="chat-panel")
|
169 |
with chat_panel:
|
170 |
+
gr.Markdown("### Financial Agent")
|
171 |
chatbot_state = gr.State([])
|
172 |
chatbot_ui = gr.Chatbot(type="messages")
|
173 |
chatbot_input = gr.Textbox(
|
174 |
+
placeholder="Type your message...",
|
175 |
+
label="Your Message",
|
176 |
+
elem_id="chatbot-input",
|
177 |
)
|
178 |
with gr.Row():
|
179 |
send_btn = gr.Button("Send")
|
|
|
182 |
|
183 |
open_btn = gr.Button("Open Chat", elem_id="open-chat-btn", visible=False)
|
184 |
|
185 |
+
chatbot_input.submit(
|
186 |
+
fn=chatbot_respond,
|
187 |
+
inputs=[chatbot_input, chatbot_state],
|
188 |
+
outputs=[chatbot_input, chatbot_state, chatbot_ui],
|
189 |
+
queue=False,
|
190 |
+
)
|
191 |
send_btn.click(
|
192 |
fn=chatbot_respond,
|
193 |
inputs=[chatbot_input, chatbot_state],
|