Spaces:
Sleeping
Sleeping
| from sklearn.model_selection import train_test_split | |
| from sklearn.linear_model import LinearRegression | |
| from sklearn.metrics import mean_squared_error, r2_score | |
| from sklearn.ensemble import RandomForestRegressor | |
| import pandas as pd | |
| from tqdm.auto import tqdm | |
| import streamlit as st | |
| from huggingface_hub import Repository, HfApi, HfFolder | |
| import os | |
| tqdm.pandas() | |
| api = HfApi() | |
| token = os.getenv("token") # Das Token wird aus den Hugging Face Secrets abgerufen | |
| tokenread = os.getenv("tokenread") | |
| # Überprüfen, ob das Token vorhanden ist | |
| if token is None: | |
| raise ValueError("Hugging Face API-Token ist nicht gesetzt.") | |
| # Klonen Sie das Repository (dies wird in Ihrem Space ausgeführt) | |
| repo = Repository(local_dir="SpotifyHitPrediction", clone_from="Add1E/SpotifyHitPrediction", use_auth_token=tokenread) | |
| def predict_popularity(features, trainset): | |
| predictions = [None] * 2 | |
| predictions[0], predictions[1] = rf_model.predict([features]), model.predict([features]) | |
| addToCsvAndTrain(trainset) | |
| return predictions | |
| def addToCsvAndTrain(trainset): | |
| trainset = [ | |
| [trainset[0],trainset[1],trainset[2],trainset[3],trainset[4],trainset[5],trainset[6],trainset[7], | |
| trainset[8],trainset[9],trainset[10],trainset[11],trainset[12],trainset[13] | |
| ] | |
| ] | |
| neues_df = pd.DataFrame(trainset, columns= data.columns) | |
| df = pd.concat([data, neues_df], ignore_index=True) | |
| df.to_csv('top50.csv', index=False) | |
| repo.git_add('top50.csv') | |
| repo.git_commit("Add top50.csv") | |
| repo.git_push() | |
| data = pd.read_csv('top50.csv', encoding='ISO-8859-1') | |
| print(data.head()) | |
| # Let's also describe the data to get a sense of the distributions | |
| print(data.describe()) | |
| # Selecting the features and the target variable | |
| X = data.drop(['Unnamed: 0', 'Track.Name', 'Artist.Name', 'Genre', 'Popularity'], axis=1) | |
| y = data['Popularity'] | |
| # Splitting the data into training and testing sets | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Initializing the Linear Regression model | |
| model = LinearRegression() | |
| # Fitting the model | |
| model.fit(X_train, y_train) | |
| # Making predictions | |
| y_pred = model.predict(X_test) | |
| # Calculating the performance metrics | |
| mse = mean_squared_error(y_test, y_pred) | |
| r2 = r2_score(y_test, y_pred) | |
| # Initialize the Random Forest Regressor | |
| rf_model = RandomForestRegressor(n_estimators=100, random_state=42) | |
| # Fitting the model | |
| rf_model.fit(X_train, y_train) | |
| # Making predictions | |
| rf_pred = rf_model.predict(X_test) | |
| # Calculating the performance metrics | |
| rf_mse = mean_squared_error(y_test, rf_pred) | |
| rf_r2 = r2_score(y_test, rf_pred) | |
| # Feature importances | |
| feature_importances = rf_model.feature_importances_ | |
| # Create a pandas series with feature importances | |
| importances = pd.Series(feature_importances, index=X.columns) | |
| # Sort the feature importances in descending order | |
| sorted_importances = importances.sort_values(ascending=False) | |