ﻲﻟﻵﺍ ﻢﻠﻌﺘﻟﺍ ﺝﺫﺎﻤﻧ ﺮﺸﻨﻟ ﻲﻠﻤﻋ ﻞﻴﻟﺩ
.ﻚﻟ ﺐﺳﺎﻨﻣ ﻞﻴﻟﺪﻟﺍ ﺍﺬﻬﻓ ،ﻲﻟﻵﺍ ﻢﻠﻌﺘﻟﺍ ﺝﺫﺎﻤﻧ ﺮﺸﻧ ﻝﻮﺣ ﺪﻳﺰﻤﻟﺍ ﺔﻓﺮﻌﻣ ﻰﻟﺇ ﻊﻠﻄﺘﺗ ﺖﻨﻛ ﺍﺫ
.ﺔﺑﺎﺤﺴﻟﺍ ﻰﻠﻋ ﺮﺸﻨﻟﺍﻭ ،ﺔﻳﻭﺎﺣ ﻲﻓ ﺕﺎﻘﻴﺒﻄﺘﻟﺍ ﺔﺠﻣﺮﺑ ﺔﻬﺟﺍﻭ ﻊﺿﻭﻭ ،ﺝﺫﺎﻤﻨﻟﺍ ﺕﺍﺆ
:ﻲﻠﻳ ﺎﻣ ﻰﻠﻋ ﻞﻴﻟﺪﻟﺍ ﺍﺬﻫ ﺰﻛﺮﻳﻭ
- Scikit-Learn ﻡﺍﺪﺨﺘﺳﺎﺑ ﻲﻟﻵﺍ ﻢﻠﻌﺘﻠﻟ ﺝﺫﻮﻤﻧ ءﺎﻨﺑ
- FastAPI ﻡﺍﺪﺨﺘﺳﺎﺑ ﺝﺫﻮﻤﻨﻟﺍ ﻦﻣ ﺕﺍﺆﺒﻨﺘﻟﺍ ﺔﻣﺪﺨﻟ REST API ءﺎﺸﻧﺇ
- Docker ﻡﺍﺪﺨﺘﺳﺎﺑ (API) ﺕﺎﻘﻴﺒﻄﺘﻟﺍ ﺔﺠﻣﺮﺑ ﺔﻬﺟﺍﻭ ﺔﻳﻭﺎﺣ
.ﺓﺩﺪﺤﻤﻟﺍ ﻝﺎﺧﺩﻹﺍ ﺕﺍﺰﻴﻣ ﻰﻠﻋ ءًﺎﻨﺑ ﻝﺯﺎﻨﻤﻟﺍ ﺭﺎﻌﺳﻷ ﺕﺎﻌﻗﻮﺗ ﻡﺪﻘﻳ ﺔﻳﻭﺎﺣ ﻞﻜﺷ ﻰﻠﻋ ﻖﻴﺒﻄﺗ ﻚ
ﻉﻭﺮﺸﻤﻟﺍ ﺔﺌﻴﺑ ﺔﺌﻴﻬﺗ
:ﻲﻠﻳ ﺎﻣ ﺖﻴﺒﺜﺗ ﻦﻣ ﺪﻛﺄﺗ ،ءﺪﺒﻟﺍ ﻞﺒﻗ
- (ﺙﺪﺣﺃ ﺭﺍﺪﺻﺇ ﻭﺃ Python 3.11 ﺭﺍﺪﺻﺇ ﻞﻀﻔﻳ) Python ﻦﻣ ﺚﻳﺪﺣ ﺭﺍﺪﺻﺇ
- ﻚﺑ ﺹﺎﺨﻟﺍ ﻞﻴﻐﺸﺘﻟﺍ ﻡﺎﻈﻨﻟ Docker ﻰﻠﻋ ﻞﺼﺣﺍ ؛ﺕﺎﻳﻭﺎﺤﻠﻟ ءﺎﺳﺭﺇ ﻞﻣﺎﻋ
.ﺕﺎﻘﻴﺒﻄﺘﻟﺍ ﺔﺠﻣﺮﺑ ﺕﺎﻬﺟﺍﻭ ﻊﻣ ﻞﻤﻌﻟﺍﻭ ﻲﻟﻵﺍ ﻢﻠﻌﺘﻟﺍ ﺝﺫﺎﻤﻧ ءﺎﻨﺒﻟ ﻲﺳﺎﺳﺃ ﻢﻬﻓ ﻚﻳﺪﻟ ﻥﻮﻜﻳ
ءﺪﺑﺍ
:ﻉﻭﺮﺸﻤﻟﺍ ﻞﻴﻟﺪﻟ (ﺎﻬﺑ ﻰﺻﻮﻤﻟﺍ) ﺔﻴﻨﺒﻟﺍ ﻚﻴﻟﺇ
project-dir/
│
├── app/
│ ├── __init__.py # Empty file
│ └── main.py # FastAPI code for prediction API
│
├── model/
│ └── linear_regression_model.pkl # Saved trained model (after running model_training.py)
│
├── model_training.py # Script to train and save the model
├── requirements.txt # Dependencies for the project
└── Dockerfile # Docker configuration
.ﻚﻟﺫ ﺪﻌﺑ ﺎﻌًﻴﻤﺟ ﺎﻬﺘﻴﺒﺜﺘﺑ ﻢﻘﻨﻟ .ءﺪﺒﻠﻟ ﻥﻮﺜﻳﺎﺑ ﺕﺎﺒﺘﻜﻣ ﻦﻣ ﻞﻴﻠﻗ ﺩﺪﻋ ﻰﻟﺇ ﺝﺎﺘﺤﻨﺳ
:ﺔﻴﺿﺍﺮﺘﻓﺍ ﺔﺌﻴﺑ ﻂﻴﺸﻨﺗﻭ ءﺎﺸﻧﺈﺑ ﻢﻗ ،ﻚﻋﻭﺮﺸﻣ ﺔﺌﻴﺑ ﻲﻓ
$ python3 -m venv v1
$ source v1/bin/activate
.ﺝﺫﻮﻤﻨﻟﺍ ﺕﺍﺆﺒﻨﺗ ﺔﻣﺪﺨﻟ ﺕﺎﻘﻴﺒﻄﺘﻟﺍ ﺔﺠﻣﺮﺑ ﺔﻬﺟﺍﻭ ءﺎﻨﺒﻟ Uvicornﻭ FastAPIﻭ .ﻲﻟﻵﺍ ﻢﻠﻌﺘ
:ﺔﻄﻘﻨﻟﺍ ﻡﺍﺪﺨﺘﺳﺎﺑ ﺔﺑﻮﻠﻄﻤﻟﺍ ﻡﺰﺤﻟﺍ ﻩﺬﻫ ﺖﻴﺒﺜﺘﺑ ﻡﻮﻘﻧ ﺎﻧﻮﻋﺩ ﻚﻟﺬﻟ
$ pip3 install pandas scikit-learn fastapi uvicorn
.GitHub ﻰﻠﻋ ﻲﻤﻴﻠﻌﺘﻟﺍ ﺞﻣﺎﻧﺮﺒﻟﺍ ﺍﺬﻬﻟ ﺔﻴﺠﻣﺮﺒﻟﺍ ﺕﺎﻤﻴﻠﻌﺘﻟﺍ ﺔﻓﺎﻛ ﻰﻠﻋ ﺭﻮﺜﻌﻟﺍ ﻚﻨﻜﻤﻳ
ﻲﻟﻵﺍ ﻢﻠﻌﺘﻟﺍ ﺝﺫﻮﻤﻧ ءﺎﻨﺑ
:model_training.py ﻰﻤﺴﻳ ﻒﻠﻣ ءﺎﺸﻧﺈﺑ ﻢﻗ ،ﻉﻭﺮﺸﻤﻟﺍ ﻞﻴﻟﺩ ﻲﻓ .ﺓﺩﺪﺤﻤﻟﺍ ﺕﺍﺰﻴﻤﻟﺍ
# model_training.py
import pandas as pd
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
import pickle
import os
# Load the dataset
data = fetch_california_housing(as_frame=True)
df = data['data']
target = data['target']
# Select a few features
selected_features = ['MedInc', 'AveRooms', 'AveOccup']
X = df[selected_features]
y = target
# Train-test split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Train the Linear Regression model
model = LinearRegression()
model.fit(X_train, y_train)
# Create a 'model' folder to save the trained model
os.makedirs('model', exist_ok=True)
# Save the trained model using pickle
with open('model/linear_regression_model.pkl', 'wb') as f:
pickle.dump(model, f)
print("Model trained and saved successfully.")
.< b>linear_regression_model.pkl ﻢﺳﺎﺑ model/ ﺪﻠﺠﻤﻟﺍ ﻲﻓ ﻪﻈﻔﺤﻳﻭ ،ﻲﻄﺨﻟ
ﺔﻈﺣﻼﻣ: ﺪﻳﺰﻤﻟﺍ ﺔﻓﺎﺿﺇ ﺔﻟﻭﺎﺤﻣ ﻚﻨﻜﻤﻳ ﻦﻜﻟﻭ .ﺕﺍﺰﻴﻤﻟﺍ ﻦﻣ ﺓﺮﻴﻐﺻ ﺔﻴﻋﺮﻓ ﺔﻋﻮﻤﺠﻣ ﻂﻘ
:ﻪﻈﻔﺣﻭ ﺝﺫﻮﻤﻨﻟﺍ ﺐﻳﺭﺪﺘﻟ ﻲﺼﻨﻟﺍ ﺞﻣﺎﻧﺮﺒﻟﺍ ﻞﻴﻐﺸﺘﺑ ﻢﻗ
$ python3 model_training.py
:model/ ﻞﻴﻟﺪﻟﺍ ﻲﻓ pkl. ﻒﻠﻣ ﻰﻠﻋ ﺭﻮﺜﻌﻟﺍ ﻦﻣ ﻦﻜﻤﺘﺗ ﻥﺃ ﺽﺮﺘﻔﻤﻟﺍ ﻦﻣﻭ ﺔﻴﻟﺎﺘﻟﺍ ﺔ
Model trained and saved successfully.
FastAPI ﻖﻴﺒﻄﺗ ءﺎﺸﻧﺇ
.FastAPI ﻡﺍﺪﺨﺘﺳﺎﺑ ﺕﺍﺆﺒﻨﺘﻟﺍ ﻡﺪﺨﺗ ﻲﺘﻟﺍ (API) ﺕﺎﻘﻴﺒﻄﺘﻟﺍ ﺔﺠﻣﺮﺑ ﺔﻬﺟﺍﻭ ءﺎﺸﻧﺈﺑ ﻥﻵﺍ ﻡﻮ
.ﻚﻟﺫ ﺪﻌﺑ Docker ﻡﺍﺪﺨﺘﺳﺎﺑ ﺔﻳﻭﺎﺣ ﻲﻓ FastAPI ﻖﻴﺒﻄﺗ ﻊﺿﻭ ﻲﻓ ﺐﻏﺮﻧ ﺎﻨﻧﻷ ﻚﻟﺬﺑ ﻡﻮﻘﻧ .
:ﻲﻟﺎﺘﻟﺍ ﺩﻮﻜﻟﺍ ﺐﺘﻛﺍ ،main.py ﻲﻓ
# app/main.py
from fastapi import FastAPI
from pydantic import BaseModel
import pickle
import os
# Define the input data schema using Pydantic
class InputData(BaseModel):
MedInc: float
AveRooms: float
AveOccup: float
# Initialize FastAPI app
app = FastAPI(title="House Price Prediction API")
# Load the model during startup
model_path = os.path.join("model", "linear_regression_model.pkl")
with open(model_path, 'rb') as f:
model = pickle.load(f)
@app.post("/predict")
def predict(data: InputData):
# Prepare the data for prediction
input_features = [[data.MedInc, data.AveRooms, data.AveOccup]]
# Make prediction using the loaded model
prediction = model.predict(input_features)
# Return the prediction result
return {"predicted_house_price": prediction[0]}
.ﻊﻗﻮﺘﻤﻟﺍ ﺮﻌﺴﻟﺍ ﻉﺎﺟﺭﺇﻭ ،ﻝﺯﺎﻨﻤﻟﺍ ﺭﺎﻌﺳﺄﺑ ﺆﺒﻨﺘﻠﻟ ﺏﺭﺪﻤﻟﺍ ﺝﺫﻮﻤﻨﻟﺍ ﻡﺪﺨﺘﺴﻳﻭ .(AveOccup
Docker ﻡﺍﺪﺨﺘﺳﺎﺑ ﺔﻳﻭﺎﺣ ﻲﻓ ﻖﻴﺒﻄﺘﻟﺍ ﺔﺌﺒﻌﺗ
.requirements.txt ﻒﻠﻣﻭ Dockerfile ﻒﻠﻣ ﺊﺸﻧﺃ ،ﻉﻭﺮﺸﻤﻠﻟ ﺭﺬﺠﻟﺍ ﻞﻴﻟﺪﻟﺍ
Docker ﻒﻠﻣ ءﺎﺸﻧﺇ
:Dockerfile ﻒﻠﻣ ءﺎﺸﻧﺈﺑ ﻢﻘﻨﻟ
# Use Python 3.11 as the base image
FROM python:3.11-slim
# Set the working directory inside the container
WORKDIR /code
# Copy the requirements file
COPY ./requirements.txt /code/requirements.txt
# Install the Python dependencies
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
# Copy the app folder into the container
COPY ./app /code/app
# Copy the model directory (with the saved model file) into the container
COPY ./model /code/model
# Expose port 80 for FastAPI
EXPOSE 80
# Command to run the FastAPI app with Uvicorn
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
.ﺖﻗﺆﻣ ﻦﻳﺰﺨﺗ ﻥﻭﺩ ﺔﻳﺭﻭﺮﻀﻟﺍ Python ﺕﺎﻴﻌﺒﺗ ﺖﻴﺒﺜﺗﻭ ،ﺔﻳﻭﺎﺤﻟﺍ ﻲﻓ requirements.txt<
.ﺓﺍﻮﺘﺤﻣ ﺔﺌﻴﺑ ﻲﻓ FastAPI ﻖﻴﺒﻄﺗ ﺮﺸﻨﻟ ﻻًﺎﻌﻓ ﺩﺍﺪﻋﻹﺍ ﺍﺬﻫ ﺪﻌﻳ .80 ﺬﻔﻨﻤﻟﺍ ﺮﺒﻋ ﻝﻮﺻﻮﻠﻟ ﺔ
txt.ﺕﺎﺒﻠﻄﺘﻤﻟﺍ ﻒﻠﻣ ءﺎﺸﻧﺇ
:ﺕﺎﻴﻌﺒﺘﻟﺍ ﺔﻓﺎﻛ ﺩﺮﺴﻳ requirements.txt ﻒﻠﻣ ءﺎﺸﻧﺈﺑ ﻢﻗ
fastapi
uvicorn
scikit-learn
pandas
ءﺎﻨﻴﻤﻟﺍ ﻞﻣﺎﻋ ﺓﺭﻮﺻ ءﺎﻨﺑ
.ﺔﻳﻭﺎﺤﻟﺍ ﻞﻴﻐﺸﺗﻭ Docker ﺓﺭﻮﺻ ءﺎﺸﻧﺈﺑ ﻢﻘﻨﻠﻓ ،ﺍﺰًﻫﺎﺟ FastAPI ﻖﻴﺒﻄﺗﻭ requirements.tx
:ﻲﻟﺎﺘﻟﺍ docker build ﺮﻣﺃ ﻞﻴﻐﺸﺗ ﻖﻳﺮﻃ ﻦﻋ Docker ﺓﺭﻮﺻ ﺊﺸﻧﺃ
$ docker build -t house-price-prediction-api .
:Docker ﺔﻳﻭﺎﺣ ﻞﻴﻐﺸﺘﺑ ﻚﻟﺫ ﺪﻌﺑ ﻢﻗ
$ docker run -d -p 80:80 house-price-prediction-api
.http://127.0.0.1:80 ﻰﻠﻋ ﺎﻬﻴﻟﺇ ﻝﻮﺻﻮﻟﺍ ﻦﻜﻤﻳﻭ ﻞﻴﻐﺸﺘﻟﺍ ﺪﻴﻗ ﻚﺑ ﺔﺻﺎﺨﻟﺍ ﺕﺎﻘﻴﺒﻄﺘﻟﺍ ﺔﺠ
:ﺐﻠﻄﻠﻟ ﻝﺎﺜﻣ ﻚﻴﻟﺇ .POST ﺐﻠﻃ ﻝﺎﺳﺭﺇ ﻖﻳﺮﻃ ﻦﻋ /predict ﺔﻳﺎﻬﻨﻟﺍ ﺔﻄﻘﻧ ﺭﺎﺒﺘﺧﻻ P
curl -X 'POST' \
'http://127.0.0.1:80/predict' \
-H 'Content-Type: application/json' \
-d '{
"MedInc": 3.5,
"AveRooms": 5.0,
"AveOccup": 2.0
}'
:ﺍﺬﻫ ﻞﺜﻣ ،ﻊﻗﻮﺘﻤﻟﺍ ﻝﺰﻨﻤﻟﺍ ﺮﻌﺴﺑ ﺔﺑﺎﺠﺘﺳﺍ ﻉﺎﺟﺭﺇ ﻰﻟﺇ ﻚﻟﺫ ﻱﺩﺆﻳ ﻥﺃ ﺽﺮﺘﻔﻤﻟﺍ ﻦﻣ
{
"predicted_house_price": 2.3248705765077062
}
Docker Hub ﻰﻟﺇ ﺎﻬﻌﻓﺩﻭ Docker ﺓﺭﻮﺻ ﻰﻠﻋ ﺕﺎﻣﻼﻋ ﻊﺿﻭ
.ﺔﻴﺑﺎﺤﺴﻟﺍ ﺔﻴﺳﺎﺳﻷﺍ ﺔﻤﻈﻧﻷﺍ ﻰﻠﻋ ﺮﺸﻨﻟﺍﻭ ﺔﻛﺭﺎﺸﻤﻟﺍ ﻞﻴﻬﺴﺘﻟ Docker Hub ﻰﻟﺇ ﻪﻌﻓﺩ ﻥﻵﺍ ﻚﻨ
:Docker Hub ﻰﻟﺇ ﻝﻮﺧﺪﻟﺍ ﻞﻴﺠﺴﺘﺑ ﻢﻗ ،ﻻًﻭﺃ
$ docker login
.ﺩﺎﻤﺘﻋﻻﺍ ﺕﺎﻧﺎﻴﺑ ﻝﺎﺧﺩﺇ ﻚﻨﻣ ﺐﻠﻄﻴُﺳ
:Docker ﺓﺭﻮﺻ ﻰﻠﻋ ﺔﻣﻼﻋ ﻊﺿ
$ docker tag house-price-prediction-api your_username/house-price-prediction-api:v1
.ﻚﺑ ﺹﺎﺨﻟﺍ Docker Hub ﻡﺪﺨﺘﺴﻣ ﻢﺳﺎﺑ your_username ﻝﺪﺒﺘﺳﺍ
ﺔﻈﺣﻼﻣ: ﻰﻟﺇ ﺔﺛﺪﺤﻤﻟﺍ ﺓﺭﻮﺼﻟﺍ ﻊﻓﺩﻭ ،ﺓﺪﻳﺪﺟ ﺔﻣﻼﻌﺑ ﺓﺭﻮﺼﻟﺍ ءﺎﻨﺑ ﺓﺩﺎﻋﺇ ﻚﻨﻜﻤﻳ ،ﺝﺫ
:Docker Hub ﻰﻟﺇ ﺓﺭﻮﺼﻟﺍ ﻊﻓﺩﺍ
$ docker push your_username/house-price-prediction-api:v1
:ﻲﻠﻳ ﺎﻤﻛ ﺎﻬﻠﻴﻐﺸﺗﻭ ﺓﺭﻮﺼﻟﺍ ﺐﺤﺳ ﻥﻵﺍ ﻦﻳﺮﺧﻵﺍ ﻦﻳﺭﻮﻄﻤﻠﻟ ﻦﻜﻤﻳ
$ docker pull your_username/house-price-prediction-api:v1
$ docker run -d -p 80:80 your_username/house-price-prediction-api:v1
.ﺔﻳﻭﺎﺤﻟﺍ ﻞﻴﻐﺸﺗﻭ ﺓﺭﻮﺼﻟﺍ ﺐﺤﺳ ﻚﺑ ﺹﺎﺨﻟﺍ Docker Hub ﻉﺩﻮﺘﺴﻣ ﻰﻟﺇ ﻝﻮﺻﻮﻟﺍ ﻖﺣ ﻪﻳﺪﻟ ﺺﺨﺷ ﻱ
ﺔﻴﻟﺎﺘﻟﺍ ﺕﺍﻮﻄﺨﻟﺍﻭ ﺔﻤﺗﺎﺨﻟﺍ
:ﻲﻤﻴﻠﻌﺘﻟﺍ ﺞﻣﺎﻧﺮﺒﻟﺍ ﺍﺬﻫ ﻲﻓ ﻩﺎﻨﻠﻌﻓ ﺎﻤﻟ ﺔﻌﻳﺮﺳ ﺔﻌﺟﺍﺮﻣ ﻲﻠﻳ ﺎﻤﻴﻓ
- scikit-learn ﻡﺍﺪﺨﺘﺳﺎﺑ ﻲﻟﻵﺍ ﻢﻠﻌﺘﻟﺍ ﺝﺫﻮﻤﻧ ﺐﻳﺭﺪﺗ
- ﺕﺎﻌﻗﻮﺘﻟﺍ ﺔﻣﺪﺨﻟ FastAPI ﻖﻴﺒﻄﺗ ﺊﺸﻧﺃ
- Docker ﻡﺍﺪﺨﺘﺳﺎﺑ ﺔﻳﻭﺎﺣ ﻲﻓ ﻖﻴﺒﻄﺘﻟﺍ ﻊﺿﻮﺑ ﻢﻗ
.ﺔﺑﺎﺤﺴﻟﺍ ﻰﻠﻋ ﺔﻳﻭﺎﺣ ﻰﻠﻋ ﻱﻮﺘﺤﻤﻟﺍ ﻖﻴﺒﻄﺘﻟﺍ ﺍﺬﻫ ﺮﺸﻧ ﻲﻫ ﺔﻴﻟﺎﺘﻟﺍ ﺔﻴﻘﻄﻨﻤﻟﺍ ﺓﻮﻄﺨﻟﺍ .ﻊﻳﺯ
.ﺔﺑﺎﺤﺴﻟﺍ ﻰﻠﻋ ﻲﻟﻵﺍ ﻢﻠﻌﺘﻟﺍ ﺝﺫﺎﻤﻧ ﺮﺸﻧ ﻝﻮﺣ ﻲﻤﻴﻠﻌﺗ ﺞﻣﺎﻧﺮﺑ ﻰﻠﻋ ﻝﻮﺼﺤﻟﺍ ﻲﻓ ﺐﻏﺮﺗ ﺖﻨﻛ ﺍﺫ
!ﺪﻴﻌﺳ ﺮﺸﻧ
ﺓءﺍﺮﻘﻟﺍ ﻦﻣ ﺪﻳﺰﻣﻭ ﻊﺟﺍﺮﻤﻟﺍ
- ﻖﺋﺎﻗﺩ ﻲﻓ Python ﻡﺍﺪﺨﺘﺳﺎﺑ ﺕﺎﻘﻴﺒﻄﺘﻟﺍ ﺔﺠﻣﺮﺑ ﺕﺎﻬﺟﺍﻭ ءﺎﺸﻧﺇ :FastAPI ﻲﻤﻴﻠﻌﺘﻟﺍ ﺞﻣﺎﻧﺮﺒ
- ﺔﻠﻬﺳ ﺕﺍﻮﻄﺧ 5 ﻲﻓ Docker ﻡﺍﺪﺨﺘﺳﺎﺑ Python ﺕﺎﻘﻴﺒﻄﺗ ﻊﻴﻤﺠﺘﺑ ﻢﻗ
- FastAPI ﺕﺎﻳﻭﺎﺤﻟﺍ ﻲﻓ