Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +97 -0
- requirements.txt +4 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: 🌍
|
| 4 |
-
colorFrom: indigo
|
| 5 |
-
colorTo: red
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.37.2
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Diamond_Prices_Prediction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 4.37.1
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import seaborn as sns
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
| 5 |
+
from sklearn.compose import ColumnTransformer
|
| 6 |
+
from sklearn.pipeline import Pipeline
|
| 7 |
+
from sklearn.ensemble import RandomForestRegressor
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# Load the diamonds dataset
|
| 11 |
+
diamonds = sns.load_dataset("diamonds")
|
| 12 |
+
|
| 13 |
+
# Prepare the features and target
|
| 14 |
+
X = diamonds.drop("price", axis=1)
|
| 15 |
+
y = diamonds["price"]
|
| 16 |
+
|
| 17 |
+
# Split the data
|
| 18 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 19 |
+
X, y, test_size=0.2, random_state=42
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Define the preprocessing steps
|
| 23 |
+
numeric_features = ["carat", "depth", "table", "x", "y", "z"]
|
| 24 |
+
categorical_features = ["cut", "color", "clarity"]
|
| 25 |
+
|
| 26 |
+
preprocessor = ColumnTransformer(
|
| 27 |
+
transformers=[
|
| 28 |
+
("num", StandardScaler(), numeric_features),
|
| 29 |
+
("cat", OneHotEncoder(drop="first"), categorical_features),
|
| 30 |
+
]
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Create a pipeline with preprocessing and model
|
| 35 |
+
model = Pipeline(
|
| 36 |
+
[
|
| 37 |
+
("preprocessor", preprocessor),
|
| 38 |
+
("regressor", RandomForestRegressor(n_estimators=100, random_state=42)),
|
| 39 |
+
]
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Fit the model
|
| 43 |
+
model.fit(X_train, y_train)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Create the Gradio interface
|
| 47 |
+
def predict_price(carat, cut, color, clarity, depth, table, x, y, z):
|
| 48 |
+
input_data = pd.DataFrame(
|
| 49 |
+
{
|
| 50 |
+
"carat": [carat],
|
| 51 |
+
"cut": [cut],
|
| 52 |
+
"color": [color],
|
| 53 |
+
"clarity": [clarity],
|
| 54 |
+
"depth": [depth],
|
| 55 |
+
"table": [table],
|
| 56 |
+
"x": [x],
|
| 57 |
+
"y": [y],
|
| 58 |
+
"z": [z],
|
| 59 |
+
}
|
| 60 |
+
)
|
| 61 |
+
prediction = model.predict(input_data)[0]
|
| 62 |
+
return f"Predicted Price: ${prediction:.2f}"
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
fn=predict_price,
|
| 67 |
+
inputs=[
|
| 68 |
+
gr.Slider(
|
| 69 |
+
minimum=diamonds["carat"].min(),
|
| 70 |
+
maximum=diamonds["carat"].max(),
|
| 71 |
+
label="Carat",
|
| 72 |
+
),
|
| 73 |
+
gr.Dropdown(["Fair", "Good", "Very Good", "Premium", "Ideal"], label="Cut"),
|
| 74 |
+
gr.Dropdown(["D", "E", "F", "G", "H", "I", "J"], label="Color"),
|
| 75 |
+
gr.Dropdown(
|
| 76 |
+
["I1", "SI2", "SI1", "VS2", "VS1", "VVS2", "VVS1", "IF"], label="Clarity"
|
| 77 |
+
),
|
| 78 |
+
gr.Slider(
|
| 79 |
+
minimum=diamonds["depth"].min(),
|
| 80 |
+
maximum=diamonds["depth"].max(),
|
| 81 |
+
label="Depth",
|
| 82 |
+
),
|
| 83 |
+
gr.Slider(
|
| 84 |
+
minimum=diamonds["table"].min(),
|
| 85 |
+
maximum=diamonds["table"].max(),
|
| 86 |
+
label="Table",
|
| 87 |
+
),
|
| 88 |
+
gr.Slider(minimum=diamonds["x"].min(), maximum=diamonds["x"].max(), label="X"),
|
| 89 |
+
gr.Slider(minimum=diamonds["y"].min(), maximum=diamonds["y"].max(), label="Y"),
|
| 90 |
+
gr.Slider(minimum=diamonds["z"].min(), maximum=diamonds["z"].max(), label="Z"),
|
| 91 |
+
],
|
| 92 |
+
outputs="text",
|
| 93 |
+
title="Diamond Price Predictor",
|
| 94 |
+
description="Enter the characteristics of a diamond to predict its price.",
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
iface.launch(share=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
scikit-learn
|
| 2 |
+
pandas
|
| 3 |
+
numpy
|
| 4 |
+
seaborn
|