import tkinter as tk
from tkinter import filedialog
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_squared_error, r2_score
import tqdm

# Create the main window
window = tk.Tk()
window.title("Machine Learning Interface")

# Create a label and entry field for the training data
train_data_label = tk.Label(window, text="Training Data:")
train_data_label.pack()
train_data_entry = tk.Entry(window)
train_data_entry.pack()

# Create a button to trigger the training process
def train_model():
    # Get the training data from the entry field
    training_data = train_data_entry.get()

    # Split the data into training and test sets
    X_train, X_test, y_train, y_test = train_test_split(training_data, training_data, test_size=0.2, random_state=42)

    # Scale the data
    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)

    # Create and train the model
    model = MLPRegressor(hidden_layer_sizes=(10,), activation='relu', solver='adam', alpha=0.01)
    model.fit(X_train_scaled, y_train, epochs=100, verbose=2)

    # Make predictions on the test set
    y_pred = model.predict(X_test_scaled)

    # Print the mean squared error and R^2 score
    print("Mean squared error:", mean_squared_error(y_test, y_pred))
    print("R^2 score:", r2_score(y_test, y_pred))

# Create the button to trigger the training process
train_model_button = tk.Button(window, text="Train Model", command=train_model)
train_model_button.pack()

# Create a button to save the model
save_model_button = tk.Button(window, text="Save Model", command=save_model)
save_model_button.pack()

# Start the main loop
window.mainloop()