|
|
|
|
|
"""MMK-Informservice_project_DP-10.ipynb |
|
|
|
|
|
Automatically generated by Colab. |
|
|
|
|
|
Original file is located at |
|
|
https://colab.research.google.com/drive/1wmkVhN6rkUnZhbwxxXnVsRP7A-7PC_1p |
|
|
""" |
|
|
|
|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
import pickle |
|
|
|
|
|
|
|
|
chugun_df = pd.read_csv("chugun.csv") |
|
|
coke_df = pd.read_csv("coke.csv") |
|
|
|
|
|
|
|
|
with open("gb_model_chugun.pkl", "rb") as f: |
|
|
gb_model_chugun = pickle.load(f) |
|
|
with open("gb_model_coke.pkl", "rb") as f: |
|
|
gb_model_coke = pickle.load(f) |
|
|
|
|
|
import gradio as gr |
|
|
import pandas as pd |
|
|
import numpy as np |
|
|
import pickle |
|
|
|
|
|
|
|
|
chugun_df = pd.read_csv("chugun.csv") |
|
|
coke_df = pd.read_csv("coke.csv") |
|
|
|
|
|
|
|
|
with open("gb_model_chugun.pkl", "rb") as f: |
|
|
gb_model_chugun = pickle.load(f) |
|
|
with open("gb_model_coke.pkl", "rb") as f: |
|
|
gb_model_coke = pickle.load(f) |
|
|
|
|
|
|
|
|
def get_random_sample(dataset): |
|
|
return dataset.sample(1) |
|
|
|
|
|
|
|
|
def format_features(sample, target_column, model): |
|
|
|
|
|
features = sample.drop(columns=[target_column]) |
|
|
feature_names = list(features.columns) |
|
|
|
|
|
|
|
|
expected_features = model.feature_names_in_ if hasattr(model, 'feature_names_in_') else feature_names |
|
|
if set(feature_names) != set(expected_features): |
|
|
missing_features = set(expected_features) - set(feature_names) |
|
|
extra_features = set(feature_names) - set(expected_features) |
|
|
if missing_features: |
|
|
raise ValueError(f"Датасет не содержит ожидаемые признаки: {missing_features}") |
|
|
if extra_features: |
|
|
|
|
|
features = features[expected_features] |
|
|
feature_names = expected_features |
|
|
|
|
|
feature_values = features.values[0] |
|
|
return feature_names, feature_values |
|
|
|
|
|
|
|
|
def predict(model_choice): |
|
|
if model_choice == "Производство чугуна": |
|
|
dataset = chugun_df |
|
|
target_column = "Количество чугуна (т)" |
|
|
model = gb_model_chugun |
|
|
else: |
|
|
dataset = coke_df |
|
|
target_column = "Удельный расход кокса (кг/т)" |
|
|
model = gb_model_coke |
|
|
|
|
|
|
|
|
sample = get_random_sample(dataset) |
|
|
feature_names, feature_values = format_features(sample, target_column, model) |
|
|
actual_value = sample[target_column].values[0] |
|
|
|
|
|
|
|
|
input_data = np.array([feature_values]) |
|
|
|
|
|
|
|
|
prediction = model.predict(input_data)[0] |
|
|
|
|
|
|
|
|
error_percent = abs((prediction - actual_value) / actual_value) * 100 |
|
|
|
|
|
|
|
|
result = f"Предсказанное значение: {prediction:.2f}\n" |
|
|
result += f"Реальное значение: {actual_value:.2f}\n" |
|
|
result += f"Процент ошибки: {error_percent:.2f}%" |
|
|
|
|
|
return ( |
|
|
gr.Dataframe(headers=["Признак"], value=[[name] for name in feature_names]), |
|
|
gr.Dataframe(headers=["Значение"], value=[[value] for value in feature_values]), |
|
|
result |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
|
|
|
|
|
|
model_choice = gr.Dropdown( |
|
|
choices=["Производство чугуна", "Удельный расход кокса"], |
|
|
label="Выберите модель", |
|
|
value="Производство чугуна" |
|
|
) |
|
|
|
|
|
|
|
|
with gr.Row(): |
|
|
feature_display = gr.Dataframe(headers=["Признак"], value=[]) |
|
|
value_display = gr.Dataframe(headers=["Значение"], value=[]) |
|
|
|
|
|
|
|
|
predict_button = gr.Button("Предсказать") |
|
|
|
|
|
|
|
|
output = gr.Textbox(label="Результат предсказания") |
|
|
|
|
|
|
|
|
predict_button.click( |
|
|
fn=predict, |
|
|
inputs=model_choice, |
|
|
outputs=[feature_display, value_display, output] |
|
|
) |
|
|
|
|
|
|
|
|
demo.launch() |