MLdataset/Numeric_dataset
0
1import pandas as pd
2import numpy as np
3from sklearn.preprocessing import StandardScaler
4from sklearn.ensemble import RandomForestClassifier
5import gradio as gr
6
7# Load dataset
8df = pd.read_csv("tracks_features.csv")
9
10df = df.sample(50000, random_state=42)
11df = df.drop_duplicates().dropna()
12
13features = [
14 'danceability', 'energy', 'loudness', 'speechiness',
15 'acousticness', 'instrumentalness', 'liveness',
16 'valence', 'tempo', 'duration_ms'
17]
18
19X = df[features]
20y = df['explicit'].astype(int)
21
22# Scaling
23scaler = StandardScaler()
24X_scaled = scaler.fit_transform(X)
25
26# Model
27rf = RandomForestClassifier(
28 n_estimators=50,
29 max_depth=10,
30 n_jobs=-1
31)
32
33rf.fit(X_scaled, y)
34
35# Prediction function
36def predict_explicit(danceability, energy, loudness, speechiness,
37 acousticness, instrumentalness, liveness,
38 valence, tempo, duration_ms):
39
40 input_data = np.array([[danceability, energy, loudness, speechiness,
41 acousticness, instrumentalness, liveness,
42 valence, tempo, duration_ms]])
43
44 input_scaled = scaler.transform(input_data)
45 prediction = rf.predict(input_scaled)[0]
46
47 return "๐ Explicit Song" if prediction == 1 else "๐ต Non-Explicit Song"
48
49
50# Gradio UI
51interface = gr.Interface(
52 fn=predict_explicit,
53 inputs=[
54 gr.Slider(0, 1, label="Danceability"),
55 gr.Slider(0, 1, label="Energy"),
56 gr.Slider(-60, 0, label="Loudness"),
57 gr.Slider(0, 1, label="Speechiness"),
58 gr.Slider(0, 1, label="Acousticness"),
59 gr.Slider(0, 1, label="Instrumentalness"),
60 gr.Slider(0, 1, label="Liveness"),
61 gr.Slider(0, 1, label="Valence"),
62 gr.Slider(50, 250, label="Tempo"),
63 gr.Slider(50000, 300000, label="Duration (ms)")
64 ],
65 outputs="text",
66 title="๐ต Song Explicit Prediction App",
67 description="Predict whether a song is explicit or not"
68)
69
70interface.launch()