
Modal Tutorial: Python Function को Cloud GPU Endpoint में बदलें
Also available in English: Read this article in English
आपके पास एक Python model script है जिसे GPU चाहिए, और GPU नहीं है। Modal उस function को cloud container में चलाता है, उस GPU के साथ जिसका नाम आप code में लिखते हैं, प्रति second bill करता है, और उसे web URL के रूप में खोल सकता है। Docker files या Kubernetes की ज़रूरत नहीं।
Setup
pip install modal
modal setup
modal setup आपको sign in कराता है (fail हो तो python -m modal setup आज़माएँ)। शुरू करने से पहले एक नियम: GPU इस्तेमाल करने के लिए आपके Modal account में valid payment method होना ज़रूरी है।
Endpoint लिखें
import modal
app = modal.App("gpu-check")
image = modal.Image.debian_slim().uv_pip_install("fastapi[standard]", "torch")
@app.function(gpu="T4", image=image, scaledown_window=120)
@modal.fastapi_endpoint(method="POST")
def gpu_info(item: dict):
import torch
return {"cuda": torch.cuda.is_available(),
"gpu": torch.cuda.get_device_name(0),
"you_sent": item}
image आपका container है, जो Python में define होता है। gpu="T4" card चुनता है; आप "L4", "A10", "L40S", "A100", "H100" और नए chips भी माँग सकते हैं, या "H100:2" से कई GPUs। Models चलाने (training नहीं) के लिए Modal L40S से शुरू करने की सलाह देता है, जिसमें 48 GB GPU memory है। @modal.fastapi_endpoint function को FastAPI में लपेटता है, इसलिए image में FastAPI होना चाहिए।
Run, serve, deploy
- modal run app.py एक बार के काम के लिए temporary app चलाता है और आपकी script ख़त्म होते ही रुक जाता है।
- modal serve app.py एक temporary public URL देता है जो file edit करने पर reload होता है। रोकने के लिए Ctrl-C दबाएँ।
- modal deploy app.py एक persistent URL बनाता है जो app रोकने तक चालू रहता है।
इसे curl -X POST -H 'Content-Type: application/json' -d '{"x": 1}' <your URL> से test करें।
खर्च और cold starts संभालें
जिस deployed function पर कोई request नहीं, उसके लिए कोई container नहीं चलता, इसलिए compute का charge नहीं लगता। पहली request container boot करती है, जिसमें कुछ seconds लग सकते हैं। खाली containers default रूप से 60 seconds तक warm रहते हैं; scaledown_window इसे 2 seconds से 20 मिनट तक कुछ भी कर सकता है। min_containers कुछ containers हमेशा चालू रखता है, जिससे cold start नहीं होता लेकिन पूरे समय का bill बनता है।
कीमतें प्रति second हैं: T4 $0.000164 (लगभग $0.59 प्रति घंटा) और H100 $0.001097 (लगभग $3.95 प्रति घंटा)। Free Starter plan में हर महीने $30 के credits मिलते हैं। बड़े model weights को हर cold start पर download करने के बजाय Modal Volume में रखें।
दूसरे विकल्पों की तुलना AI agent hosting options, side projects के लिए free hosting और vibe-coded app कैसे deploy करें में करें, या Modal टूल पेज पर जाएँ।
स्रोत
- Introduction, Modal docs
- GPU acceleration, Modal docs
- Web Functions, Modal docs
- Apps, Functions, and entrypoints और Cold start performance, Modal docs
- Pricing, Modal
- modal-client, GitHub और modal package, PyPI
FAQ
क्या Modal free है?
Starter plan की कोई monthly fee नहीं है और इसमें हर महीने $30 के compute credits मिलते हैं।
क्या मुझे Docker चाहिए?
नहीं। Container image आप Python में modal.Image से define करते हैं।
modal serve और modal deploy में क्या फ़र्क है?
Serve एक temporary, live-reload होने वाला URL देता है; deploy एक persistent URL देता है।
पहली request धीमी क्यों है?
वह container शुरू करती है। एक container warm रखने के लिए scaledown_window या min_containers इस्तेमाल करें।