Update app.py
Browse files
app.py
CHANGED
@@ -1,112 +1,98 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
import
|
5 |
-
from sklearn.
|
6 |
-
import
|
7 |
-
from
|
8 |
-
import gradio as gr
|
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 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
return df_target.to_html()
|
100 |
-
|
101 |
-
# Crear la interfaz con Gradio
|
102 |
-
interface = gr.Interface(
|
103 |
-
fn=flujo, # Funci贸n principal
|
104 |
-
inputs=gr.File(label="Sube tu archivo CSV"), # Entrada de archivo
|
105 |
-
outputs="html", # Salida como tabla HTML
|
106 |
-
title="Prediccion geenracion de energia",
|
107 |
-
description="Sube un archivo CSV y perdice la geenracion de energia."
|
108 |
-
)
|
109 |
-
|
110 |
-
interface.launch(share = True)
|
111 |
-
|
112 |
-
|
|
|
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 |
+
# Create the Gradio interface
|
46 |
+
def predict_price(carat, cut, color, clarity, depth, table, x, y, z):
|
47 |
+
|
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 |
+
|
62 |
+
prediction = model.predict(input_data)[0]
|
63 |
+
return f"Predicted Price: ${prediction:.2f}"
|
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 |
+
iface.launch(share=True)
|
97 |
+
|
98 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|