Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import pandas as pd
|
3 |
+
import tensorflow as tf
|
4 |
+
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
|
5 |
+
from keras.models import Sequential
|
6 |
+
from keras.layers import Dense, Dropout
|
7 |
+
from sklearn.model_selection import train_test_split
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
data = pd.read_csv('cars_raw.csv')
|
11 |
+
|
12 |
+
le = LabelEncoder()
|
13 |
+
data['Make'] = le.fit_transform(data['Make'])
|
14 |
+
data['Model'] = le.fit_transform(data['Model'])
|
15 |
+
data = data[data['Price'] != 'Not Priced']
|
16 |
+
data["Price"] = data["Price"].str.replace("$", "").str.replace(",", "").astype(float)
|
17 |
+
|
18 |
+
scaler = MinMaxScaler()
|
19 |
+
data['Price'] = scaler.fit_transform(data['Price'].values.reshape(-1, 1))
|
20 |
+
data = data.dropna()
|
21 |
+
|
22 |
+
for col in data.select_dtypes(include=['category', 'object']).columns:
|
23 |
+
data[col] = le.fit_transform(data[col])
|
24 |
+
|
25 |
+
for col in data.select_dtypes(include=['number']).columns:
|
26 |
+
data[col] = scaler.fit_transform(data[col].values.reshape(-1, 1))
|
27 |
+
|
28 |
+
data = data.drop(columns=["Mileage", "SellerType", "VIN", "Stock#", "Drivetrain", "SellerName", "ConsumerReviews", "ExteriorStylingRating", "State", "Zipcode", "DealType"])
|
29 |
+
X = data.drop('Price', axis=1)
|
30 |
+
y = data['Price']
|
31 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
|
32 |
+
|
33 |
+
model = Sequential()
|
34 |
+
model.add(Dense(128, input_shape=(X_train.shape[1],)))
|
35 |
+
model.add(Dropout(0.3))
|
36 |
+
model.add(Dense(64))
|
37 |
+
model.add(Dropout(0.3))
|
38 |
+
model.add(Dense(1))
|
39 |
+
|
40 |
+
model.compile(optimizer='adam', loss='mean_squared_error')
|
41 |
+
model.fit(X_train, y_train, epochs=100, batch_size=32, validation_data=(X_test, y_test))
|
42 |
+
|
43 |
+
def predict_price(year, make, model_name, fuel_type, engine):
|
44 |
+
try:
|
45 |
+
car_data = {
|
46 |
+
"Year": [year],
|
47 |
+
"Make": [make],
|
48 |
+
"Model": [model_name],
|
49 |
+
"FuelType": [fuel_type],
|
50 |
+
"Engine": [engine]
|
51 |
+
}
|
52 |
+
|
53 |
+
car_data_df = pd.DataFrame(car_data)
|
54 |
+
|
55 |
+
needs_df = pd.concat([X, car_data_df], ignore_index=True)
|
56 |
+
|
57 |
+
for col in needs_df.select_dtypes(include=['category', 'object']).columns:
|
58 |
+
needs_df[col] = le.fit_transform(needs_df[col])
|
59 |
+
|
60 |
+
for col in needs_df.select_dtypes(include=['number']).columns:
|
61 |
+
needs_df[col] = scaler.fit_transform(needs_df[col].values.reshape(-1, 1))
|
62 |
+
|
63 |
+
result_df = needs_df.loc[[0]]
|
64 |
+
prediction = model.predict(result_df)
|
65 |
+
|
66 |
+
inverted_prediction = scaler.inverse_transform(prediction.reshape(-1, 1))
|
67 |
+
predicted_price = inverted_prediction[0][0]
|
68 |
+
|
69 |
+
return f'Предсказанная цена: ${predicted_price:.2f}'
|
70 |
+
except Exception as e:
|
71 |
+
return f'Ошибка: {str(e)}'
|
72 |
+
|
73 |
+
iface = gr.Interface(
|
74 |
+
fn=predict_price,
|
75 |
+
inputs=[
|
76 |
+
gr.inputs.Number(label="Год издания машины"),
|
77 |
+
gr.inputs.Textbox(label="Бренд машины"),
|
78 |
+
gr.inputs.Textbox(label="Модель машины"),
|
79 |
+
gr.inputs.Textbox(label="Тип топлива"),
|
80 |
+
gr.inputs.Textbox(label="Характеристики двигателя")
|
81 |
+
],
|
82 |
+
outputs="text",
|
83 |
+
title="Предсказание цены автомобиля",
|
84 |
+
description="Введите характеристики автомобиля для предсказания его цены."
|
85 |
+
)
|
86 |
+
|
87 |
+
iface.launch()
|