लाल और काले graphics card का close-up, वैसा GPU जिसे Modal Python functions के लिए प्रति second किराए पर देता है
Photo by Rohan on Unsplash · source · credits

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 workflow: setup, modal run, modal serve, modal deploy
Modal docs (Introduction, Web Functions, Apps) से। यह illustration है, screenshot नहीं।

इसे curl -X POST -H 'Content-Type: application/json' -d '{"x": 1}' <your URL> से test करें।

खर्च और cold starts संभालें

Modal GPU की कीमतें प्रति second और प्रति घंटा
modal.com/pricing से। यह illustration है, screenshot नहीं।

जिस 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 टूल पेज पर जाएँ।

स्रोत

FAQ

Starter plan की कोई monthly fee नहीं है और इसमें हर महीने $30 के compute credits मिलते हैं।

क्या मुझे Docker चाहिए?

नहीं। Container image आप Python में modal.Image से define करते हैं।

Serve एक temporary, live-reload होने वाला URL देता है; deploy एक persistent URL देता है।

पहली request धीमी क्यों है?

वह container शुरू करती है। एक container warm रखने के लिए scaledown_window या min_containers इस्तेमाल करें।

About the author

Suraj Kumar builds and edits AIUseFlow, a directory of AI tools for vibe coders, agent builders and creators. Guides like this one walk through a practical workflow with cited sources — not a news wire rewrite. How we rank tools.

Get the weekly stack

Rank movers and new AI tools for builders, once a week. Free, no spam, unsubscribe anytime.