avram48 commited on
Commit
d3dca5c
·
verified ·
1 Parent(s): e53e7c6

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ car_data = {
45
+ "Year": [year],
46
+ "Make": [make],
47
+ "Model": [model_name],
48
+ "FuelType": [fuel_type],
49
+ "Engine": [engine]
50
+ }
51
+
52
+ car_data_df = pd.DataFrame(car_data)
53
+
54
+ needs_df = pd.concat([X, car_data_df], ignore_index=True)
55
+
56
+ for col in needs_df.select_dtypes(include=['category', 'object']).columns:
57
+ needs_df[col] = le.fit_transform(needs_df[col])
58
+
59
+ for col in needs_df.select_dtypes(include=['number']).columns:
60
+ needs_df[col] = scaler.fit_transform(needs_df[col].values.reshape(-1, 1))
61
+
62
+ result_df = needs_df.loc[[0]]
63
+ prediction = model.predict(result_df)
64
+
65
+ inverted_prediction = scaler.inverse_transform(prediction.reshape(-1, 1))
66
+ predicted_price = inverted_prediction[0][0]
67
+
68
+ return f'Предсказанная цена: ${predicted_price:.2f}'
69
+
70
+ iface = gr.Interface(
71
+ fn=predict_price,
72
+ inputs=[
73
+ gr.inputs.Number(label="Год издания машины"),
74
+ gr.inputs.Textbox(label="Бренд машины"),
75
+ gr.inputs.Textbox(label="Модель машины"),
76
+ gr.inputs.Textbox(label="Тип топлива"),
77
+ gr.inputs.Textbox(label="Характеристики двигателя")
78
+ ],
79
+ outputs="text",
80
+ title="Предсказание цены автомобиля",
81
+ description="Введите характеристики автомобиля для предсказания его цены."
82
+ )
83
+
84
+ iface.launch(share=True)