Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Global variables
|
6 |
+
expenses = []
|
7 |
+
|
8 |
+
# Add Expense
|
9 |
+
def add_expense(description, amount, payer, participants):
|
10 |
+
global expenses
|
11 |
+
participants = participants.split(",") # Split participants by comma
|
12 |
+
amount = float(amount)
|
13 |
+
expense = {
|
14 |
+
"Description": description,
|
15 |
+
"Amount": amount,
|
16 |
+
"Payer": payer,
|
17 |
+
"Participants": participants,
|
18 |
+
"Split Amount": round(amount / len(participants), 2),
|
19 |
+
}
|
20 |
+
expenses.append(expense)
|
21 |
+
return pd.DataFrame(expenses)
|
22 |
+
|
23 |
+
# Optimize Balances
|
24 |
+
def optimize_balances():
|
25 |
+
global expenses
|
26 |
+
# Create a balance sheet
|
27 |
+
balances = {}
|
28 |
+
for expense in expenses:
|
29 |
+
payer = expense["Payer"]
|
30 |
+
participants = expense["Participants"]
|
31 |
+
split_amount = expense["Split Amount"]
|
32 |
+
|
33 |
+
# Adjust balances for payer
|
34 |
+
balances[payer] = balances.get(payer, 0) + (len(participants) * split_amount - expense["Amount"])
|
35 |
+
|
36 |
+
# Adjust balances for participants
|
37 |
+
for participant in participants:
|
38 |
+
if participant != payer:
|
39 |
+
balances[participant] = balances.get(participant, 0) - split_amount
|
40 |
+
|
41 |
+
# Simplify debts
|
42 |
+
def simplify_debts(balances):
|
43 |
+
transactions = []
|
44 |
+
debtors = {person: bal for person, bal in balances.items() if bal < 0}
|
45 |
+
creditors = {person: bal for person, bal in balances.items() if bal > 0}
|
46 |
+
|
47 |
+
while debtors and creditors:
|
48 |
+
# Pick a debtor and creditor
|
49 |
+
debtor, debt_amount = min(debtors.items(), key=lambda x: x[1])
|
50 |
+
creditor, credit_amount = max(creditors.items(), key=lambda x: x[1])
|
51 |
+
|
52 |
+
# Settle the minimum of the two
|
53 |
+
settled_amount = min(-debt_amount, credit_amount)
|
54 |
+
transactions.append(f"{debtor} pays {creditor} ${settled_amount:.2f}")
|
55 |
+
|
56 |
+
# Update balances
|
57 |
+
balances[debtor] += settled_amount
|
58 |
+
balances[creditor] -= settled_amount
|
59 |
+
|
60 |
+
# Remove settled accounts
|
61 |
+
if balances[debtor] == 0:
|
62 |
+
del debtors[debtor]
|
63 |
+
else:
|
64 |
+
debtors[debtor] = balances[debtor]
|
65 |
+
|
66 |
+
if balances[creditor] == 0:
|
67 |
+
del creditors[creditor]
|
68 |
+
else:
|
69 |
+
creditors[creditor] = balances[creditor]
|
70 |
+
|
71 |
+
return transactions
|
72 |
+
|
73 |
+
return simplify_debts(balances)
|
74 |
+
|
75 |
+
# Reset App
|
76 |
+
def reset():
|
77 |
+
global expenses
|
78 |
+
expenses = []
|
79 |
+
return pd.DataFrame(expenses), []
|
80 |
+
|
81 |
+
# Gradio Interface
|
82 |
+
with gr.Blocks() as app:
|
83 |
+
gr.Markdown("# Expense Splitter App")
|
84 |
+
|
85 |
+
with gr.Row():
|
86 |
+
with gr.Column():
|
87 |
+
description = gr.Textbox(label="Description", placeholder="e.g., Dinner")
|
88 |
+
amount = gr.Number(label="Amount", placeholder="e.g., 100")
|
89 |
+
payer = gr.Textbox(label="Payer", placeholder="e.g., Alice")
|
90 |
+
participants = gr.Textbox(label="Participants", placeholder="e.g., Alice,Bob,Charlie")
|
91 |
+
add_btn = gr.Button("Add Expense")
|
92 |
+
with gr.Column():
|
93 |
+
expense_table = gr.Dataframe(headers=["Description", "Amount", "Payer", "Participants", "Split Amount"], datatype=["str", "number", "str", "list", "number"])
|
94 |
+
|
95 |
+
add_btn.click(add_expense, inputs=[description, amount, payer, participants], outputs=expense_table)
|
96 |
+
|
97 |
+
with gr.Row():
|
98 |
+
optimize_btn = gr.Button("Optimize Balances")
|
99 |
+
result = gr.Textbox(label="Transactions", lines=5)
|
100 |
+
reset_btn = gr.Button("Reset")
|
101 |
+
|
102 |
+
optimize_btn.click(optimize_balances, inputs=[], outputs=result)
|
103 |
+
reset_btn.click(reset, inputs=[], outputs=[expense_table, result])
|
104 |
+
|
105 |
+
app.launch()
|