timfocus's picture
Update app.py
8ebd0d2 verified
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()