مین مواد پر جائیں

Chapter 35: FastAPI for ML Deployment

MODULE 6 | MLOps​

Chapter 35: FastAPI for ML Deployment​

Ab tak tumhara model Python file ya notebook ke andar rehta tha. Koi website, mobile app ya doosra program usay seedha nahi chala sakta. Is chapter me hum model ko ek API me lapetenge taake koi bhi use internet (ya apne computer) par bhej kar jawab le sake. Is kaam ke liye hum FastAPI use karenge.

35.1 API kya hoti hai?​

JSON ki misaal​
{"sepal_length": 5.1, "sepal_width": 3.5, "petal_length": 1.4, "petal_width": 0.2}
HTTP methodKaamML me misaal
GETMaloomatlena/health (server theek hai?), /symptoms (list lo)
POSTDatabhej karkaam karwana/predict (features bhejo, prediction lo)
Status codeMatlabKab aata hai
200OK, sab theekRequest kamyab rahi
400Bad RequestRequest me galti (hamari apni check se)
404Not FoundAisa URL maujood hi nahi
422Unprocessable EntityData ka format ya qeemat galat (Pydantic ki check)
500Server ErrorServer ke andar code ya model me masla

35.2 FastAPI install aur pehla endpoint​

FastAPI ek tez aur asaan Python framework hai jo API banane ke liye bana hai. Iske sath Uvicorn chalta hai, jo server hai (API ko chalane wala program). Chapter 34 me hum ne dono install kar liye the. Pehle sab se chhoti API likhte hain. Ye code hello.py me rakho:

hello.py​
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def home():
return {"message": "Salam! FastAPI chal rahi hai"}

Ab terminal me server chalao (hello file ka naam hai, app us ke andar wala object):

TERMINAL​

uvicorn hello:app --reload TERMINAL OUTPUT (folder ka naam aur process number tumhare yahan alag honge) INFO: Will watch for changes in these directories: ['/tmp/snip'] INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [377] using StatReload INFO: Started server process [380] INFO: Waiting for application startup. INFO: Application startup complete.

Ab browser me http://127.0.0.1:8000 kholo, ya doosre terminal me curl chalao:

TERMINAL​

curl http://127.0.0.1:8000/ OUTPUT

{"message":"Salam! FastAPI chal rahi hai"}
Code ka hissaMatlab
app = FastAPI()Hamari API kaasli object; saare endpoints isi se jurte hain
@app.get("/")Decorator: batata hai ke neeche wala functionGETrequest par,**/**address par chalega
return {...}Pythondictionarykhud ba khudJSONme badal jati hai
--reloadCode badlo to serverkhud dobarashuru ho (sirf development me)

35.3 Trained model ko /predict endpoint se serve karna​

Ab asli kaam: Chapter 34 me save kiya hua iris_model.joblib API me lagate hain. Do cheezein dhyan se dekho: model ek baar, shuru me load hota hai (har request par nahi), aur /predict POST hai kyunke hum data bhej rahe hain. File ka naam iris_api.py:

iris_api.py

import joblib
from fastapi import FastAPI
from pydantic import BaseModel, Field
app = FastAPI(title="Iris Predictor")
model = joblib.load("iris_model.joblib")
CLASS_NAMES = ["setosa", "versicolor", "virginica"]
class IrisInput(BaseModel):
sepal_length:
float = Field(gt=0, lt=15)
sepal_width:
float = Field(gt=0, lt=15)
petal_length:
float = Field(gt=0, lt=15)
petal_width:
float = Field(gt=0, lt=15)
@app.get("/health")
def health():
return {"status": "ok"}
@app.post("/predict")
def predict(data: IrisInput):
features = [[data.sepal_length, data.sepal_width,
data.petal_length, data.petal_width]]
probs = model.predict_proba(features)[0]
idx = int(probs.argmax())
return {"species": CLASS_NAMES[idx], "confidence": round(float(probs[idx]), 3)}

Server chalao (uvicorn iris_api:app --reload) aur do alag phoolon ke naap bhej kar dekho:

TERMINAL​
curl http://127.0.0.1:8000/health
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" \
-d '{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.2}'
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" \
-d '{"sepal_length":6.7,"sepal_width":3.0,"petal_length":5.2,"petal_width":2.3}'
OUTPUT​
{"status":"ok"}
{"species":"setosa","confidence":1.0}
{"species":"virginica","confidence":1.0}

35.4 Pydantic se Input Validation​

Agar koi -1 cm ya "abc" bhej de, to model ko kharab data milega aur API bina bataye ajeeb jawab dega. Pydantic is masle ko hal karta hai: tum IrisInput class me bata dete ho ke har field kaisa hona chahiye, aur FastAPI model tak pohanchne se pehle hi check kar leta hai.

Pydantic ka hissaKya karta hai
class IrisInput(BaseModel):Input ka**dhancha (schema)**batata hai
sepal_length: floatIs field ki qismfloathoni chahiye ("abc" nahi chalega)
Field(gt=0, lt=15)Qeemat0 se bariaur15 se chhotiho (gt = greater than, lt = less than)
Field ghayab hoKhud ba khud422error aur saaf wazahat

Ab galat input bhej kar dekho (pehle me negative qeemat, doosre me ek field ghayab):

TERMINAL​
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" \
-d '{"sepal_length":-1,"sepal_width":3.5,"petal_length":1.4,"petal_width":0.2}'
curl -X POST http://127.0.0.1:8000/predict -H "Content-Type: application/json" \
-d '{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4}'
OUTPUT​
{"detail":[{"type":"greater_than","loc":["body","sepal_length"],
"msg":"Input should be greater than 0","input":-1,"ctx":{"gt":0.0}}]}
{"detail":[{"type":"missing","loc":["body","petal_width"],"msg":"Field required",
"input":{"sepal_length":5.1,"sepal_width":3.5,"petal_length":1.4}}]}

(Asal jawab ek hi line me aata hai; yahan parhne ke liye do lines me toda hai.) Dono baar status code 422 aaya (server ke log me 422 Unprocessable Entity dikhta hai), aur loc batata hai kis field me galti hai. Model tak ye data pohancha hi nahi. Yehi validation ka faida hai: sasta, tez aur mehfooz .

35.5 Swagger Docs (/docs) se Testing​

FastAPI ki sab se pyari khasiyat: tumhari API ke liye khud ba khud ek testing page ban jata hai. Server chalte hue browser me kholo: http://127.0.0.1:8000/docs. Is page ko Swagger UI kehte hain.

  1. Browser me http://127.0.0.1:8000/docs kholo. Tumhe tumhare saare endpoints ki list nazar aayegi (/health, /predict).

  2. POST /predict par click karo, phir Try it out dabao.

  3. Request body wale box me apna JSON likho (ya diye gaye example me qeemat badlo).

  4. Execute dabao. Neeche Response body me jawab aur Code (200 ya 422) nazar aayega.

  5. Schema section me tumhara IrisInput bhi dikhta hai, matlab dusron ke liye documentation tayyar hai.

AddressKya hai
/docsSwagger UI: interactive testing page
/redocDoosra, parhne me asaan documentation page
/openapi.jsonAPI ka poora naqsha machine ke parhne wali shakal me

35.6 Automatic Testing: TestClient aur pytest​

Har baar haath se /docs me click karna thaka dene wala hai, aur CI/CD (Chapter 38) me ye kaam khud hona chahiye. Is liye hum tests code me likhte hain . FastAPI ka TestClient server chalaye baghair API ko call karta hai. File ka naam test_iris_api.py:

test_iris_api.py

from fastapi.testclient import TestClient
from iris_api import app
client = TestClient(app)
GOOD = {"sepal_length": 5.1, "sepal_width": 3.5,
"petal_length": 1.4, "petal_width": 0.2}
def test_health():
res = client.get("/health")
assert res.status_code == 200
assert res.json() == {"status": "ok"}
def test_predict_ok():
res = client.post("/predict", json=GOOD)
assert res.status_code == 200
assert res.json()["species"] == "setosa"
def test_predict_negative_value():
res = client.post("/predict", json={**GOOD, "sepal_length": -1})
assert res.status_code == 422
def test_predict_missing_field():
bad = {k: v for k, v in GOOD.items() if k != "petal_width"}
res = client.post("/predict", json=bad)
assert res.status_code == 422
TERMINAL​
pytest -v
OUTPUT​
test_iris_api.py::test_health PASSED [ 25%]
test_iris_api.py::test_predict_ok PASSED [ 50%]
test_iris_api.py::test_predict_negative_value PASSED [ 75%]
test_iris_api.py::test_predict_missing_field PASSED [100%]
========================= 4 passed, 1 warning in 1.56s =========================

35.7 Production ke liye chhoti chhoti achhi aadatein​

AadatKyun
/health endpoint rakhoDocker, load balancer aur monitoring isi se pata karte hain ke API zinda hai
response_model / Pydantic sejawabka format
bhi tay karo
Jawab hamesha ek jaisa aur documented rahega
Modelek baarload karoHar request par load karne se API sust ho jati hai
Qeemton kihadd(gt, lt) lagaoBekaar input model tak na pohanche
Tests likhoHar badlav ke baad API ke toot jane ka darr khatam

35.8 Aam Galtiyan aur Hands-on​

AAM GALTIYAN​
  • uvicorn iris_api:app me file ka naam aur object ka naam galat likhna (iris_api file hai, app object).

  • • Server ko localhost par chala kar sochna ke doosre log bhi dekh lenge. Doosron tak pohanchane ke liye deployment chahiye (Docker + hosting).

  • Model ko predict() ke andar load karna (API bohat sust ho jati hai).

  • Validation na lagana aur model ko kuch bhi bhejne dena.

  • --reload ko production me chalana. Ye sirf development ke liye hai.

Hands-on​

  • Apne kisi purane project (maslan Titanic ya Iris) ka model joblib se save karo aur uski /predict API banao.

  • Field(gt=..., lt=...) ki hadd badlo aur dekho kaun si qeemat 422 deti hai.

  • /docs kholo aur Swagger se 3 alag predictions karo.

  • test_iris_api.py me ek aur test likho: /health ka jawab galat hone par test fail ho (jaan boojh kar jawab badal kar dekho).

KEY TAKEAWAYS​
  • API model ka waiter hai: request lekar model tak pohnchati aur JSON response wapas laati hai.

  • GET maloomat lene ke liye, POST data bhej kar prediction lene ke liye.

  • FastAPI me @app.get(...) / @app.post(...) decorator se endpoint banta hai, aur Uvicorn server chalata hai.

  • Model ek baar load karo, aur /predict se serve karo; Pydantic galat input ko 422 de kar rok deta hai.

  • /docs (Swagger) me testing asaan hai, aur TestClient + pytest testing ko automatic bana dete hain.