Shrijeeth-Suresh commited on
Commit
01c92a0
·
1 Parent(s): 4b2a563

feat: add Makefile and integrate live financial data with auto-refresh charts

Browse files
Files changed (3) hide show
  1. Makefile +14 -0
  2. app.py +61 -21
  3. requirements.txt +2 -1
Makefile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ format:
2
+ python -m ruff format .
3
+
4
+ lint:
5
+ python -m ruff check --select I,RUF022 --fix .
6
+
7
+ check:
8
+ python -m ruff check .
9
+
10
+ install:
11
+ pip install -r requirements.txt
12
+
13
+ run:
14
+ python app.py
app.py CHANGED
@@ -1,37 +1,68 @@
 
 
1
  import gradio as gr
2
- from gradio.themes import Default
3
  import pandas as pd
4
  import plotly.express as px
5
  import requests
 
6
 
7
 
8
  def get_financial_summary():
9
  try:
10
- response = requests.get("http://localhost:8000/api/financial_summary")
 
 
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
 
18
- def display_financial_charts():
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
@@ -163,11 +194,20 @@ with gr.Blocks(
163
  with gr.Column(scale=3):
164
  with gr.Tabs():
165
  with gr.TabItem("Charts"):
166
- fig2, fig3, fig4 = display_financial_charts()
167
- # gr.Plot(fig1)
168
- gr.Plot(fig2)
169
- gr.Plot(fig3)
170
- gr.Plot(fig4)
 
 
 
 
 
 
 
 
 
171
  with gr.TabItem("Overview"):
172
  # summary = get_financial_summary()
173
  summary = chatbot_respond("What is the financial summary?", [])[-1][
 
1
+ import datetime
2
+
3
  import gradio as gr
 
4
  import pandas as pd
5
  import plotly.express as px
6
  import requests
7
+ from gradio.themes import Default
8
 
9
 
10
  def get_financial_summary():
11
  try:
12
+ response = requests.get(
13
+ f"https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/api/transactions/monthly-summary/{datetime.datetime.now().year}",
14
+ )
15
  response.raise_for_status()
16
  data = response.json()
17
  except Exception:
18
+ data = {
19
+ "months": [
20
+ "Jan",
21
+ "Feb",
22
+ "Mar",
23
+ "Apr",
24
+ "May",
25
+ "Jun",
26
+ "Jul",
27
+ "Aug",
28
+ "Sep",
29
+ "Oct",
30
+ "Nov",
31
+ "Dec",
32
+ ],
33
+ "credits": [1000, 1200, 900, 1300, 1500, 1700],
34
+ "expenses": [400, 600, 500, 700, 800, 900],
35
+ }
36
  return data
37
 
38
 
39
+ def get_category_distribution():
40
+ try:
41
+ response = requests.get(
42
+ f"https://green-smoke-labs-dev--green-smoke-labs-expensynth-api-server.modal.run/api/transactions/category-distribution/{datetime.datetime.now().year}",
43
+ )
44
+ response.raise_for_status()
45
+ data = response.json()
46
+ except Exception:
47
+ data = {
48
+ "categories": ["Food", "Retail", "Others"],
49
+ "distribution": [40, 30, 30],
50
  }
51
+ return data
52
+
53
+
54
+ def display_financial_charts():
55
+ data = get_financial_summary()
56
+ df = pd.DataFrame(data)
57
+ df["savings"] = df["credits"] - df["expenses"]
58
 
59
  # fig1 = px.line(df, x="Month", y="Credit", title="Monthly Credit")
60
+ fig2 = px.bar(df, x="months", y="expenses", title="Monthly Expenses")
61
+ fig3 = px.area(df, x="months", y="credits", title="Monthly Savings")
62
+ latest = get_category_distribution()
63
  fig4 = px.pie(
64
+ names=latest["categories"],
65
+ values=latest["distribution"],
66
  title="Latest Financial Distribution",
67
  )
68
  return fig2, fig3, fig4
 
194
  with gr.Column(scale=3):
195
  with gr.Tabs():
196
  with gr.TabItem("Charts"):
197
+ chart1 = gr.Plot(label="Monthly Expenses")
198
+ chart2 = gr.Plot(label="Monthly Savings")
199
+ chart3 = gr.Plot(label="Latest Financial Distribution")
200
+
201
+ def reload_charts():
202
+ fig2, fig3, fig4 = display_financial_charts()
203
+ return fig2, fig3, fig4
204
+
205
+ # Initial load
206
+ demo.load(reload_charts, outputs=[chart1, chart2, chart3])
207
+
208
+ # Auto-refresh every 1 hour (3600 seconds)
209
+ timer = gr.Timer(value=60, active=True)
210
+ timer.tick(fn=reload_charts, outputs=[chart1, chart2, chart3])
211
  with gr.TabItem("Overview"):
212
  # summary = get_financial_summary()
213
  summary = chatbot_respond("What is the financial summary?", [])[-1][
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
  pandas
2
  requests
3
  plotly
4
- gradio
 
 
1
  pandas
2
  requests
3
  plotly
4
+ gradio
5
+ ruff