Skip to content

Commit

Permalink
Updated app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaykold committed Aug 12, 2024
1 parent de55c78 commit 1a1d913
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 98 deletions.
66 changes: 44 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,48 +57,70 @@ To run this project, you need to install the following packages:
* pylint
* matplotlib

You can install the required packages using the provided `conda_dependencies.yml` file:
`conda env create -f conda_dependencies.yml`
## Setting up

### Clone the repository

```git clone https://github.com/Jaykold/dry-bean-predictor.git```

### Navigate to the project directory

```cd dry-bean-predictor```

### Create a virtual environment
You can install the required packages using the provided`conda_dependencies.yml` file:

```conda env create -f conda_dependencies.yml```

After creating your virtual environment, you can activate it using:

```conda activate myenv // That's is the name specified in the .yml file```

Or using `requirements.txt`:
`pip install -r requirements.txt`

Alternatively, you can use the `setup.py` file to install the project and its dependencies:
`pip install .`
```python -m venv myenv```

Usage
1. Clone the repository
`git clone https://github.com/Jaykold/dry-bean-predictor.git`
Activate the virtual environment
* On windows

2. Navigate to the project directory
`cd dry-bean-predictor`
```.\venv\Scripts\activate```
* On macOS/Linux

3. Run Jupyter Notebook
`jupyter notebook`
``` source venv/bin/activate```

4. Open `dry_bean.ipynb` and execute the cells to preprocess the data and train the model
Install the Required Packages

```pip install -r requirements.txt```

Alternatively, you can use ```pip install .``` to install the project and its dependencies as defined in the `setup.py` file.

Run Jupyter Notebook

```jupyter notebook```

Open `dry_bean.ipynb` and execute the cells to preprocess the data and train the model

## Components

### Data Ingestion
#### Data Ingestion
The `data_ingestion.py` script is responsible for loading the dataset and performing initial data checks.

### Data Preprocessing
#### Data Preprocessing
The `data_preprocess.py` script handles data cleaning, feature engineering, and splitting the data into training and testing sets.

### Model Training
#### Model Training
The `model_trainer.py` script trains a classification model using the preprocessed data and evaluates its performance.

### Pipeline
## Pipeline
The `pipeline` directory contains script for model prediction.
* `predict.py`

### Model training
#### Model training
To train the model, run the `model_trainer.py` script:
`python src/components/model_trainer.py`

This will train the model and save the results to the `artifacts` directory.
```python src/components/model_trainer.py```

### Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
This will train the model and save the results to the `artifacts` directory.

## Contributing
Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
35 changes: 17 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,28 @@ def predict_data():
return render_template('home.html')
else:
data=CustomData(
Area=request.form.get('area'),
Perimeter=request.form.get('perimeter'),
MajorAxisLength=request.form.get('majoraxislength'),
MinorAxisLength=request.form.get('minoraxislength'),
AspectRatio=request.form.get('aspectratio'),
Eccentricity=request.form.get('eccentricity'),
ConvexArea=request.form.get('convexarea'),
EquivDiameter=request.form.get('equivdiameter'),
Extent=request.form.get('extent'),
Solidity=request.form.get('solidity'),
Roundness=request.form.get('roundness'),
Compactness=request.form.get('compactness'),
ShapeFactor1=request.form.get('shapefactor1'),
ShapeFactor2=request.form.get('shapefactor2'),
ShapeFactor3=request.form.get('shapefactor3'),
ShapeFactor4=request.form.get('shapefactor4')
Area=request.form.get('Area'),
Perimeter=request.form.get('Perimeter'),
MajorAxisLength=request.form.get('MajorAxisLength'),
MinorAxisLength=request.form.get('MinorAxisLength'),
Aspectratio=request.form.get('Aspectratio'),
Eccentricity=request.form.get('Eccentricity'),
Convexarea=request.form.get('Convexarea'),
Equivdiameter=request.form.get('Equivdiameter'),
Extent=request.form.get('Extent'),
Solidity=request.form.get('Solidity'),
Roundness=request.form.get('Roundness'),
Compactness=request.form.get('Compactness'),
ShapeFactor1=request.form.get('ShapeFactor1'),
ShapeFactor2=request.form.get('ShapeFactor2'),
ShapeFactor3=request.form.get('ShapeFactor3'),
ShapeFactor4=request.form.get('ShapeFactor4')
)
pred_df=data.to_dataframe()
print(pred_df)

predict_pipeline=PredictPipeline()
y_pred, le=predict_pipeline.predict(pred_df)
results=le.inverse_transform(y_pred)
results=predict_pipeline.predict(pred_df)
return render_template('home.html', results=results[0])


Expand Down
81 changes: 42 additions & 39 deletions src/pipeline/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from src.exception import CustomException
from src.utils import load_object
from pydantic import BaseModel
from typing import Union


class PredictPipeline:
Expand All @@ -14,19 +15,21 @@ def predict(self, features:pd.DataFrame):
# Get artifacts path
model_path = 'artifacts/model.pkl'
scaler_path = 'artifacts/scaler.pkl'
labelencoder_path = 'artifiacts/labelencoder.pkl'
labelencoder_path = 'artifacts/labelencoder.pkl'

# Load the model, SMOTE, and scaler objects
model = load_object(model_path)
scaler = load_object(scaler_path)
labelencoder = load_object(labelencoder_path)
model = load_object(file_path=model_path)
scaler = load_object(file_path=scaler_path)
labelencoder = load_object(file_path=labelencoder_path)

# Transform the features using scaler
data_scaled = scaler.transform(features)

print(data_scaled)
# Make predictions using the model
preds = model.predict(data_scaled)
return preds, labelencoder
preds = model.predict(data_scaled).astype(int)
print(preds)
result = labelencoder.inverse_transform(preds)
return result

except Exception as e:
# Raise a custom exception if an error occurs
Expand All @@ -35,42 +38,42 @@ def predict(self, features:pd.DataFrame):


class CustomData(BaseModel):
area:float
perimeter:float
majoraxislength:float
minoraxislength: float
aspectratio: float
eccentricity: float
convexarea: float
equivdiameter: float
extent: float
solidity: float
roundness: float
compactness: float
shapefactor1: float
shapefactor2: float
shapefactor3: float
shapefactor4: float
Area:Union[float, int]
Perimeter:float
MajorAxisLength:float
MinorAxisLength: float
Aspectratio: float
Eccentricity: float
Convexarea: float
Equivdiameter: float
Extent: float
Solidity: float
Roundness: float
Compactness: float
ShapeFactor1: float
ShapeFactor2: float
ShapeFactor3: float
ShapeFactor4: float

def to_dataframe(self):
try:
data = {
'Area': [self.area],
'Perimeter': [self.perimeter],
'MajorAxisLength': [self.majoraxislength],
'MinorAxisLength': [self.minoraxislength],
'AspectRatio': [self.aspectratio],
'Eccentricity': [self.eccentricity],
'ConvexArea': [self.convexarea],
'EquivDiameter': [self.equivdiameter],
'Extent': [self.extent],
'Solidity': [self.solidity],
'Roundness': [self.roundness],
'Compactness': [self.compactness],
'ShapeFactor1': [self.shapefactor1],
'ShapeFactor2': [self.shapefactor2],
'ShapeFactor3': [self.shapefactor3],
'ShapeFactor4': [self.shapefactor4]
'Area': [self.Area],
'Perimeter': [self.Perimeter],
'MajorAxisLength': [self.MajorAxisLength],
'MinorAxisLength': [self.MinorAxisLength],
'AspectRatio': [self.Aspectratio],
'Eccentricity': [self.Eccentricity],
'ConvexArea': [self.Convexarea],
'EquivDiameter': [self.Equivdiameter],
'Extent': [self.Extent],
'Solidity': [self.Solidity],
'Roundness': [self.Roundness],
'Compactness': [self.Compactness],
'ShapeFactor1': [self.ShapeFactor1],
'ShapeFactor2': [self.ShapeFactor2],
'ShapeFactor3': [self.ShapeFactor3],
'ShapeFactor4': [self.ShapeFactor4]
}
return pd.DataFrame(data)

Expand Down
1 change: 0 additions & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def load_object(file_path:str):
with open(file_path, 'rb') as input:
obj=pickle.load(input)
logging.info(f"File sucessfully loaded from {file_path}")

return obj

except Exception as e:
Expand Down
36 changes: 18 additions & 18 deletions templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,94 +3,94 @@
<div class="login">
<h1>Dry Bean Classifier</h1>

<form action="{{ url_for('predict')}}" method="post">
<form action="{{ url_for('predict_data')}}" method="post">
<h2>
<legend>Dry Bean Prediction</legend>
</h2>
<div class="mb-3">
<label class="form-label">Area</label>
<input class="form-control" type="float" name="area"
<input class="form-control" type="float" name="Area"
placeholder="Enter the Area"/>
</div>
<div class="mb-3">
<label class="form-label">Perimeter</label>
<input class="form-control" type="float" name="perimeter"
<input class="form-control" type="float" name="Perimeter"
placeholder="Enter the Perimeter"/>
</div>
<div class="mb-3">
<label class="form-label">Major Axis Length</label>
<input class="form-control" type="float" name="majoraxislength"
<input class="form-control" type="float" name="MajorAxisLength"
placeholder="Enter the Major Axis Length"/>
</div>
<div class="mb-3">
<label class="form-label">Minor Axis Length</label>
<input class="form-control" type="float" name="minoraxislength"
<input class="form-control" type="float" name="MinorAxisLength"
placeholder="Enter the Minor Axis Length"/>
</div>
<div class="mb-3">
<label class="form-label">Aspect Ratio</label>
<input class="form-control" type="float" name="aspectratio"
<input class="form-control" type="float" name="Aspectratio"
placeholder="Enter the Aspect Ratio"/>
</div>
<div class="mb-3">
<label class="form-label">Eccentricity</label>
<input class="form-control" type="float" name="eccentricity"
<input class="form-control" type="float" name="Eccentricity"
placeholder="Enter the Eccentricity" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Convex Area</label>
<input class="form-control" type="float" name="convexarea"
<input class="form-control" type="float" name="Convexarea"
placeholder="Enter the Convex Area" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Equivalent Diameter</label>
<input class="form-control" type="float" name="equivdiameter"
<input class="form-control" type="float" name="Equivdiameter"
placeholder="Enter the Equivalent Diameter" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Extent</label>
<input class="form-control" type="float" name="extent"
<input class="form-control" type="float" name="Extent"
placeholder="Enter the Extent" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Solidity</label>
<input class="form-control" type="float" name="solidity"
<input class="form-control" type="float" name="Solidity"
placeholder="Enter the Solidity" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Roundness</label>
<input class="form-control" type="float" name="roundness"
<input class="form-control" type="float" name="Roundness"
placeholder="Enter the Roundness" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Compactness</label>
<input class="form-control" type="float" name="compactness"
<input class="form-control" type="float" name="Compactness"
placeholder="Enter the Compactness" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Shape Factor1</label>
<input class="form-control" type="float" name="shapefactor1"
<input class="form-control" type="float" name="ShapeFactor1"
placeholder="Enter the Shape Factor1" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Shape Factor2</label>
<input class="form-control" type="float" name="shapefactor2"
<input class="form-control" type="float" name="ShapeFactor2"
placeholder="Enter the Shape Factor2" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Shape Factor3</label>
<input class="form-control" type="float" name="shapefactor3"
<input class="form-control" type="float" name="ShapeFactor3"
placeholder="Enter the shape Factor3" min='0' max='100' />
</div>
<div class="mb-3">
<label class="form-label">Shape Factor4</label>
<input class="form-control" type="float" name="shapefactor4"
<input class="form-control" type="float" name="ShapeFactor4"
placeholder="Enter the shape Factor4" min='0' max='100' />
</div>
<div class="mb-3">
<input class="btn btn-primary" type="submit" value="Predict the Bean class" required />
</div>
</form>
</form>
<h2>
Your Dry bean is {{results}}
</h2>
Expand Down

0 comments on commit 1a1d913

Please sign in to comment.