Serving Deep Learning Models via API / Web App

model को train और save कर लिया है — अब बारी है उसे दुनिया के सामने पेश करने की 🎯
यानि model को API या Web App के ज़रिए serve करना।


🧩 दो मुख्य तरीकें:

तरीकाविवरणउदाहरण
✅ API-BasedModel को backend में serve करेंFlask / FastAPI
✅ Web AppModel के ऊपर UI बनाएंStreamlit / Gradio

🔶 1. ✅ Flask API से Model Serve करना (PyTorch Example)

🛠️ Step-by-step Code

📌 app.py:

from flask import Flask, request, jsonify
import torch
import torch.nn as nn

# Your trained model class
class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.fc1 = nn.Linear(2, 4)
self.fc2 = nn.Linear(4, 1)

def forward(self, x):
x = torch.relu(self.fc1(x))
return torch.sigmoid(self.fc2(x))

# Load model
model = MyNet()
model.load_state_dict(torch.load("model_weights.pth"))
model.eval()

# Create Flask app
app = Flask(__name__)

@app.route('/predict', methods=['POST'])
def predict():
data = request.get_json()
input_tensor = torch.tensor(data["input"]).float()
with torch.no_grad():
output = model(input_tensor).item()
return jsonify({"prediction": round(output)})

if __name__ == "__main__":
app.run(debug=True)

📲 Test via curl or Postman:

curl -X POST http://127.0.0.1:5000/predict \
-H "Content-Type: application/json" \
-d '{"input": [1.0, 0.0]}'

🔶 2. ✅ Streamlit Web App (Quick & Visual)

📌 app.py

import streamlit as st
import torch
import torch.nn as nn

# Load model
class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.fc1 = nn.Linear(2, 4)
self.fc2 = nn.Linear(4, 1)

def forward(self, x):
x = torch.relu(self.fc1(x))
return torch.sigmoid(self.fc2(x))

model = MyNet()
model.load_state_dict(torch.load("model_weights.pth"))
model.eval()

# UI
st.title("XOR Prediction Model")
x1 = st.slider("x1", 0.0, 1.0, 0.0)
x2 = st.slider("x2", 0.0, 1.0, 0.0)

if st.button("Predict"):
input_tensor = torch.tensor([[x1, x2]])
with torch.no_grad():
output = model(input_tensor).item()
st.write("Prediction:", round(output))

🔼 Run the App:

streamlit run app.py

🔶 3. ✅ Gradio Web Interface

import gradio as gr
import torch
import torch.nn as nn

class MyNet(nn.Module):
def __init__(self):
super(MyNet, self).__init__()
self.fc1 = nn.Linear(2, 4)
self.fc2 = nn.Linear(4, 1)

def forward(self, x):
x = torch.relu(self.fc1(x))
return torch.sigmoid(self.fc2(x))

model = MyNet()
model.load_state_dict(torch.load("model_weights.pth"))
model.eval()

def predict(x1, x2):
input_tensor = torch.tensor([[x1, x2]])
with torch.no_grad():
output = model(input_tensor).item()
return round(output)

gr.Interface(fn=predict, inputs=["slider", "slider"], outputs="text").launch()

✅ Deployment Options:

PlatformToolSuitable For
Local MachineFlask / StreamlitTesting, demos
Heroku / RenderFlask + GunicornCloud API
Hugging FaceGradio SpacePublic demo
AWS/GCPDocker / APIProduction

📝 Practice Questions:

  1. Flask API बनाने के लिए कौन से steps follow होते हैं?
  2. Streamlit और Gradio में क्या अंतर है?
  3. Model को Web पर serve करने के 3 तरीके क्या हैं?
  4. TorchScript model को Flask में कैसे load करेंगे?
  5. curl से API call कैसे की जाती है?

🧠 Summary Table

MethodInterfaceSetup TimeUI
FlaskJSON APIMedium
StreamlitWeb UIVery Low
GradioWeb UIVery Low
FastAPIFast JSONMedium-High