Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,30 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
import sklearn
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Загружаем сохраненную модель и feature names
|
| 8 |
-
# def load_model():
|
| 9 |
-
# try:
|
| 10 |
-
# with open('car_price_pipeline.pkl', 'rb') as f:
|
| 11 |
-
# pipeline = pickle.load(f)
|
| 12 |
-
# with open('feature_names.pkl', 'rb') as f:
|
| 13 |
-
# feature_names = pickle.load(f)
|
| 14 |
-
# return pipeline, feature_names
|
| 15 |
-
# except Exception as e:
|
| 16 |
-
# print(f"Error loading model: {e}")
|
| 17 |
-
# return None, None
|
| 18 |
-
|
| 19 |
-
# def load_model():
|
| 20 |
-
# try:
|
| 21 |
-
# pipeline = joblib.load('car_price_pipeline.pkl')
|
| 22 |
-
# with open('feature_names.pkl', 'rb') as f:
|
| 23 |
-
# feature_names = pickle.load(f)
|
| 24 |
-
# return pipeline, feature_names
|
| 25 |
-
# except Exception as e:
|
| 26 |
-
# print(f"Error loading model: {e}")
|
| 27 |
-
# return None, None
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
model_path = 'car_price_pipeline.pkl'
|
| 34 |
-
|
| 35 |
-
if not os.path.exists(model_path):
|
| 36 |
-
return None
|
| 37 |
-
|
| 38 |
-
# Загружаем через joblib (лучше для sklearn)
|
| 39 |
-
pipeline = joblib.load(model_path)
|
| 40 |
-
|
| 41 |
-
if not hasattr(pipeline, 'predict'):
|
| 42 |
-
return None
|
| 43 |
-
|
| 44 |
-
return pipeline
|
| 45 |
-
|
| 46 |
-
except Exception as e:
|
| 47 |
-
return None
|
| 48 |
|
| 49 |
-
#
|
| 50 |
def predict_car_price(vehicle_manufacturer, vehicle_category, current_mileage,
|
| 51 |
vehicle_year, vehicle_gearbox_type, doors_cnt, wheels,
|
| 52 |
vehicle_color, car_leather_interior):
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
'doors_cnt': [doors_cnt],
|
| 63 |
-
'wheels': [wheels],
|
| 64 |
-
'vehicle_color': [vehicle_color],
|
| 65 |
-
'car_leather_interior': [int(car_leather_interior)]
|
| 66 |
-
})
|
| 67 |
-
|
| 68 |
-
# Загружаем модель
|
| 69 |
-
pipeline = load_model()
|
| 70 |
-
|
| 71 |
-
if pipeline is None:
|
| 72 |
-
return "Ошибка: модель не загружена"
|
| 73 |
-
|
| 74 |
-
# Предсказание
|
| 75 |
-
prediction = pipeline.predict(input_data)[0]
|
| 76 |
-
return f"Предсказанная цена: ${prediction:,.2f}"
|
| 77 |
|
| 78 |
-
|
| 79 |
-
return f"Ошибка предсказания: {str(e)}"
|
| 80 |
|
| 81 |
-
# Создаем интерфейс
|
| 82 |
with gr.Blocks(title="Car Price Predictor", theme=gr.themes.Soft()) as demo:
|
| 83 |
gr.Markdown("# 🚗 Car Price Prediction Model")
|
| 84 |
gr.Markdown("Введите параметры автомобиля для предсказания цены")
|
|
@@ -147,7 +93,8 @@ with gr.Blocks(title="Car Price Predictor", theme=gr.themes.Soft()) as demo:
|
|
| 147 |
|
| 148 |
output = gr.Textbox(
|
| 149 |
label="Результат",
|
| 150 |
-
interactive=False
|
|
|
|
| 151 |
)
|
| 152 |
|
| 153 |
predict_btn.click(
|
|
@@ -157,11 +104,6 @@ with gr.Blocks(title="Car Price Predictor", theme=gr.themes.Soft()) as demo:
|
|
| 157 |
vehicle_color, car_leather_interior],
|
| 158 |
outputs=output
|
| 159 |
)
|
| 160 |
-
|
| 161 |
-
gr.Markdown("---")
|
| 162 |
-
gr.Markdown("### Примеры параметров:")
|
| 163 |
-
gr.Markdown("- **TOYOTA, Sedan, 100,000 km, 2015, Automatic** → ~$5,000")
|
| 164 |
-
gr.Markdown("- **BMW, Sedan, 50,000 km, 2018, Automatic** → ~$15,000")
|
| 165 |
|
| 166 |
if __name__ == "__main__":
|
| 167 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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 |
+
base_price = 5000
|
| 18 |
+
year_bonus = (vehicle_year - 2000) * 200
|
| 19 |
+
mileage_penalty = current_mileage * 0.01
|
| 20 |
+
leather_bonus = 1000 if car_leather_interior == 1 else 0
|
| 21 |
+
|
| 22 |
+
estimated_price = base_price + year_bonus - mileage_penalty + leather_bonus
|
| 23 |
+
estimated_price = max(estimated_price, 500) # Минимальная цена
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
return f"Примерная цена: ${estimated_price:,.2f} (демо-режим)\n\nФайлы в директории: {os.listdir('.')}"
|
|
|
|
| 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("Введите параметры автомобиля для предсказания цены")
|
|
|
|
| 93 |
|
| 94 |
output = gr.Textbox(
|
| 95 |
label="Результат",
|
| 96 |
+
interactive=False,
|
| 97 |
+
lines=3
|
| 98 |
)
|
| 99 |
|
| 100 |
predict_btn.click(
|
|
|
|
| 104 |
vehicle_color, car_leather_interior],
|
| 105 |
outputs=output
|
| 106 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
if __name__ == "__main__":
|
| 109 |
demo.launch()
|