Spaces:
Sleeping
Sleeping
| import numpy as np | |
| import pandas as pd | |
| from sklearn.linear_model import LinearRegression | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.metrics import mean_squared_error | |
| import joblib | |
| # Mock dataset | |
| data = { | |
| 'age': [25, 32, 47, 51, 29, 45, 35, 33, 29, 24], | |
| 'education_level': [16, 18, 20, 21, 16, 18, 17, 16, 16, 15], | |
| 'experience': [1, 6, 20, 25, 3, 15, 8, 4, 2, 1], | |
| 'salary': [30000, 50000, 120000, 140000, 35000, 110000, 60000, 52000, 40000, 32000] | |
| } | |
| df = pd.DataFrame(data) | |
| # Split dataset | |
| X = df[['age', 'education_level', 'experience']] | |
| y = df['salary'] | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) | |
| # Train model | |
| model = LinearRegression() | |
| model.fit(X_train, y_train) | |
| # Evaluate model | |
| y_pred = model.predict(X_test) | |
| mse = mean_squared_error(y_test, y_pred) | |
| print(f"Model MSE: {mse}") | |
| # Save model | |
| joblib.dump(model, 'salary_model.joblib') | |
| import gradio as gr | |
| import joblib | |
| # Load the trained model | |
| model = joblib.load('salary_model.joblib') | |
| # Define prediction function | |
| def predict_salary(age, education_level, experience): | |
| input_data = [[age, education_level, experience]] | |
| prediction = model.predict(input_data) | |
| return f"Predicted Salary: ${prediction[0]:.2f}" | |
| # Create Gradio interface | |
| demo = gr.Interface( | |
| fn=predict_salary, | |
| inputs=[ | |
| gr.Number(label="Age"), | |
| gr.Number(label="Education Level (years)"), | |
| gr.Number(label="Experience (years)") | |
| ], | |
| outputs="text", | |
| title="Salary Prediction Model", | |
| description="Predict salary based on age, education level, and years of experience." | |
| ) | |
| # Launch the Gradio app | |
| demo.launch() |