--- title: Mlflow emoji: 🐢 colorFrom: green colorTo: purple sdk: docker pinned: false --- ``` import mlflow import numpy as np from sklearn.linear_model import LinearRegression from sklearn.metrics import mean_squared_error from sklearn.datasets import make_regression from sklearn.model_selection import train_test_split # Set the tracking URI to point to your MLflow server mlflow.set_tracking_uri("https://subhrajit-mohanty-mlflow.hf.space") # Create an experiment or use an existing one experiment_name = "file-store-experiment" try: experiment_id = mlflow.create_experiment(experiment_name) except: experiment_id = mlflow.get_experiment_by_name(experiment_name).experiment_id # Set the experiment mlflow.set_experiment(experiment_name) # Create a simple dataset X, y = make_regression(n_samples=100, n_features=5, noise=0.1, random_state=42) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Start an MLflow run with mlflow.start_run(run_name="metrics_only_example"): # Log parameters mlflow.log_param("model_type", "LinearRegression") mlflow.log_param("n_features", X.shape[1]) mlflow.log_param("n_samples", X.shape[0]) # Train the model model = LinearRegression() model.fit(X_train, y_train) # Make predictions and evaluate y_pred = model.predict(X_test) mse = mean_squared_error(y_test, y_pred) # Log metrics only (avoiding artifact storage) mlflow.log_metric("mse", mse) mlflow.log_metric("r2_score", model.score(X_test, y_test)) # Log coefficients as parameters instead of storing the model for i, coef in enumerate(model.coef_): mlflow.log_param(f"coef_{i}", coef) mlflow.log_param("intercept", model.intercept_) print(f"Run completed with MSE: {mse:.4f}") print(f"Metrics and parameters have been logged to MLflow") print(f"Run ID: {mlflow.active_run().info.run_id}") ``` Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference