import pandas as pd import joblib import numpy as np import gradio as gr # Laden der zusätzlichen Daten aus der CSV-Datei df_bfs_data = pd.read_csv('bfs_municipality_and_tax_data_2021_cleaned.csv', sep=',', encoding='utf-8') df_bfs_data['MEAN_TAXABLE_INCOME'] = df_bfs_data['MEAN_TAXABLE_INCOME'].astype(str).str.replace("'", "").astype(float) # Laden der locations aus der locations.csv Datei locations_df = pd.read_csv('locations.csv', sep=',', encoding='utf-8') locations = dict(zip(locations_df['BFS_NAME'], locations_df['BFS_NUMMER'])) # Modell laden randomforest_model = joblib.load('randomforest_regression_super.pkl') another_model = joblib.load('randomforest_regression_bleifrei.pkl') # Define the core prediction function def predict_petrol_price(distance1, distance2, distance3, town): if town not in locations: return "Town not found in locations.", "Town not found in locations." bfs_number = locations[town] df = df_bfs_data[df_bfs_data['BFS_NUMMER'] == bfs_number] if len(df) != 1: # if there are more than two records with the same bfs_number return -1 return "Data inconsistency detected.", "Data inconsistency detected." input = pd.DataFrame({ 'POP': [df['POP'].iloc[0]], 'POP_DENS': [df['POP_DENS'].iloc[0]], 'FRG_PCT': [df['FRG_PCT'].iloc[0]], 'EMP': [df['EMP'].iloc[0]], 'MEAN_TAXABLE_INCOME': [df['MEAN_TAXABLE_INCOME'].iloc[0]], 'distance_m': [distance1], 'distance_2_m': [distance2], 'distance_3_m': [distance3] }) prediction1 = randomforest_model.predict(input) prediction2 = another_model.predict(input) rounded_prediction1 = round(float(prediction1[0]), 3) rounded_prediction2 = round(float(prediction2[0]), 3) return rounded_prediction1, rounded_prediction2 # Erstellen der Gradio-Oberfläche iface = gr.Interface( fn=predict_petrol_price, inputs=[gr.Number(label="Distance to highway entrance (m)"), gr.Number(label="Distance to a hotel (m)"), gr.Number(label="Distance to a supermarket (m)"), gr.Dropdown(choices=locations.keys(), label="Town")], outputs=[gr.Number(label="Random Forest Prediction - Super"), gr.Number(label="Random Forest Prediction - Bleifrei95")], examples=[[100, 200, 300, "Winterthur"], [150, 250, 350, "Kloten"], [5000, 5000, 2000, "Elsau"], [9400, 5500, 100, "Burgdorf"]] ) iface.launch()