import datetime import gradio as gr import pandas as pd import plotly.express as px import requests from gradio.themes import Default def get_financial_summary(): try: response = requests.get( f"https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/api/transactions/monthly-summary/{datetime.datetime.now().year}", ) response.raise_for_status() data = response.json() except Exception: data = { "months": [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ], "credits": [1000, 1200, 900, 1300, 1500, 1700], "expenses": [400, 600, 500, 700, 800, 900], } return data def get_category_distribution(): try: response = requests.get( f"https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/api/transactions/category-distribution/{datetime.datetime.now().year}", ) response.raise_for_status() data = response.json() except Exception: data = { "categories": ["Food", "Retail", "Others"], "distribution": [40, 30, 30], } return data def display_financial_charts(): data = get_financial_summary() df = pd.DataFrame(data) df["savings"] = df["credits"] - df["expenses"] # fig1 = px.line(df, x="Month", y="Credit", title="Monthly Credit") fig2 = px.bar(df, x="months", y="expenses", title="Monthly Expenses") fig3 = px.area(df, x="months", y="credits", title="Monthly Savings") latest = get_category_distribution() fig4 = px.pie( names=latest["categories"], values=latest["distribution"], 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( # '