Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,107 +3,94 @@ import pandas as pd
|
|
| 3 |
import os
|
| 4 |
import sklearn
|
| 5 |
import pickle
|
|
|
|
| 6 |
|
| 7 |
print("=== ЗАПУСК ПРИЛОЖЕНИЯ ===")
|
| 8 |
print(f"Текущая директория: {os.getcwd()}")
|
| 9 |
print(f"Файлы в директории: {os.listdir('.')}")
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
def predict_car_price(vehicle_manufacturer, vehicle_category, current_mileage,
|
| 13 |
vehicle_year, vehicle_gearbox_type, doors_cnt, wheels,
|
| 14 |
vehicle_color, car_leather_interior):
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Создаем интерфейс
|
| 28 |
-
with gr.Blocks(title="Car Price Predictor", theme=gr.themes.Soft()) as demo:
|
| 29 |
-
gr.Markdown("# 🚗 Car Price Prediction Model")
|
| 30 |
-
gr.Markdown("Введите параметры автомобиля для предсказания цены")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
current_mileage = gr.Number(
|
| 48 |
-
label="Пробег (км)",
|
| 49 |
-
value=100000,
|
| 50 |
-
minimum=0
|
| 51 |
-
)
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
minimum=1990,
|
| 56 |
-
maximum=2024,
|
| 57 |
-
value=2015,
|
| 58 |
-
step=1
|
| 59 |
-
)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
choices=['Automatic', 'Manual', 'Tiptronic'],
|
| 64 |
-
label="Тип коробки передач",
|
| 65 |
-
value='Automatic'
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
doors_cnt = gr.Dropdown(
|
| 69 |
-
choices=['2/3', '4/5'],
|
| 70 |
-
label="Количество дверей",
|
| 71 |
-
value='4/5'
|
| 72 |
-
)
|
| 73 |
-
|
| 74 |
-
wheels = gr.Dropdown(
|
| 75 |
-
choices=['Left wheel', 'Right-hand drive'],
|
| 76 |
-
label="Расположение руля",
|
| 77 |
-
value='Left wheel'
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
vehicle_color = gr.Dropdown(
|
| 81 |
-
choices=['Silver', 'White', 'Grey', 'Black', 'Blue', 'Red'],
|
| 82 |
-
label="Цвет",
|
| 83 |
-
value='Black'
|
| 84 |
-
)
|
| 85 |
-
|
| 86 |
-
car_leather_interior = gr.Radio(
|
| 87 |
-
choices=[("Нет", 0), ("Да", 1)],
|
| 88 |
-
label="Кожаный салон",
|
| 89 |
-
value=1
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
predict_btn = gr.Button("Предсказать цену", variant="primary")
|
| 93 |
-
|
| 94 |
-
output = gr.Textbox(
|
| 95 |
-
label="Результат",
|
| 96 |
-
interactive=False,
|
| 97 |
-
lines=3
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
predict_btn.click(
|
| 101 |
-
fn=predict_car_price,
|
| 102 |
-
inputs=[vehicle_manufacturer, vehicle_category, current_mileage,
|
| 103 |
-
vehicle_year, vehicle_gearbox_type, doors_cnt, wheels,
|
| 104 |
-
vehicle_color, car_leather_interior],
|
| 105 |
-
outputs=output
|
| 106 |
-
)
|
| 107 |
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
| 3 |
import os
|
| 4 |
import sklearn
|
| 5 |
import pickle
|
| 6 |
+
import joblib
|
| 7 |
|
| 8 |
print("=== ЗАПУСК ПРИЛОЖЕНИЯ ===")
|
| 9 |
print(f"Текущая директория: {os.getcwd()}")
|
| 10 |
print(f"Файлы в директории: {os.listdir('.')}")
|
| 11 |
|
| 12 |
+
# Функция загрузки модели
|
| 13 |
+
def load_model():
|
| 14 |
+
try:
|
| 15 |
+
# Пробуем разные форматы и пути
|
| 16 |
+
model_files = [
|
| 17 |
+
'car_price_model.pkl',
|
| 18 |
+
'car_price_model.joblib',
|
| 19 |
+
'car_price_pipeline.pkl',
|
| 20 |
+
'./car_price_model.pkl'
|
| 21 |
+
]
|
| 22 |
+
|
| 23 |
+
for model_file in model_files:
|
| 24 |
+
if os.path.exists(model_file):
|
| 25 |
+
print(f"Найден файл модели: {model_file}")
|
| 26 |
+
try:
|
| 27 |
+
# Пробуем загрузить через joblib
|
| 28 |
+
model = joblib.load(model_file)
|
| 29 |
+
print(f"Модель загружена через joblib, тип: {type(model)}")
|
| 30 |
+
return model, None
|
| 31 |
+
except:
|
| 32 |
+
# Пробуем загрузить через pickle
|
| 33 |
+
try:
|
| 34 |
+
with open(model_file, 'rb') as f:
|
| 35 |
+
model = pickle.load(f)
|
| 36 |
+
print(f"Модель загружена через pickle, тип: {type(model)}")
|
| 37 |
+
return model, None
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Ошибка загрузки {model_file}: {e}")
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
return None, "Файл модели не найден"
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return None, f"Ошибка загрузки модели: {str(e)}"
|
| 46 |
+
|
| 47 |
+
# Загружаем модель при старте
|
| 48 |
+
model, error = load_model()
|
| 49 |
+
if error:
|
| 50 |
+
print(f"Ошибка загрузки модели: {error}")
|
| 51 |
+
demo_mode = True
|
| 52 |
+
else:
|
| 53 |
+
print("Модель успешно загружена!")
|
| 54 |
+
demo_mode = False
|
| 55 |
+
|
| 56 |
+
# Функция предсказания
|
| 57 |
def predict_car_price(vehicle_manufacturer, vehicle_category, current_mileage,
|
| 58 |
vehicle_year, vehicle_gearbox_type, doors_cnt, wheels,
|
| 59 |
vehicle_color, car_leather_interior):
|
| 60 |
|
| 61 |
+
if demo_mode:
|
| 62 |
+
# Демо-режим
|
| 63 |
+
base_price = 5000
|
| 64 |
+
year_bonus = (vehicle_year - 2000) * 200
|
| 65 |
+
mileage_penalty = current_mileage * 0.01
|
| 66 |
+
leather_bonus = 1000 if car_leather_interior == 1 else 0
|
| 67 |
+
|
| 68 |
+
estimated_price = base_price + year_bonus - mileage_penalty + leather_bonus
|
| 69 |
+
estimated_price = max(estimated_price, 500)
|
| 70 |
+
|
| 71 |
+
return f"Примерная цена: ${estimated_price:,.2f} (демо-режим)\n\nМодель не загружена: {error}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
else:
|
| 74 |
+
# Режим с ML моделью
|
| 75 |
+
try:
|
| 76 |
+
input_data = pd.DataFrame({
|
| 77 |
+
'vehicle_manufacturer': [vehicle_manufacturer],
|
| 78 |
+
'vehicle_category': [vehicle_category],
|
| 79 |
+
'current_mileage': [int(current_mileage)],
|
| 80 |
+
'vehicle_year': [int(vehicle_year)],
|
| 81 |
+
'vehicle_gearbox_type': [vehicle_gearbox_type],
|
| 82 |
+
'doors_cnt': [doors_cnt],
|
| 83 |
+
'wheels': [wheels],
|
| 84 |
+
'vehicle_color': [vehicle_color],
|
| 85 |
+
'car_leather_interior': [int(car_leather_interior)]
|
| 86 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
prediction = model.predict(input_data)[0]
|
| 89 |
+
return f"Предсказанная цена: ${prediction:,.2f}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
except Exception as e:
|
| 92 |
+
return f"Ошибка предсказания: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
+
# Интерфейс (остается таким же как в демо-версии)
|
| 95 |
+
with gr.Blocks(title="Car Price Predictor", theme=gr.themes.Soft()) as demo:
|
| 96 |
+
# ... тот же интерфейс что и в демо-версии ...
|