Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from datasets import load_dataset
|
4 |
+
|
5 |
+
app = FastAPI()
|
6 |
+
|
7 |
+
# Load the datasets from the Hugging Face Hub
|
8 |
+
customers_dataset = load_dataset("dwb2023/customers")["train"]
|
9 |
+
orders_dataset = load_dataset("dwb2023/orders")["train"]
|
10 |
+
|
11 |
+
class GetUserInput(BaseModel):
|
12 |
+
key: str
|
13 |
+
value: str
|
14 |
+
|
15 |
+
class UpdateUserInput(BaseModel):
|
16 |
+
user_id: str
|
17 |
+
email: str = None
|
18 |
+
phone: str = None
|
19 |
+
|
20 |
+
class GetOrderByIdInput(BaseModel):
|
21 |
+
order_id: str
|
22 |
+
|
23 |
+
class GetCustomerOrdersInput(BaseModel):
|
24 |
+
customer_id: str
|
25 |
+
|
26 |
+
class CancelOrderInput(BaseModel):
|
27 |
+
order_id: str
|
28 |
+
|
29 |
+
class GetUserInfoInput(BaseModel):
|
30 |
+
key: str
|
31 |
+
value: str
|
32 |
+
|
33 |
+
@app.post("/get_user")
|
34 |
+
def get_user(input: GetUserInput):
|
35 |
+
customer = [c for c in customers_dataset if c[input.key] == input.value]
|
36 |
+
if customer:
|
37 |
+
return customer[0]
|
38 |
+
else:
|
39 |
+
raise HTTPException(status_code=404, detail=f"Couldn't find a user with {input.key} of {input.value}")
|
40 |
+
|
41 |
+
@app.post("/update_user")
|
42 |
+
def update_user(input: UpdateUserInput):
|
43 |
+
global customers_dataset
|
44 |
+
customers_data = customers_dataset.to_list()
|
45 |
+
user_found = False
|
46 |
+
|
47 |
+
for customer in customers_data:
|
48 |
+
if customer["id"] == input.user_id:
|
49 |
+
if input.email:
|
50 |
+
customer["email"] = input.email
|
51 |
+
if input.phone:
|
52 |
+
customer["phone"] = input.phone
|
53 |
+
user_found = True
|
54 |
+
break
|
55 |
+
|
56 |
+
if user_found:
|
57 |
+
updated_dataset = Dataset.from_list(customers_data)
|
58 |
+
updated_dataset.push_to_hub("dwb2023/customers", split="train")
|
59 |
+
customers_dataset = updated_dataset # Update the global dataset
|
60 |
+
return {"message": "User information updated successfully"}
|
61 |
+
else:
|
62 |
+
raise HTTPException(status_code=404, detail=f"Couldn't find a user with ID {input.user_id}")
|
63 |
+
|
64 |
+
@app.post("/get_order_by_id")
|
65 |
+
def get_order_by_id(input: GetOrderByIdInput):
|
66 |
+
order = [o for o in orders_dataset if o["id"] == input.order_id]
|
67 |
+
if order:
|
68 |
+
return order[0]
|
69 |
+
else:
|
70 |
+
raise HTTPException(status_code=404, detail="Order not found")
|
71 |
+
|
72 |
+
@app.post("/get_customer_orders")
|
73 |
+
def get_customer_orders(input: GetCustomerOrdersInput):
|
74 |
+
customer_orders = [o for o in orders_dataset if o["customer_id"] == input.customer_id]
|
75 |
+
return customer_orders
|
76 |
+
|
77 |
+
@app.post("/cancel_order")
|
78 |
+
def cancel_order(input: CancelOrderInput):
|
79 |
+
global orders_dataset
|
80 |
+
orders_data = orders_dataset.to_list()
|
81 |
+
order_found = False
|
82 |
+
|
83 |
+
for order in orders_data:
|
84 |
+
if order["id"] == input.order_id:
|
85 |
+
if order["status"] == "Processing":
|
86 |
+
order["status"] = "Cancelled"
|
87 |
+
order_found = True
|
88 |
+
break
|
89 |
+
else:
|
90 |
+
raise HTTPException(status_code=400, detail="Order has already shipped. Can't cancel it.")
|
91 |
+
|
92 |
+
if order_found:
|
93 |
+
updated_orders_dataset = Dataset.from_list(orders_data)
|
94 |
+
updated_orders_dataset.push_to_hub("dwb2023/orders", split="train")
|
95 |
+
orders_dataset = updated_orders_dataset # Update the global dataset
|
96 |
+
return {"status": "Cancelled"}
|
97 |
+
else:
|
98 |
+
raise HTTPException(status_code=404, detail="Order not found")
|
99 |
+
|
100 |
+
@app.post("/get_user_info")
|
101 |
+
def get_user_info(input: GetUserInfoInput):
|
102 |
+
customer = [c for c in customers_dataset if c[input.key] == input.value]
|
103 |
+
if customer:
|
104 |
+
customer_id = customer[0]["id"]
|
105 |
+
customer_orders = [o for o in orders_dataset if o["customer_id"] == customer_id]
|
106 |
+
return {
|
107 |
+
"user_info": customer[0],
|
108 |
+
"orders": customer_orders
|
109 |
+
}
|
110 |
+
else:
|
111 |
+
raise HTTPException(status_code=404, detail=f"Couldn't find a user with {input.key} of {input.value}")
|