File size: 6,414 Bytes
d48fa9e
89e6f0a
d48fa9e
 
 
 
 
 
 
 
 
 
 
34c9847
d48fa9e
 
 
 
 
 
 
34c9847
d48fa9e
 
 
34c9847
d48fa9e
34c9847
d48fa9e
34c9847
d48fa9e
 
34c9847
 
d48fa9e
 
34c9847
d48fa9e
 
 
 
34c9847
 
d48fa9e
 
 
 
 
 
 
 
 
34c9847
d48fa9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89e6f0a
d48fa9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b2a563
d48fa9e
2be6cd2
d48fa9e
34c9847
d48fa9e
 
 
 
 
34c9847
4b2a563
d48fa9e
 
 
 
 
2be6cd2
d48fa9e
 
34c9847
 
 
418816e
d48fa9e
d664722
 
 
d2a05eb
d664722
 
 
 
 
 
 
 
 
 
 
d48fa9e
 
 
 
 
 
 
 
34c9847
 
 
 
d48fa9e
 
 
 
34c9847
 
d48fa9e
 
 
34c9847
 
 
 
 
 
 
 
 
 
 
 
d48fa9e
 
 
 
 
34c9847
 
 
 
 
 
d48fa9e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import gradio as gr
from gradio.themes import Default
import pandas as pd
import plotly.express as px
import requests


def get_financial_summary():
    try:
        response = requests.get("http://localhost:8000/api/financial_summary")
        response.raise_for_status()
        data = response.json()
    except Exception:
        data = {"revenue": 1000, "expenses": 500, "savings": 500}
    return data


def display_financial_charts():
    df = pd.DataFrame(
        {
            "Month": ["Jan", "Feb", "Mar", "Apr", "May", "Jun"],
            "Credit": [1000, 1200, 900, 1300, 1500, 1700],
            "Expenses": [400, 600, 500, 700, 800, 900],
        }
    )
    df["Savings"] = df["Credit"] - df["Expenses"]

    # fig1 = px.line(df, x="Month", y="Credit", title="Monthly Credit")
    fig2 = px.bar(df, x="Month", y="Expenses", title="Monthly Expenses")
    fig3 = px.area(df, x="Month", y="Savings", title="Monthly Savings")
    latest = df.iloc[-1]
    fig4 = px.pie(
        names=["Food", "Retail", "Others"],
        values=[latest["Credit"], latest["Expenses"], latest["Savings"]],
        title="Latest Financial Distribution",
    )
    return fig2, fig3, fig4


def chatbot_respond(user_message, history):
    history = history or []
    if user_message:
        history.append({"role": "user", "content": user_message})
    try:
        response = requests.post(
            "https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/bot/query",
            json={"messages": history},
        )
        response.raise_for_status()
        bot_reply = (
            response.json().get("data", {}).get("raw", "Sorry, I didn't understand.")
        )
    except Exception:
        bot_reply = "Server unavailable. This is a mocked reply."
    history.append({"role": "assistant", "content": bot_reply})

    return "", history, history


def reset_chat():
    """Reset chat history and clear the chatbot text box."""
    return "", [], []


def minimize_chat():
    return gr.update(visible=False), gr.update(visible=True)


def restore_chat():
    return gr.update(visible=True), gr.update(visible=False)


# Frontend

with gr.Blocks(
    theme=Default(),
    css="""
    #profile-pic-wrapper {
        width: 100px;
        height: 100px;
        border-radius: 50%;
        overflow: hidden;
        display: flex;
        align-items: center;
        justify-content: center;
        margin: 0 auto 10px auto;
        background: #fff;
    }
    #profile-icon {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: 50%;
        display: block;
        margin: 0;
    }
    .header h1 {
        margin: 0;
        font-family: 'Arial', sans-serif;
        color: #333;
    }
    .left-navbar {
        background-color: #fff;
        padding: 20px;
        border-right: 1px solid #ddd;
        min-height: 80vh;
    }
    .chat-panel {
        position: fixed;
        top: 20px;
        right: 20px;
        left: auto;
        width: 350px;
        height: 620px;
        background: #fff;
        border: 1px solid #ddd;
        box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
        border-radius: 8px;
        padding: 10px;
        overflow: visible;
        z-index: 1001;
    }
    #open-chat-btn {
        position: fixed;
        bottom: 20px;
        right: 20px;
        left: auto;
        z-index: 1000;
    }
    #chatbot-input {
        height: 150px;
    }
    """,
) as demo:
    chat_panel = gr.Column(visible=True, elem_classes="chat-panel")

    with chat_panel:
        gr.Markdown("### Financial Agent", height=200)
        chatbot_state = gr.State([])
        chatbot_ui = gr.Chatbot(type="messages")
        chatbot_input = gr.Textbox(
            placeholder="Type your message...",
            label="Your Message",
            elem_id="chatbot-input",
        )
        with gr.Row():
            send_btn = gr.Button("Send")
            reset_btn = gr.Button("Reset Chat")
            minimize_btn = gr.Button("Minimize", elem_id="minimize-chat-btn")

    with gr.Row():
        # gr.HTML(
        #     '<div id="profile-pic-wrapper"><img id="profile-icon" src="assets/profile_pic.png" /></div>'
        # )
        gr.Markdown("<h1>Financial Health Dashboard</h1>", elem_classes="header")

    with gr.Row():
        # with gr.Column(scale=1, elem_classes="left-navbar"):
        #     gr.Markdown("## Navigation")
        #     for nav in ["Dashboard", "Reports", "Analytics", "Settings"]:
        #         gr.Button(nav)

        with gr.Column(scale=3):
            with gr.Tabs():
                with gr.TabItem("Charts"):
                    fig2, fig3, fig4 = display_financial_charts()
                    # gr.Plot(fig1)
                    gr.Plot(fig2)
                    gr.Plot(fig3)
                    gr.Plot(fig4)
                with gr.TabItem("Overview"):
                    # summary = get_financial_summary()
                    summary = chatbot_respond("What is the financial summary?", [])[-1][
                        -1
                    ]["content"]
                    advice = chatbot_respond(
                        "What financial advice can you give? Give general advice, do not give anything specific to my transactions",
                        [],
                    )[-1][-1]["content"]
                    gr.Markdown(
                        f"""### Summary\n{summary}\n\n### Financial Advice\n{advice}"""
                    )

    minimized_state = gr.State(False)

    open_btn = gr.Button("Open Chat", elem_id="open-chat-btn", visible=False)

    chatbot_input.submit(
        fn=chatbot_respond,
        inputs=[chatbot_input, chatbot_state],
        outputs=[chatbot_input, chatbot_state, chatbot_ui],
        queue=False,
    )
    send_btn.click(
        fn=chatbot_respond,
        inputs=[chatbot_input, chatbot_state],
        outputs=[chatbot_input, chatbot_state, chatbot_ui],
        queue=False,
    )
    reset_btn.click(
        fn=reset_chat,
        inputs=None,
        outputs=[chatbot_input, chatbot_state, chatbot_ui],
        queue=False,
    )
    minimize_btn.click(
        fn=minimize_chat,
        inputs=None,
        outputs=[chat_panel, open_btn],
        queue=False,
    )
    open_btn.click(
        fn=restore_chat,
        inputs=None,
        outputs=[chat_panel, open_btn],
        queue=False,
    )

demo.launch()