TamerTokgoz's picture
Update app.py
bc4fc84 verified
import gradio as gr
import pandas as pd
import numpy as np
import joblib
import random
# MODEL
model = joblib.load("DeliveryTime.pkl")
model_features = joblib.load("DTFeatures.pkl")
# ---------------------------
# RANDOM GENERATOR
# ---------------------------
def generate_driver():
return {
"age": random.randint(18, 50),
"rating": round(random.uniform(3.5, 5.0), 2),
"vehicle": random.randint(1, 5),
"deliveries": random.randint(0, 3)
}
# ---------------------------
# YEMEK → PREP TIME
# ---------------------------
def get_prep_time(food):
prep_map = {
"Pizza": random.randint(15, 25),
"Burger": random.randint(10, 20),
"Sushi": random.randint(20, 35),
"Kebab": random.randint(15, 30),
"Dessert": random.randint(5, 15)
}
return prep_map.get(food, 15)
# ---------------------------
# PREDICT
# ---------------------------
def predict(food, distance, traffic, weather, weekend):
driver = generate_driver()
prep_time = get_prep_time(food)
df = pd.DataFrame([{
"Delivery_person_Age": driver["age"],
"Delivery_person_Ratings": driver["rating"],
"distance": distance,
"Vehicle_condition": driver["vehicle"],
"multiple_deliveries": driver["deliveries"],
"Road_traffic_density": traffic,
"Weatherconditions": weather,
"prep_time": prep_time,
"is_weekend": weekend
}])
df = pd.get_dummies(df)
df = df.reindex(columns=model_features, fill_value=0)
pred = model.predict(df)[0]
return f"""
🚴 Courier Info:
- Age: {driver['age']}
- Rating: {driver['rating']}
- Vehicle Condition: {driver['vehicle']}
- Active Deliveries: {driver['deliveries']}
🍽️ Food: {food}
⏱️ Prep Time: {prep_time} min
🚚 Estimated Delivery Time:
👉 {round(pred,2)} minutes
"""
# ---------------------------
# UI
# ---------------------------
interface = gr.Interface(
fn=predict,
inputs=[
gr.Dropdown(["Pizza","Burger","Sushi","Kebab","Dessert"], label="Food Type"),
gr.Number(label="Distance"),
gr.Dropdown(["Low","Medium","High","Jam"], label="Traffic"),
gr.Dropdown(["Sunny","Stormy","Fog","Sandstorms"], label="Weather"),
gr.Radio([0,1], label="Weekend")
],
outputs="text",
title="🚚 Delivery Simulator Game",
description="Choose your order → system generates courier → predict delivery time"
)
interface.launch()