File size: 2,569 Bytes
96577b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr

# STORAGE
employees = []
products = []
sales = []

# FUNCTIONS

def add_employee(name, role):
    employees.append({"name": name, "role": role})
    return show_all()

def add_product(name, price, qty):
    products.append({"name": name, "price": float(price), "qty": int(qty)})
    return show_all()

def add_sale(product_name, qty):
    qty = int(qty)

    for p in products:
        if p["name"] == product_name:
            if p["qty"] >= qty:
                p["qty"] -= qty
                total = qty * p["price"]
                sales.append({"name": product_name, "qty": qty, "total": total})
                return show_all()
            else:
                return "Stock ntihagije", show_all()

    return "Product ntibashije kuboneka", show_all()


def show_all():
    emp_table = "\n".join([f'{e["name"]} - {e["role"]}' for e in employees]) or "No employees"
    prod_table = "\n".join([f'{p["name"]} | {p["price"]} | {p["qty"]}' for p in products]) or "No products"
    sale_table = "\n".join([f'{s["name"]} | {s["qty"]} | {s["total"]}' for s in sales]) or "No sales"

    total_sales = sum(s["total"] for s in sales)

    dashboard = f"""
EMPLOYEES: {len(employees)}
PRODUCTS: {len(products)}
TOTAL SALES: {total_sales}
"""

    return dashboard, emp_table, prod_table, sale_table


# UI
with gr.Blocks() as app:
    gr.Markdown("# 🏢 COMPANY ERP SYSTEM (Gradio + HF)")

    dashboard = gr.Textbox(label="Dashboard")

    with gr.Tab("Employees"):
        name = gr.Textbox(label="Name")
        role = gr.Textbox(label="Role")
        btn = gr.Button("Add Employee")

    with gr.Tab("Products"):
        pname = gr.Textbox(label="Product Name")
        price = gr.Textbox(label="Price")
        qty = gr.Textbox(label="Quantity")
        btn2 = gr.Button("Add Product")

    with gr.Tab("Sales"):
        sname = gr.Textbox(label="Product Name")
        sqty = gr.Textbox(label="Quantity")
        btn3 = gr.Button("Sell Product")

    emp_table = gr.Textbox(label="Employees")
    prod_table = gr.Textbox(label="Products")
    sale_table = gr.Textbox(label="Sales")

    btn.click(add_employee, inputs=[name, role],
              outputs=[dashboard, emp_table, prod_table, sale_table])

    btn2.click(add_product, inputs=[pname, price, qty],
               outputs=[dashboard, emp_table, prod_table, sale_table])

    btn3.click(add_sale, inputs=[sname, sqty],
               outputs=[dashboard, emp_table, prod_table, sale_table])

    app.load(show_all, outputs=[dashboard, emp_table, prod_table, sale_table])

app.launch()