Spaces:
Sleeping
Sleeping
File size: 3,221 Bytes
d850a05 e6419df d850a05 e6419df f2c94fe e6419df f2c94fe e6419df d850a05 f2c94fe d850a05 e6419df f2c94fe e6419df f2c94fe e6419df d850a05 f2c94fe d850a05 e6419df d850a05 e6419df d850a05 e6419df d850a05 e6419df d850a05 f2c94fe d850a05 8ebd0d2 d850a05 2de1405 d850a05 | 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 | import gradio as gr
import csv
import random
import datetime
import os
csv_filename = "shipping_data.csv"
# Sample data
shipper_names = ["Global Express", "Fast Ship Logistics", "Secure Courier"]
cities_states = [("New York", "NY"), ("Los Angeles", "CA"), ("Chicago", "IL"), ("Houston", "TX"), ("Miami", "FL")]
shipping_methods = ["FedEx", "UPS", "DHL", "USPS"]
weight_range = (0.5, 50) # Min and max weight in kg
def random_phone():
return f"+1-{random.randint(200, 999)}-{random.randint(100, 999)}-{random.randint(1000, 9999)}"
def generate_tracking(carrier):
prefix = {"FedEx": "FDX", "UPS": "UPS", "DHL": "DHL", "USPS": "USPS"}.get(carrier, "TRK")
return f"{prefix}-{random.randint(100000, 999999)}"
def generate_csv():
try:
shipping_data = []
for order_id in range(1001, 1011):
order_date = datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 30))
delivery_date = order_date + datetime.timedelta(days=random.randint(2, 10))
shipper = random.choice(shipper_names)
shipper_city, shipper_state = random.choice(cities_states)
receiver_city, receiver_state = random.choice(cities_states)
shipping_method = random.choice(shipping_methods)
weight = round(random.uniform(*weight_range), 2)
tracking_number = generate_tracking(shipping_method)
shipping_data.append([
order_id,
order_date.strftime("%Y-%m-%d %H:%M:%S"),
shipper,
f"{random.randint(100, 999)} {shipper_city} St, {shipper_city}, {shipper_state}",
random_phone(),
f"Customer {order_id}",
f"{random.randint(100, 999)} {receiver_city} Ave, {receiver_city}, {receiver_state}",
random_phone(),
weight,
shipping_method,
tracking_number,
delivery_date.strftime("%Y-%m-%d"),
])
csv_columns = ["Order ID", "Order Date", "Shipper Name", "Shipper Address", "Shipper Phone",
"Receiver Name", "Receiver Address", "Receiver Phone", "Package Weight (kg)",
"Shipping Method", "Tracking Number", "Estimated Delivery Date"]
with open(csv_filename, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(csv_columns)
writer.writerows(shipping_data)
print("✅ CSV file generated successfully!")
return csv_filename
except Exception as e:
print(f"❌ Error generating CSV: {e}")
return None
def download_csv():
file_path = generate_csv()
if file_path and os.path.exists(file_path):
return file_path
return "Error: CSV file not found."
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# 📦 Shipping CSV Generator")
gr.Markdown("Click the button below to generate and download a CSV file.")
btn = gr.Button("Generate CSV")
output = gr.File(label="Download your CSV", type="filepath") # FIXED: Changed 'file' to 'filepath'
btn.click(fn=download_csv, outputs=output)
if __name__ == "__main__":
demo.launch() |