timfocus commited on
Commit
d850a05
·
verified ·
1 Parent(s): 2de1405

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -47
app.py CHANGED
@@ -1,18 +1,16 @@
1
- from fastapi import FastAPI
2
- from fastapi.responses import FileResponse
3
  import csv
4
  import random
5
  import datetime
6
- import uvicorn
7
 
8
- app = FastAPI()
9
 
10
  # Sample data for randomization
11
  shipper_names = ["Global Express", "Fast Ship Logistics", "Secure Courier"]
12
  cities_states = [("New York", "NY"), ("Los Angeles", "CA"), ("Chicago", "IL"), ("Houston", "TX"), ("Miami", "FL")]
13
  shipping_methods = ["FedEx", "UPS", "DHL", "USPS"]
14
  weight_range = (0.5, 50) # Min and max weight in kg
15
- csv_filename = "realistic_shipping_data.csv"
16
 
17
  # Function to generate a random phone number
18
  def random_phone():
@@ -25,55 +23,68 @@ def generate_tracking(carrier):
25
 
26
  # Function to generate the shipping CSV file
27
  def generate_csv():
28
- shipping_data = []
29
- for order_id in range(1001, 1011): # Generating 10 orders
30
- order_date = datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 30))
31
- delivery_date = order_date + datetime.timedelta(days=random.randint(2, 10))
 
 
 
 
 
 
 
 
 
32
 
33
- shipper = random.choice(shipper_names)
34
- shipper_city, shipper_state = random.choice(cities_states)
35
- receiver_city, receiver_state = random.choice(cities_states)
 
 
 
 
 
 
 
 
 
 
 
36
 
37
- shipping_method = random.choice(shipping_methods)
38
- weight = round(random.uniform(*weight_range), 2)
39
- tracking_number = generate_tracking(shipping_method)
 
40
 
41
- shipping_data.append({
42
- "Order ID": order_id,
43
- "Order Date": order_date.strftime("%Y-%m-%d %H:%M:%S"),
44
- "Shipper Name": shipper,
45
- "Shipper Address": f"{random.randint(100, 999)} {shipper_city} St, {shipper_city}, {shipper_state}",
46
- "Shipper Phone": random_phone(),
47
- "Receiver Name": f"Customer {order_id}",
48
- "Receiver Address": f"{random.randint(100, 999)} {receiver_city} Ave, {receiver_city}, {receiver_state}",
49
- "Receiver Phone": random_phone(),
50
- "Package Weight (kg)": weight,
51
- "Shipping Method": shipping_method,
52
- "Tracking Number": tracking_number,
53
- "Estimated Delivery Date": delivery_date.strftime("%Y-%m-%d")
54
- })
55
 
56
- # Define CSV column headers
57
- csv_columns = ["Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone", "Receiver Name",
58
- "Receiver Address", "Receiver Phone", "Package Weight (kg)", "Shipping Method", "Tracking Number",
59
- "Estimated Delivery Date"]
60
 
61
- # Write data to CSV file
62
- with open(csv_filename, mode="w", newline="") as file:
63
- writer = csv.DictWriter(file, fieldnames=csv_columns)
64
- writer.writeheader()
65
- writer.writerows(shipping_data)
66
 
67
- # API Route to Generate and Download CSV
68
- @app.get("/generate-csv")
69
  def download_csv():
70
- generate_csv()
71
- return FileResponse(csv_filename, media_type="text/csv", filename=csv_filename)
 
 
72
 
73
- @app.get("/")
74
- def root():
75
- return {"message": "Welcome to the Shipping CSV Generator API! Use /generate-csv to download the file."}
 
 
 
 
76
 
77
- # Ensure FastAPI starts correctly on Hugging Face
78
  if __name__ == "__main__":
79
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ import gradio as gr
 
2
  import csv
3
  import random
4
  import datetime
5
+ import os
6
 
7
+ csv_filename = "realistic_shipping_data.csv"
8
 
9
  # Sample data for randomization
10
  shipper_names = ["Global Express", "Fast Ship Logistics", "Secure Courier"]
11
  cities_states = [("New York", "NY"), ("Los Angeles", "CA"), ("Chicago", "IL"), ("Houston", "TX"), ("Miami", "FL")]
12
  shipping_methods = ["FedEx", "UPS", "DHL", "USPS"]
13
  weight_range = (0.5, 50) # Min and max weight in kg
 
14
 
15
  # Function to generate a random phone number
16
  def random_phone():
 
23
 
24
  # Function to generate the shipping CSV file
25
  def generate_csv():
26
+ try:
27
+ shipping_data = []
28
+ for order_id in range(1001, 1011): # Generating 10 orders
29
+ order_date = datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 30))
30
+ delivery_date = order_date + datetime.timedelta(days=random.randint(2, 10))
31
+
32
+ shipper = random.choice(shipper_names)
33
+ shipper_city, shipper_state = random.choice(cities_states)
34
+ receiver_city, receiver_state = random.choice(cities_states)
35
+
36
+ shipping_method = random.choice(shipping_methods)
37
+ weight = round(random.uniform(*weight_range), 2)
38
+ tracking_number = generate_tracking(shipping_method)
39
 
40
+ shipping_data.append({
41
+ "Order ID": order_id,
42
+ "Order Date": order_date.strftime("%Y-%m-%d %H:%M:%S"),
43
+ "Shipper Name": shipper,
44
+ "Shipper Address": f"{random.randint(100, 999)} {shipper_city} St, {shipper_city}, {shipper_state}",
45
+ "Shipper Phone": random_phone(),
46
+ "Receiver Name": f"Customer {order_id}",
47
+ "Receiver Address": f"{random.randint(100, 999)} {receiver_city} Ave, {receiver_city}, {receiver_state}",
48
+ "Receiver Phone": random_phone(),
49
+ "Package Weight (kg)": weight,
50
+ "Shipping Method": shipping_method,
51
+ "Tracking Number": tracking_number,
52
+ "Estimated Delivery Date": delivery_date.strftime("%Y-%m-%d")
53
+ })
54
 
55
+ # Define CSV column headers
56
+ csv_columns = ["Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone", "Receiver Name",
57
+ "Receiver Address", "Receiver Phone", "Package Weight (kg)", "Shipping Method", "Tracking Number",
58
+ "Estimated Delivery Date"]
59
 
60
+ # Write data to CSV file
61
+ with open(csv_filename, mode="w", newline="") as file:
62
+ writer = csv.DictWriter(file, fieldnames=csv_columns)
63
+ writer.writeheader()
64
+ writer.writerows(shipping_data)
 
 
 
 
 
 
 
 
 
65
 
66
+ print("✅ CSV file generated successfully!")
67
+ return csv_filename
 
 
68
 
69
+ except Exception as e:
70
+ print(f" Error generating CSV: {e}")
71
+ return None
 
 
72
 
73
+ # Gradio function to return the CSV file
 
74
  def download_csv():
75
+ file_path = generate_csv()
76
+ if file_path and os.path.exists(file_path):
77
+ return file_path
78
+ return "Error: CSV file not found."
79
 
80
+ # Gradio UI
81
+ with gr.Blocks() as demo:
82
+ gr.Markdown("# 📦 Shipping CSV Generator")
83
+ gr.Markdown("Click the button below to generate a shipping CSV file.")
84
+ btn = gr.Button("Generate CSV")
85
+ output = gr.File(label="Download your CSV")
86
+ btn.click(fn=download_csv, outputs=output)
87
 
88
+ # Launch Gradio app
89
  if __name__ == "__main__":
90
+ demo.launch()