Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.metrics import r2_score
|
| 5 |
+
from xgboost import XGBRegressor
|
| 6 |
+
|
| 7 |
+
# Load dataset
|
| 8 |
+
data = pd.read_csv('uber.csv')
|
| 9 |
+
data = data.drop('cars_available', axis=1)
|
| 10 |
+
|
| 11 |
+
# Features and target
|
| 12 |
+
X = data.drop('price_usd', axis=1)
|
| 13 |
+
Y = data['price_usd']
|
| 14 |
+
|
| 15 |
+
# Train-test split
|
| 16 |
+
Xtrain, Xtest, Ytrain, Ytest = train_test_split(X, Y, test_size=0.2)
|
| 17 |
+
|
| 18 |
+
# Train model
|
| 19 |
+
xgb = XGBRegressor()
|
| 20 |
+
xgb.fit(Xtrain, Ytrain)
|
| 21 |
+
|
| 22 |
+
# Evaluate model (just printing in logs, not in interface)
|
| 23 |
+
output = xgb.predict(Xtest)
|
| 24 |
+
score = r2_score(Ytest, output)
|
| 25 |
+
print("R² Score:", score)
|
| 26 |
+
|
| 27 |
+
# Gradio prediction function
|
| 28 |
+
def predict_price(rain, distance, time, traffic):
|
| 29 |
+
user_data = [[rain, distance, time, traffic]]
|
| 30 |
+
predicted_price = xgb.predict(user_data)[0]
|
| 31 |
+
return f"Predicted Uber Price: ${predicted_price:,.2f}"
|
| 32 |
+
|
| 33 |
+
# Gradio Interface
|
| 34 |
+
interface = gr.Interface(
|
| 35 |
+
fn=predict_price,
|
| 36 |
+
inputs=[
|
| 37 |
+
gr.Slider(1, 10, step=1, label="Rain (1-10)", value=5),
|
| 38 |
+
gr.Number(label="Distance (km)", value=10),
|
| 39 |
+
gr.Slider(1, 24, step=1, label="Time (Hour of Day)", value=12),
|
| 40 |
+
gr.Slider(1, 10, step=1, label="Traffic (1-10)", value=5),
|
| 41 |
+
],
|
| 42 |
+
outputs=gr.Textbox(label="Prediction"),
|
| 43 |
+
title="Uber Price Prediction",
|
| 44 |
+
description="Enter ride details to predict Uber price using XGBoost."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
# Launch app
|
| 48 |
+
if __name__ == "__main__":
|
| 49 |
+
interface.launch(share=True)
|