C.W.K.
Stream
Lesson 02 of 04 · published

Gradio: 10 줄로 ML 데모

~26 min · spaces, gradio

Level 0스카우트
0 XP0/50 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

Gradio 시그니처: Python 함수가 UI 됨

함수 정의. gr.Interface 또는 gr.Blocks 로 wrap. Gradio 가 시그니처 inspect, inputs/outputs 자동 빌드, 웹 UI 서빙. demo.launch() 가 로컬 서버 시작; Spaces 에선 런타임이 자동 launch() 호출.

레이아웃 둘

  • gr.Interface(fn, inputs, outputs) — 함수 하나, UI 하나. 단일 task 데모에 best.
  • gr.Blocks() — explicit 레이아웃. 여러 inputs/outputs, 탭, 커스텀 CSS, 조건부 visibility. Interface 가 너무 rigid 할 때.

Streaming 과 ChatInterface

챗 모델엔 gr.ChatInterface 가 메시지 history + streaming 을 generator-based handler 로 핸들. InferenceClient 또는 로컬 모델과 같은 패턴.

Code

HF 모델의 10 줄 Gradio 데모·python
import gradio as gr
from transformers import pipeline

pipe = pipeline("sentiment-analysis")

def classify(text):
    out = pipe(text)[0]
    return f"{out['label']} ({out['score']:.2f})"

demo = gr.Interface(fn=classify, inputs=gr.Textbox(label="Review"), outputs="text")
demo.launch()
InferenceClient 로 streaming 챗·python
import gradio as gr
from huggingface_hub import InferenceClient

client = InferenceClient(model="meta-llama/Llama-3.1-8B-Instruct", provider="hf-inference")

def chat(message, history):
    messages = [{"role": h[0] and "user" or "assistant", "content": h[1]} for h in history if h]
    messages.append({"role": "user", "content": message})
    stream = client.chat_completion(messages=messages, max_tokens=500, stream=True)
    answer = ""
    for chunk in stream:
        delta = chunk.choices[0].delta.content or ""
        answer += delta
        yield answer

gr.ChatInterface(chat, title="Llama Chat").launch()

External links

Exercise

작은 HF 모델 (예: text classification 또는 whisper-tiny) wrap 하는 Gradio Space 빌드. 로컬 먼저 테스트. Space 에 push. 다른 모델 데모하는 두 번째 탭 추가. 둘 다 side-by-side 동작 검증.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.