Lesson 160 — Intermediate

Quick Demos with Gradio & Hugging Face Spaces

Is lesson ka mental model: Function → UI in one call → share link — aur ek line jo demo ka status batati hai, a demo is evidence, not architecture. Is lesson mein tum har Python function ko Gradio Interface se web UI mein badalna, Blocks se chat/streaming banana, Spaces pe push karna, Gradio vs Streamlit vs FastAPI wala decision table, aur demo hygiene seekhoge — demo kahan khatam hota hai, production kahan shuru.

⏱ 10 min• Intermediate• Visual AI Workflows with n8n-style Automation

Demo kyun — evidence banao, architecture nahi

Kya hai?

Gradio tumhare Python function ko web UI mein badal deta hai — inputs, buttons, output dikhana — ek call mein. Function signature hi UI ban jaata hai: do inputs do components, tuple output do components. Usi app ko HF Spaces pe push karke public link mil jaata hai.

Kab kaam aata hai?

Model ko karke dikhane ke liye — teammate, reviewer ya user feedback ke liye jaldi. Demo ka kaam hai convince karna ("dekh, kaam karta hai") aur feedback ikattha karna. Backend banana abhi nahi — wo alag decision hai.

Kahan milta hai?

gradio.app/docs ke Interface/Blocks API mein, Hugging Face Spaces (Gradio SDK) mein, aur model research pages ke peeche jo har model ke saath chhota playground dete hain. Production ka raasta L10 LLMOps mein hai — demo uska substitute nahi.

Demo aur product ka farq ek line mein: demo ka kaam hai sach dikhana, product ka kaam hai zimmedari lena. Demo mein kal link badal sakte ho; product mein on-call hota hai, auth hota hai, contract hota hai. Isliye demo ko banaw aur use karo — par uspe SLA mat chadhao.

Demo ka kaam convince karna hai, serve nahi: Gradio link team ko dikhao, feedback lo, scope tay karo. Jab traffic, auth aur SLA aayein — tab FastAPI wala backend banao (L10/L14 wala raasta). Demo link ko production endpoint mat samjho.

Gradio Interface — function se UI ek call mein

import gradio as gr

def spam_check(text):                       # TF-IDF wala chhota checker bhi chalega
    words = text.lower().split()
    hits  = sum(1 for w in words if w in {"free", "winner", "click"})
    label = "spam" if hits >= 2 else "ham"
    return label, round(hits / max(len(words), 1), 2)

demo = gr.Interface(
    fn       = spam_check,
    inputs   = gr.Textbox(lines=3, label="Message"),
    outputs  = [gr.Label(label="verdict"), gr.Number(label="signal ratio")],
    examples = [["free gift winner click here"], ["kal meeting 10 baje"]],
)
demo.launch(share=True)                     # temporary share link — demo ke liye
Function hi contract hai: Jo function exceptions fekta hai, UI bhi wahi dikhayega (error state). Chhoti, validate karti hui functions likho — demo ka error message bhi feedback hai, hide mat karo.

Blocks, chat aur streaming — Interface chhoti pad jaaye tab

import gradio as gr

# Blocks: layout + event flow tumhare control mein
with gr.Blocks() as chat:
    gr.Markdown("## Support assistant (demo)")
    msg = gr.Textbox(label="Sawaal")
    out = gr.Chatbot(label="Jawab")
    btn = gr.Button("Bhejo")

    def reply(q, history):
        history = history + [[q, "Demo jawab — model yahan plug hoga"]]
        yield history                  # streaming: row by row update

    btn.click(reply, [msg, out], [out])

chat.launch()
Pattern yaad rakho, signature docs se lo: Gradio ke naye versions mein component signatures badalte hain (chat format, streaming style). Interview mein pattern yaad rakho — textbox → fn → chatbot, generator → stream — aur exact signature gradio.app/docs se uthao.

Hugging Face Spaces — push karo, link milega

# Space root pe config (Gradio SDK wala app)
title: spam-check-demo
sdk: gradio                # static | gradio | docker
app_file: app.py
license: apache-2.0
# sdk_version pin karo — "latest" chhod doge toh kal app hi na chale
# resources demo-grade: chhota quota, cold start ho sakta hai
# batch job / long training yahan nahi — preview yahan
Push se pehle teen line: Requirements pin, synthetic data, aur README mein likha "demo hai, data mock hai". Ye teen line hi tumhari credibility hain — bina ye share link embarrassing ho sakta hai.

Decision table — Gradio vs Streamlit vs FastAPI

                  Gradio             Streamlit          FastAPI
kya banta hai      model ka UI         dashboard/forms   REST API
der mein           minutes             minutes           ghanton (UI khud)
seeds              function I/O        widgets/data      request schema
sahi audience      model karke dikhao  stakeholder view  doosri services
auth/rate limit    demo-grade          DIY               tum likhte ho
prod path          backend alag chahiye — wahi L10/L14 wala raasta

stage-wise decision: pehle demo (Gradio), phir dashboard (Streamlit),
contract chahiye toh API (FastAPI) — teeno ek saath bhi chalte hain
Gradio production API kyun nahi: Authz, rate limiting, request validation, observability, versioned contracts — Gradio ye handle nahi karta, aur karna bhi nahi chahiye. Wo ek UI wrapper hai — wrapper ke peeche service apni jagah chahiye.

Demo hygiene — honesty hi design rule hai

Demo ko kachra mat samjho — demo se aaya feedback curated eval set ban sakta hai: kya tune kiya, kya tod diya, kaunse inputs aaye. Isi se agla fixed version ya fine-tune ka data banta hai. Demo sirf screenshot nahi, data ka source bhi hai.

Labelling ek feature hai: Jo team demo ko product bol kar share karti hai, uska trust production mein ek hi baar tootta hai. Naam mein hi "demo" rakho — expectations sahi, feedback bhi sahi aayega.

Exercise

Question: Function ko ek call mein web UI mein badalne wala sabse tez constructor kaunsa hai?

Question: share=True se milne wala public link permanent hota hai ya temporary?

Question: Spaces ke teen SDKs — static aur gradio ke alawa teesra kaunsa?

Question: Dashboard banana ho toh Gradio ki jagah kaunsa option? (ek shabd)

Question: Demo ko backend se alag karne walo mein se ek: auth, UI widget, ya button?

Common mistakes

Desi example: Tasting counter vs restaurant ka license

Food stall wala tasting counter lagata hai — logon ko chakha kar feedback leta hai (demo). Wo counter hi uska restaurant nahi: roz ki service ke liye kitchen ka license, staff aur delivery system chahiye (backend). Gradio = tasting counter: minutes mein lag jaata hai aur feedback sach deta hai — par usko restaurant mat samjho:

python
# tasting counter (demo)
demo = gr.Interface(fn=taste, inputs=menu_item, outputs=review)
demo.launch(share=True)          # logon ne chakha, feedback aaya

# restaurant (product) — ye demo nahi karega:
#   auth | rate limit | logging | SLA | versioned API  -> FastAPI service
Interview angle — yahan se sawaal aate hain
  • Q: Gradio Interface kya karta hai? — Ek Python function ko inputs/outputs ke web UI mein wrap karta hai, ek call mein; share link se turant share.
  • Q: Gradio, Streamlit, FastAPI — kab kaunsa? — Model I/O demo: Gradio; dashboard/forms: Streamlit; service-to-service contract: FastAPI.
  • Q: Demo production kyun nahi? — Koi auth story nahi, rate limit/observability/SLA nahi, demo-grade resources — wrapper vs service ka farq.
  • Q: Spaces deploy ke liye kya chahiye? — Repo + config (sdk: gradio, app_file) + pinned requirements; secrets settings se, data synthetic.
  • Q: Demo ki asli business value kya hai? — Evidence aur feedback data (curated eval set banta hai) — architecture nahi, tool hai.
Lesson complete?

Demo bana, share kiya, boundaries bhi clear — yehi L17 ka poora khel: mechanism samajhna (VAE/GAN/diffusion), language ko reward se shape karna (RL), aur usse workflow-demo mein tod kar dikhana. L17 Generative Models & Automation yahan poora hua — ab tum theory ko component, graph aur demo teeno roop mein explain kar sakte ho.

Popular courses: Data Science Course in Lucknow · AI Course in Lucknow · Python Course in Lucknow · Power BI Course · Data Analytics Course