๐ Data Engineering
Gradio
Gradio
An ML demo UI builder. Acquired by Hugging Face.
Gradio
An ML demo UI builder. Acquired by Hugging Face.
Gradio is a Python library that lets you quickly build web-based demo UIs for machine learning models. It was acquired by Hugging Face in 2022 and has become a core tool of the Hugging Face ecosystem.
| Component | Use case | Examples |
|---|---|---|
| gr.Textbox | Text Input/Output | Chatbot, translation |
| gr.Image | Image Input/Output | Image classification, generation |
| gr.Audio | Audio Input/Output | Speech recognition, TTS |
| gr.Chatbot | Conversational Interface | LLM chat |
| gr.File | File Upload/Download | Document processing |
# ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ ๋ฐ๋ชจ UI
import gradio as gr
from transformers import pipeline
# ์ฌ์ ํ์ต๋ ์ด๋ฏธ์ง ๋ถ๋ฅ ๋ชจ๋ธ ๋ก๋
classifier = pipeline("image-classification", model="google/vit-base-patch16-224")
def classify_image(image):
"""์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฅํ๊ณ ๊ฒฐ๊ณผ ๋ฐํ"""
results = classifier(image)
return {result['label']: result['score'] for result in results}
# Gradio ์ธํฐํ์ด์ค ์์ฑ
demo = gr.Interface(
fn=classify_image,
inputs=gr.Image(type="pil", label="์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ์ธ์"),
outputs=gr.Label(num_top_classes=5, label="๋ถ๋ฅ ๊ฒฐ๊ณผ"),
title="์ด๋ฏธ์ง ๋ถ๋ฅ ๋ฐ๋ชจ",
description="์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํ๋ฉด AI๊ฐ ๋ถ๋ฅํฉ๋๋ค.",
examples=[
["examples/cat.jpg"],
["examples/dog.jpg"],
["examples/car.jpg"]
],
cache_examples=True
)
demo.launch(share=True) # ๊ณต๊ฐ ๋งํฌ ์์ฑ
# LLM ๊ธฐ๋ฐ ์ฑ๋ด ๋ฐ๋ชจ
import gradio as gr
from openai import OpenAI
client = OpenAI()
def chat_with_gpt(message, history):
"""GPT์ ๋ํํ๋ ํจ์"""
# ๋ํ ์ด๋ ฅ์ OpenAI ํ์์ผ๋ก ๋ณํ
messages = [{"role": "system", "content": "๋น์ ์ ์น์ ํ AI ์ด์์คํดํธ์
๋๋ค."}]
for human, assistant in history:
messages.append({"role": "user", "content": human})
messages.append({"role": "assistant", "content": assistant})
messages.append({"role": "user", "content": message})
# ์คํธ๋ฆฌ๋ฐ ์๋ต
response = client.chat.completions.create(
model="gpt-4",
messages=messages,
stream=True
)
partial_message = ""
for chunk in response:
if chunk.choices[0].delta.content:
partial_message += chunk.choices[0].delta.content
yield partial_message
# Chatbot ์ธํฐํ์ด์ค
demo = gr.ChatInterface(
fn=chat_with_gpt,
title="GPT-4 ์ฑ๋ด",
description="GPT-4์ ๋ํํด๋ณด์ธ์!",
examples=["์๋
ํ์ธ์!", "Python์์ ๋ฆฌ์คํธ์ ํํ์ ์ฐจ์ด์ ์?", "์ค๋ ๋ ์จ๊ฐ ์ด๋?"],
retry_btn="๋ค์ ์๋",
undo_btn="์คํ ์ทจ์",
clear_btn="๋ํ ์ด๊ธฐํ"
)
demo.launch()
# Blocks API๋ฅผ ์ฌ์ฉํ ๋ณต์กํ ๋ ์ด์์
import gradio as gr
import torch
from diffusers import StableDiffusionPipeline
# Stable Diffusion ๋ชจ๋ธ ๋ก๋
pipe = StableDiffusionPipeline.from_pretrained(
"runwayml/stable-diffusion-v1-5",
torch_dtype=torch.float16
)
pipe = pipe.to("cuda")
def generate_image(prompt, negative_prompt, num_steps, guidance_scale):
"""์ด๋ฏธ์ง ์์ฑ ํจ์"""
image = pipe(
prompt=prompt,
negative_prompt=negative_prompt,
num_inference_steps=num_steps,
guidance_scale=guidance_scale
).images[0]
return image
# Blocks API๋ก ์ปค์คํ
๋ ์ด์์
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# Stable Diffusion ์ด๋ฏธ์ง ์์ฑ๊ธฐ")
gr.Markdown("ํ
์คํธ ํ๋กฌํํธ๋ก ์ด๋ฏธ์ง๋ฅผ ์์ฑํ์ธ์!")
with gr.Row():
with gr.Column(scale=2):
prompt = gr.Textbox(
label="ํ๋กฌํํธ",
placeholder="์์ฑํ๊ณ ์ถ์ ์ด๋ฏธ์ง๋ฅผ ์ค๋ช
ํ์ธ์...",
lines=3
)
negative_prompt = gr.Textbox(
label="๋ค๊ฑฐํฐ๋ธ ํ๋กฌํํธ",
placeholder="์ ์ธํ๊ณ ์ถ์ ์์...",
lines=2
)
with gr.Row():
num_steps = gr.Slider(
minimum=10, maximum=50, value=30, step=1,
label="์ถ๋ก ์คํ
์"
)
guidance_scale = gr.Slider(
minimum=1, maximum=20, value=7.5, step=0.5,
label="๊ฐ์ด๋์ค ์ค์ผ์ผ"
)
generate_btn = gr.Button("์ด๋ฏธ์ง ์์ฑ", variant="primary")
with gr.Column(scale=3):
output_image = gr.Image(label="์์ฑ๋ ์ด๋ฏธ์ง", type="pil")
# ์์
gr.Examples(
examples=[
["A beautiful sunset over mountains, digital art", "blurry, low quality", 30, 7.5],
["A cute robot reading a book, cartoon style", "realistic, photo", 30, 7.5],
],
inputs=[prompt, negative_prompt, num_steps, guidance_scale],
outputs=output_image,
fn=generate_image,
cache_examples=True
)
# ์ด๋ฒคํธ ์ฐ๊ฒฐ
generate_btn.click(
fn=generate_image,
inputs=[prompt, negative_prompt, num_steps, guidance_scale],
outputs=output_image
)
# Hugging Face Spaces ๋ฐฐํฌ์ฉ ์ค์
demo.queue() # ์์ฒญ ํ์ ํ์ฑํ
demo.launch(server_name="0.0.0.0", server_port=7860)
# Gradio ์ฑ์ ์๋ ์์ฑ๋ API ์ฌ์ฉํ๊ธฐ
from gradio_client import Client
# Hugging Face Spaces์ ๋ฐฐํฌ๋ ์ฑ ์ฐ๊ฒฐ
client = Client("username/my-gradio-app")
# API ํธ์ถ
result = client.predict(
"Hello, how are you?", # ์
๋ ฅ ํ
์คํธ
api_name="/predict" # ์๋ํฌ์ธํธ
)
print(result)
# ๋น๋๊ธฐ ํธ์ถ
job = client.submit(
"Generate a story about AI",
api_name="/generate"
)
print(job.result()) # ๊ฒฐ๊ณผ ๋๊ธฐ