sdacelo commited on
Commit
8a586f6
·
1 Parent(s): 104bfad

first commit

Browse files
Files changed (4) hide show
  1. Dockerfile +13 -0
  2. app.py +105 -0
  3. ecommerce_fake_data.py +161 -0
  4. requirements.txt +97 -0
Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ RUN useradd -m -u 1000 user
4
+ USER user
5
+ ENV PATH="/home/user/.local/bin:$PATH"
6
+
7
+ WORKDIR /app
8
+
9
+ COPY --chown=user ./requirements.txt requirements.txt
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
11
+
12
+ COPY --chown=user . /app
13
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, HTTPException
2
+ from pydantic import BaseModel
3
+ from typing import List
4
+ from ecommerce_fake_data import EcommerceFakeData
5
+
6
+ app = FastAPI()
7
+
8
+ efd = EcommerceFakeData()
9
+
10
+ efd.generate(n_cust=500, n_prod=300, n_sell=300, n_ord=1000)
11
+
12
+ class Customer(BaseModel):
13
+ customer_id: str
14
+ name: str
15
+ email: str
16
+ lat: float
17
+ lon: float
18
+ signup_date: str
19
+
20
+ class Product(BaseModel):
21
+ product_id: str
22
+ category: str
23
+ price: float
24
+
25
+ class Seller(BaseModel):
26
+ seller_id: str
27
+ lat: float
28
+ lon: float
29
+
30
+ class Order(BaseModel):
31
+ order_id: str
32
+ customer_id: str
33
+ product_id: str
34
+ seller_id: str
35
+ order_date: str
36
+ quantity: int
37
+ total_amount: float
38
+
39
+ class Payment(BaseModel):
40
+ payment_id: str
41
+ order_id: str
42
+ payment_method: str
43
+ payment_status: str
44
+ payment_date: str
45
+
46
+
47
+ # GET
48
+ @app.get("/customers/", response_model=List[Customer])
49
+ def get_customers():
50
+ return efd.customers
51
+
52
+ @app.get("/products/", response_model=List[Product])
53
+ def get_products():
54
+ return efd.products
55
+
56
+ @app.get("/sellers/", response_model=List[Seller])
57
+ def get_sellers():
58
+ return efd.sellers
59
+
60
+ @app.get("/orders/", response_model=List[Order])
61
+ def get_orders():
62
+ return efd.orders
63
+
64
+ @app.get("/payments/", response_model=List[Payment])
65
+ def get_payments():
66
+ return efd.payments
67
+
68
+ # POST
69
+ @app.post("/customers/")
70
+ def create_customer(customer: Customer):
71
+ efd.customers.append(customer.model_dump())
72
+ return {"message": "Client ajouté avec succès !"}
73
+
74
+ @app.post("/products/")
75
+ def create_product(product: Product):
76
+ efd.products.append(product.model_dump())
77
+ return {"message": "Produit ajouté avec succès !"}
78
+
79
+ # PUT
80
+ @app.put("/customers/{customer_id}")
81
+ def update_customer(customer_id: str, updated_customer: Customer):
82
+ for customer in efd.customers:
83
+ if customer["customer_id"] == customer_id:
84
+ customer.update(updated_customer.model_dump())
85
+ return {"message": "Client mis à jour avec succès !"}
86
+ raise HTTPException(status_code=404, detail="Client non trouvé")
87
+
88
+ @app.put("/products/{product_id}")
89
+ def update_product(product_id: str, updated_product: Product):
90
+ for product in efd.products:
91
+ if product["product_id"] == product_id:
92
+ product.update(updated_product.model_dump())
93
+ return {"message": "Produit mis à jour avec succès !"}
94
+ raise HTTPException(status_code=404, detail="Produit non trouvé")
95
+
96
+ # DELETE
97
+ @app.delete("/customers/{customer_id}")
98
+ def delete_customer(customer_id: str):
99
+ efd.customers = [c for c in efd.customers if c["customer_id"] != customer_id]
100
+ return {"message": "Client supprimé avec succès !"}
101
+
102
+ @app.delete("/products/{product_id}")
103
+ def delete_product(product_id: str):
104
+ efd.products = [p for p in efd.products if p["product_id"] != product_id]
105
+ return {"message": "Produit supprimé avec succès !"}
ecommerce_fake_data.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from faker import Faker
2
+ import random
3
+
4
+ SEED = 42
5
+ random.seed(SEED)
6
+ fake = Faker()
7
+ fake.seed_instance(SEED)
8
+
9
+
10
+ class EcommerceFakeData:
11
+ '''
12
+ Generates fake e-commerce data, including customers, products, orders, and payments.
13
+ '''
14
+
15
+ def __init__(self):
16
+ '''
17
+ Initializes the FakeCustomerData class.
18
+
19
+ Parameters:
20
+ n (int): The number of records to generate for each dataset.
21
+ '''
22
+ self.n = 0
23
+ self.customers = None
24
+ self.products = None
25
+ self.sellers = None
26
+ self.orders = None
27
+ self.payments = None
28
+ self.signup_date = fake.date_between(start_date='-2y', end_date='today')
29
+
30
+ def generate_customers(self, n) -> list[dict]:
31
+ '''
32
+ Generates fake customer data, including:
33
+ - customer_id: Unique identifier for the customer
34
+ - name: Full name of the customer
35
+ - location: Geographical location (latitude, longitude, city, country)
36
+ - signup_date: Date the customer signed up (formatted as YYYY-MM-DD)
37
+ - email: Customer's email address, generated based on their name
38
+
39
+ Returns:
40
+ list[dict]: A list of dictionaries containing customer details.
41
+ '''
42
+ customers = []
43
+ for _ in range(n):
44
+ lat, lon , *_ = fake.local_latlng()
45
+ customers.append({
46
+ "customer_id": fake.uuid4(),
47
+ "name": fake.name(),
48
+ "lat": lat,
49
+ "lon": lon,
50
+ "signup_date": self.signup_date.strftime('%Y-%m-%d')
51
+ })
52
+
53
+ for customer in customers:
54
+ customer["email"] = f"{customer['name'].replace(' ', '').lower()}@mail.com"
55
+
56
+ return customers
57
+
58
+ def generate_products(self, n) -> list[dict]:
59
+ '''
60
+ Generates fake product data, including:
61
+ - product_id: Unique identifier for the product
62
+ - category: Randomly chosen category from a predefined list
63
+ - price: Randomly generated price between $5 and $200
64
+
65
+ Returns:
66
+ list[dict]: A list of dictionaries containing product details.
67
+ '''
68
+ categories = ["Electronics", "Books", "Clothing", "Appliances", "Toys", "Beauty", "Furniture", "Tools", "Pets"]
69
+ products = []
70
+ for _ in range(n):
71
+ products.append({
72
+ "product_id": fake.uuid4(),
73
+ "category": random.choice(categories),
74
+ "price": round(random.uniform(5, 200), 2)
75
+ })
76
+ return products
77
+
78
+ def generate_sellers(self, n) -> list[dict]:
79
+ '''
80
+ Generates fake seller data, including:
81
+ - seller_id: Unique identifier for the seller
82
+ - location: Geographical location (latitude, longitude, city, country)
83
+
84
+ Returns:
85
+ list[dict]: A list of dictionaries containing product details.
86
+ '''
87
+ sellers = []
88
+ for _ in range(n):
89
+ lat, lon, *_ = fake.local_latlng()
90
+ sellers.append({
91
+ "seller_id": fake.uuid4(),
92
+ "lat": lat,
93
+ "lon": lon
94
+ })
95
+ return sellers
96
+
97
+ def generate_orders(self, n) -> list[dict]:
98
+ '''
99
+ Generates fake order data, including:
100
+ - order_id: Unique identifier for the order
101
+ - customer_id: Associated customer ID
102
+ - order_date: Date of the order, within the signup date range
103
+ - product_id: ID of the purchased product
104
+ - seller_id : ID of the seller
105
+ - quantity: Random quantity (between 1 and 5)
106
+ - total_amount: Calculated total amount based on quantity and product price
107
+
108
+ Returns:
109
+ list[dict]: A list of dictionaries containing order details.
110
+ '''
111
+ orders = []
112
+ for _ in range(n):
113
+ customer: dict = random.choice(self.customers)
114
+ product: dict = random.choice(self.products)
115
+ seller: dict = random.choice(self.sellers)
116
+ quantity: int = random.randint(1, 5)
117
+ orders.append({
118
+ "order_id": fake.uuid4(),
119
+ "customer_id": customer["customer_id"],
120
+ "order_date": fake.date_between_dates(self.signup_date).strftime('%Y-%m-%d'),
121
+ "product_id": product["product_id"],
122
+ "seller_id": seller["seller_id"],
123
+ "quantity": quantity,
124
+ "total_amount": round(product["price"] * quantity, 2)
125
+ })
126
+ return orders
127
+
128
+ def generate_payments(self) -> list[dict]:
129
+ '''
130
+ Generates fake payment data, including:
131
+ - payment_id: Unique identifier for the payment
132
+ - order_id: Associated order ID
133
+ - payment_method: Randomly chosen method (Card, PayPal, Wire)
134
+ - payment_status: Randomly assigned status (Successful, Failed, Pending) with weighted probability
135
+ - payment_date: Date of payment (same as order date)
136
+
137
+ Returns:
138
+ list[dict]: A list of dictionaries containing payment details.
139
+ '''
140
+ payments = []
141
+ methods = ["Card", "PayPal", "Wire"]
142
+ statuses = ["Successful", "Failed", "Pending"]
143
+ for order in self.orders:
144
+ payments.append({
145
+ "payment_id": fake.uuid4(),
146
+ "order_id": order["order_id"],
147
+ "payment_method": random.choice(methods),
148
+ "payment_status": random.choices(statuses, weights=[80, 15, 5])[0],
149
+ "payment_date": order["order_date"]
150
+ })
151
+ return payments
152
+
153
+ def generate(self, n_cust, n_prod, n_sell, n_ord):
154
+ '''
155
+ Generates all datasets in sequence: customers, products, orders, and payments.
156
+ '''
157
+ self.customers = self.generate_customers(n_cust)
158
+ self.products = self.generate_products(n_prod)
159
+ self.sellers = self.generate_sellers(n_sell)
160
+ self.orders = self.generate_orders(n_ord)
161
+ self.payments = self.generate_payments()
requirements.txt ADDED
@@ -0,0 +1,97 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiofiles==23.2.1
2
+ annotated-types==0.7.0
3
+ anyio==4.8.0
4
+ appnope==0.1.4
5
+ asttokens==3.0.0
6
+ audioop-lts==0.2.1
7
+ branca==0.8.1
8
+ certifi==2025.1.31
9
+ charset-normalizer==3.4.1
10
+ click==8.1.8
11
+ comm==0.2.2
12
+ contourpy==1.3.1
13
+ cycler==0.12.1
14
+ debugpy==1.8.12
15
+ decorator==5.2.1
16
+ dnspython==2.7.0
17
+ email_validator==2.2.0
18
+ executing==2.2.0
19
+ Faker==36.1.1
20
+ fastapi==0.115.8
21
+ fastapi-cli==0.0.7
22
+ ffmpy==0.5.0
23
+ filelock==3.17.0
24
+ folium==0.19.5
25
+ fonttools==4.56.0
26
+ fsspec==2025.2.0
27
+ geographiclib==2.0
28
+ geopy==2.4.1
29
+ groovy==0.1.2
30
+ h11==0.14.0
31
+ httpcore==1.0.7
32
+ httptools==0.6.4
33
+ httpx==0.28.1
34
+ huggingface-hub==0.29.2
35
+ idna==3.10
36
+ ipykernel==6.29.5
37
+ ipython==8.32.0
38
+ jedi==0.19.2
39
+ Jinja2==3.1.5
40
+ jupyter_client==8.6.3
41
+ jupyter_core==5.7.2
42
+ kiwisolver==1.4.8
43
+ markdown-it-py==3.0.0
44
+ MarkupSafe==2.1.5
45
+ matplotlib==3.10.1
46
+ matplotlib-inline==0.1.7
47
+ mdurl==0.1.2
48
+ nest-asyncio==1.6.0
49
+ numpy==2.2.3
50
+ orjson==3.10.15
51
+ packaging==24.2
52
+ pandas==2.2.3
53
+ parso==0.8.4
54
+ pexpect==4.9.0
55
+ pillow==11.1.0
56
+ platformdirs==4.3.6
57
+ prompt_toolkit==3.0.50
58
+ psutil==7.0.0
59
+ ptyprocess==0.7.0
60
+ pure_eval==0.2.3
61
+ pydantic==2.10.6
62
+ pydantic_core==2.27.2
63
+ pydub==0.25.1
64
+ Pygments==2.19.1
65
+ pyparsing==3.2.1
66
+ python-dateutil==2.9.0.post0
67
+ python-dotenv==1.0.1
68
+ python-multipart==0.0.20
69
+ pytz==2025.1
70
+ PyYAML==6.0.2
71
+ pyzmq==26.2.1
72
+ requests==2.32.3
73
+ rich==13.9.4
74
+ rich-toolkit==0.13.2
75
+ ruff==0.9.9
76
+ safehttpx==0.1.6
77
+ seaborn==0.13.2
78
+ semantic-version==2.10.0
79
+ shellingham==1.5.4
80
+ six==1.17.0
81
+ sniffio==1.3.1
82
+ stack-data==0.6.3
83
+ starlette==0.45.3
84
+ tomlkit==0.13.2
85
+ tornado==6.4.2
86
+ tqdm==4.67.1
87
+ traitlets==5.14.3
88
+ typer==0.15.1
89
+ typing_extensions==4.12.2
90
+ tzdata==2025.1
91
+ urllib3==2.3.0
92
+ uvicorn==0.34.0
93
+ uvloop==0.21.0
94
+ watchfiles==1.0.4
95
+ wcwidth==0.2.13
96
+ websockets==15.0
97
+ xyzservices==2025.1.0