mingyk commited on
Commit
21b4ef9
1 Parent(s): 7b4c18a
Files changed (3) hide show
  1. README.md +78 -4
  2. model.py +15 -7
  3. request.py +14 -0
README.md CHANGED
@@ -1,17 +1,91 @@
1
  # Linear Regression Model
2
 
3
- This model provides two main functions:
4
- 1. **Retrieve Coefficients and Intercept**: Return the model's coefficients and intercept based on input column names.
5
- 2. **Predict Values**: Given column names and data values, return predictions.
 
 
 
 
 
6
 
7
  ## Usage
8
 
9
  ### 1. Retrieve Coefficients and Intercept
10
- Send a JSON payload with just column names:
11
 
 
 
 
12
  ```json
13
  {
14
  "inputs": {
15
  "columns": ["feature1", "feature2", "feature3"]
16
  }
17
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Linear Regression Model
2
 
3
+ This repository hosts a simple linear regression model. The model provides two primary functions:
4
+ 1. **Retrieve Coefficients and Intercept**: Given column names, the model will return its coefficients and intercept.
5
+ 2. **Predict Values from CSV**: Given column names and a CSV file with data, the model will use the data to predict values.
6
+
7
+ ## Model Files
8
+
9
+ - `linear_regression_model.joblib`: The trained linear regression model, saved with joblib.
10
+ - `model.py`: Contains the model class and custom Hugging Face pipeline for processing inputs and returning outputs.
11
 
12
  ## Usage
13
 
14
  ### 1. Retrieve Coefficients and Intercept
 
15
 
16
+ Send a JSON payload with just column names to retrieve the model’s coefficients and intercept.
17
+
18
+ **Input JSON Example**:
19
  ```json
20
  {
21
  "inputs": {
22
  "columns": ["feature1", "feature2", "feature3"]
23
  }
24
  }
25
+ ```
26
+
27
+ **Response JSON Example**:
28
+ ```json
29
+ {
30
+ "coefficients": {"feature1": 0.5, "feature2": -1.2, "feature3": 2.3},
31
+ "intercept": 0.1
32
+ }
33
+ ```
34
+
35
+ ### 2. Predict Values from CSV
36
+
37
+ Send a request with column names and a CSV file containing data for prediction. The model will use the data in the specified columns to make predictions.
38
+
39
+ - **Columns**: A JSON list of strings specifying the column names used in the model.
40
+ - **CSV File**: A file containing the data rows, uploaded as binary content.
41
+
42
+ **Python Example Using `requests`**:
43
+
44
+ ```python
45
+ import requests
46
+
47
+ url = "https://api-inference.huggingface.co/models/your-username/linear-regression-model"
48
+ headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"}
49
+
50
+ # Define the columns and open the CSV file in binary mode
51
+ columns = ["feature1", "feature2", "feature3"]
52
+ files = {
53
+ "inputs": ("data.csv", open("path/to/your/data.csv", "rb")),
54
+ "columns": (None, str(columns)) # Send columns as JSON string
55
+ }
56
+
57
+ response = requests.post(url, headers=headers, files=files)
58
+ print(response.json())
59
+ ```
60
+
61
+ **curl Example**:
62
+
63
+ ```bash
64
+ curl -X POST "https://api-inference.huggingface.co/models/your-username/linear-regression-model" \
65
+ -H "Authorization: Bearer YOUR_HUGGINGFACE_API_TOKEN" \
66
+ -F "columns=['feature1', 'feature2', 'feature3']" \
67
+ -F "inputs=@path/to/your/data.csv"
68
+ ```
69
+
70
+ **Response JSON Example**:
71
+ ```json
72
+ {
73
+ "predictions": [10.5, 15.8, 12.3]
74
+ }
75
+ ```
76
+
77
+ ### 3. File Structure
78
+
79
+ The main files in this repository:
80
+
81
+ - `linear_regression_model.joblib`: Contains the trained linear regression model.
82
+ - `model.py`: Model and pipeline definitions, handling CSV input and returning predictions or coefficients.
83
+ - `README.md`: This file, explaining model functionality and usage.
84
+
85
+ ### Notes
86
+
87
+ 1. Ensure your CSV file includes the specified columns for accurate predictions.
88
+ 2. The CSV file is temporarily saved and used for predictions. It will not be stored permanently.
89
+ 3. Your API token is required to authenticate requests.
90
+
91
+ This setup allows users to choose between retrieving coefficients or making predictions based on CSV data for more flexibility.
model.py CHANGED
@@ -1,5 +1,6 @@
1
  # model.py
2
  import joblib
 
3
  from typing import List, Dict, Union
4
  from transformers import Pipeline
5
 
@@ -14,8 +15,9 @@ class LinearRegressionModel:
14
  intercept = self.model.intercept_
15
  return {"coefficients": coefficients, "intercept": intercept}
16
 
17
- def predict(self, columns: List[str], data: List[List[float]]) -> List[float]:
18
- # Predict values for given input data
 
19
  return self.model.predict(data).tolist()
20
 
21
  # Instantiate the model
@@ -27,14 +29,20 @@ class CustomLinearRegressionPipeline(Pipeline):
27
  super().__init__()
28
  self.model = model
29
 
30
- def __call__(self, inputs: Dict[str, Union[List[str], List[List[float]]]]) -> Dict[str, Union[Dict[str, float], List[float]]]:
31
  columns = inputs.get("columns", [])
32
- data = inputs.get("data", [])
33
 
34
- if not data: # If no data, return coefficients and intercept
35
  return self.model.get_coefficients(columns)
36
- else: # If data is provided, return predictions
37
- return {"predictions": self.model.predict(columns, data)}
 
 
 
 
 
 
38
 
39
  # Instantiate the custom pipeline
40
  pipeline = CustomLinearRegressionPipeline(model)
 
1
  # model.py
2
  import joblib
3
+ import pandas as pd
4
  from typing import List, Dict, Union
5
  from transformers import Pipeline
6
 
 
15
  intercept = self.model.intercept_
16
  return {"coefficients": coefficients, "intercept": intercept}
17
 
18
+ def predict_from_csv(self, columns: List[str], csv_file_path: str) -> List[float]:
19
+ # Read the CSV file and select specified columns
20
+ data = pd.read_csv(csv_file_path)[columns]
21
  return self.model.predict(data).tolist()
22
 
23
  # Instantiate the model
 
29
  super().__init__()
30
  self.model = model
31
 
32
+ def __call__(self, inputs: Dict[str, Union[List[str], bytes]]) -> Dict[str, Union[Dict[str, float], List[float]]]:
33
  columns = inputs.get("columns", [])
34
+ csv_file = inputs.get("csv_file", None) # Expect a CSV file in binary format
35
 
36
+ if not csv_file: # If no CSV file, return coefficients and intercept
37
  return self.model.get_coefficients(columns)
38
+ else: # If CSV file is provided, save and process it for predictions
39
+ csv_file_path = "/tmp/input_data.csv" # Temporary path to save the CSV file
40
+ with open(csv_file_path, "wb") as f:
41
+ f.write(csv_file)
42
+
43
+ # Make predictions from CSV file
44
+ predictions = self.model.predict_from_csv(columns, csv_file_path)
45
+ return {"predictions": predictions}
46
 
47
  # Instantiate the custom pipeline
48
  pipeline = CustomLinearRegressionPipeline(model)
request.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+
3
+ url = "https://api-inference.huggingface.co/models/your-username/linear-regression-model"
4
+ headers = {"Authorization": "Bearer YOUR_HUGGINGFACE_API_TOKEN"}
5
+
6
+ # Define the columns and open the CSV file in binary mode
7
+ columns = ["feature1", "feature2", "feature3"]
8
+ files = {
9
+ "inputs": ("data.csv", open("path/to/your/data.csv", "rb")),
10
+ "columns": columns
11
+ }
12
+
13
+ response = requests.post(url, headers=headers, files=files)
14
+ print(response.json())