Lesson · Hinglish · ⏱ 7 min padhne mein

🚪 Model ko FastAPI se API ke roop mein serve karna

EnglishHinglish

🏠 Asli zindagi ki misaal

Restaurant ki kitchen (aapka model) ko seedha customers se order nahi lena chahiye. Waiter (API) menu se saaf order leta hai, kitchen tak pahunchaata hai, aur dish wapas laata hai. Doosri apps, jaise website ya mobile app, aapke model se sirf isi waiter ke through baat karti hain.

💡 Aasaan shabdon mein

Notebook mein pada model kisi ke kaam ka nahi. Use web API mein wrap karne se koi bhi app use use kar sakti hai. FastAPI popular Python framework hai: aap ek endpoint define karte ho (jaise POST /predict), input ko Pydantic model se describe karte ho (jo types automatically validate karta hai), model ko startup pe ek baar load karte ho, aur JSON response lautaate ho. FastAPI /docs pe interactive docs bhi bana deta hai. Health-check endpoint, input validation, timeouts aur logging jodo. Inhi se woh production-ready banta hai.

🎬 Dekho kaise kaam karta hai

📱 AppFastAPIPOST /predict🧠 model.pkl{area: 900, bhk: 2}{price_lakh: 70.2}validate input · load model once · log every request

Apps model ko seedha nahi chhooti. Woh API endpoint ko request bhejti hain, API model chalati hai, aur JSON response wapas aata hai.

🪜 Step by step

  1. App start hote hi model ek baar load karo, har request pe nahi.
  2. Input ko Pydantic model se define karo taaki FastAPI types aur required fields khud validate kare.
  3. POST /predict endpoint likho jo preprocess kare, predict kare aur prediction aur model version ke saath JSON lautaye.
  4. Load balancers ke liye GET /health jodo, aur har request ki latency aur errors log karo.
  5. Ise uvicorn se chalao, automatic /docs page se test karo, aur kuch automated tests likho.

🔑 Zaroori shabd

Endpoint
URL aur method jo ek kaam karta hai, jaise POST /predict.
Pydantic model
Class jo expected input describe karti hai, automatic validation ke saath.
Latency
Request ka response aane mein kitna time lagta hai.

👀 Example dekho

python
from fastapi import FastAPI
from pydantic import BaseModel
import joblib

app = FastAPI()
model = joblib.load("price_model.pkl")   # loaded once at startup

class Flat(BaseModel):
    area_sqft: float
    bhk: int

@app.post("/predict")
def predict(flat: Flat):
    price = model.predict([[flat.area_sqft, flat.bhk]])[0]
    return {"price_lakh": round(price, 1)}

uvicorn app:app se chalao. Ab koi bhi app flat POST karke price le sakti hai.

⚠️ Aam galtiyaan jinse bachna hai

  • Input validation nahi: ek missing field service crash kar deta hai ya bekaar predictions deta hai.
  • Request ke andar slow kaam, jaise files load karna ya bina timeout slow services call karna.

✅ Jaldi se check karo

Model ko predict function ke andar ki jagah startup pe load kyun karte hain?

  1. Disk kam lagti hai
  2. Har request pe load karne se har request slow ho jaayegi
  3. Python mein zaroori hai
Jawaab dekho

B. Har request pe load karne se har request slow ho jaayegi: Model load hone mein seconds lag sakte hain. Ek baar karo aur har request ke liye reuse karo.

🙋 Seekhne wale yeh poochhte hain

FastAPI kyun, Flask kyun nahi?

Dono chalte hain. FastAPI automatic validation, async support aur interactive docs deta hai, jo ML APIs ke liye accha hai.

Ek saath bahut requests kaise sambhalun?

Load balancer ke peeche kai worker processes ya replicas chalao, aur jahan model support kare wahan predictions batch karo.

📌 Yaad rakho

Model → API endpoint → koi bhi app use kare. Inputs validate karo aur sab log karo.

📖 Is lesson ke shabd (AI dictionary)

Poora course, aapke roadmap ke hisaab se

Progress tracking, quick checks aur step-by-step order ke saath. Yeh lesson in courses ka hissa hai:

ML Engineer · AI Product Engineer · MLOps / AI Platform Engineer · LLM / Agent Engineer · AI Forward Deployed Engineer

Course mein seekho