Spaces:
No application file
No application file
Download app.py from hamzacodesnow989/linear_Regression: direct link, hf CLI and curl.
- Browser
- Download file 1.33 kB
-
https://huggingface.co/spaces/hamzacodesnow989/linear_Regression/resolve/main/app.py
- Command line
-
hf download hf://spaces/hamzacodesnow989/linear_Regression/app.py
-
curl -L -o app.py https://huggingface.co/spaces/hamzacodesnow989/linear_Regression/resolve/main/app.py
1.33 kB
| from flask import Flask, request, render_template, jsonify | |
| import pickle | |
| import pandas as pd | |
| from sklearn.linear_model import LinearRegression | |
| app = Flask(__name__) | |
| # Load your trained model | |
| with open("linear_regression_model.pkl", "rb") as f: | |
| model = pickle.load(f) | |
| # Home page | |
| def home(): | |
| if request.method == "POST": | |
| # Check if file is uploaded | |
| if "file" not in request.files: | |
| return "No file uploaded", 400 | |
| file = request.files["file"] | |
| if file.filename == "": | |
| return "No selected file", 400 | |
| # Read CSV into DataFrame | |
| df = pd.read_csv(file) | |
| # Validate columns | |
| if not all(col in df.columns for col in ["x1", "x2"]): | |
| return "CSV must have columns x1 and x2", 400 | |
| # Make predictions | |
| predictions = model.predict(df[["x1", "x2"]]) | |
| df["prediction"] = predictions | |
| # Return results as JSON | |
| return df.to_json(orient="records") | |
| # GET request returns simple upload form | |
| return ''' | |
| <h2>Upload CSV for Predictions</h2> | |
| <form method="POST" enctype="multipart/form-data"> | |
| <input type="file" name="file"> | |
| <input type="submit" value="Predict"> | |
| </form> | |
| ''' | |
| if __name__ == "__main__": | |
| app.run(debug=True) | |