๐Ÿ Python & library/HuggingFace

HuggingFace Space ๋งŒ๋“ค๊ธฐ

๋ณต๋งŒ 2023. 6. 10. 15:52

HuggingFace Space๋Š” ๊ฐ„ํŽธํ•˜๊ณ  ์‰ฝ๊ฒŒ ML ๋ฐ๋ชจ ์•ฑ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์ด๋‹ค.

 

Spaces - Hugging Face

Spaces Discover amazing ML apps made by the community!

huggingface.co

Space๋“ค์˜ ์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

1: text-to-image

 

Rich Text To Image - a Hugging Face Space by songweig

 

huggingface.co

2: chat-with-GPT4

 

Chat-with-GPT4 - a Hugging Face Space by ysharma

 

huggingface.co

 

๋ณธ ๊ธ€์—์„œ๋Š” ์ด๋Ÿฌํ•œ HuggingFace Space๋ฅผ ๋งŒ๋“œ๋Š” ๋ฒ•์— ๋Œ€ํ•ด ๊ฐ„๋žตํžˆ ์†Œ๊ฐœํ•˜๊ณ ์ž ํ•ฉ๋‹ˆ๋‹ค. ๋ณด๋‹ค ์ž์„ธํ•œ ์„ค๋ช…์€ ๊ณต์‹ docs๋ฅผ ์ฐธ๊ณ ๋ฐ”๋žŒ.

 

HuggingFace Space๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด Streamlit๊ณผ Gradio ๋‘๊ฐ€์ง€ ์ข…๋ฅ˜์˜ SDK๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๋ณด๋‹ค ๋””ํ…Œ์ผํ•œ ์ปค์Šคํ…€์„ ์œ„ํ•ด JavaScript๋‚˜ HTML์„ ์ด์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค.

 

์ด ๊ธ€์—์„œ๋Š” Gradio๋ฅผ ์‚ฌ์šฉํ•ด Space๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒŸ์Šต๋‹ˆ๋‹ค.

 

 

 

1. Space ์ƒ์„ฑํ•˜๊ธฐ


Space Home์—์„œ Create new Space ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ƒˆ๋กœ์šด Space๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

Space์˜ ์ด๋ฆ„๊ณผ ๋ผ์ด์„ผ์Šค, SDK, Space๊ฐ€ ๋Œ์•„๊ฐˆ ํ•˜๋“œ์›จ์–ด ์ข…๋ฅ˜ ๋“ฑ์„ ์„ ํƒํ•  ์ˆ˜ ์žˆ๋‹ค.

SDK๋Š” space๋ฅผ ๋งŒ๋“ค ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ํˆด์ด๋‹ค. Streamlit, Gradio๋“ฑ์ด ์žˆ๋‹ค.

 

 

 

ํ•˜๋“œ์›จ์–ด์˜ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ์˜ต์…˜์ธ CPU๋Š” ๋ฌด๋ฃŒ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, CPU ๊ฐฏ์ˆ˜๋ฅผ ๋Š˜๋ฆฌ๊ฑฐ๋‚˜ GPU๋ฅผ ์ด์šฉํ•˜๋ ค๋ฉด ์ถ”๊ฐ€ ์š”๊ธˆ์„ ๋‚ด์•ผ ํ•œ๋‹ค.

Inference๋งŒ ํ• ๊ฑฐ๋ผ๋ฉด ์กฐ๊ธˆ ๋Š๋ฆฌ์ง€๋งŒ ๊ธฐ๋ณธ CPU๋„ ์ถฉ๋ถ„ํ•œ ๊ฒƒ ๊ฐ™๋‹ค.

 

 

Space๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์Šคํƒ€ํŠธ ํŽ˜์ด์ง€๊ฐ€ ๋œฌ๋‹ค.

 

 

Space์˜ ์ฝ”๋“œ๋“ค์€ git repository์— ์ €์žฅ๋œ๋‹ค. ์ด repository๋ฅผ cloneํ•ด์„œ ์ฝ”๋“œ๋ฅผ ์ˆ˜์ •ํ•  ์ˆ˜ ์žˆ๊ณ ,

commit๋“ค์ด push๋  ๋•Œ๋งˆ๋‹ค ํ•ด๋‹น ๋‚ด์šฉ์ด space์— ์ž๋™์œผ๋กœ ๋ฐ˜์˜๋œ๋‹ค.

 

 

 

2. Gardio๋กœ space ๊พธ๋ฏธ๊ธฐ (๊ธฐ์ดˆ)

์ƒ์„ฑํ•œ repository๋ฅผ cloneํ•˜๊ณ , ๊ทธ ์•ˆ์— app.py ํŒŒ์ผ์„ ์ถ”๊ฐ€ํ•˜์—ฌ app์„ ์‹คํ–‰์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

 

์˜ˆ์‹œ๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๊ณ  pushํ•˜๋ฉด,

#app.py

import gradio as gr

def greet(name):
    return "Hello " + name + "!!"

iface = gr.Interface(fn=greet, inputs="text", outputs="text")
iface.launch()

 

HuggingFace Space์—์„œ ์ž๋™์œผ๋กœ ๋นŒ๋“œ๊ฐ€ ์ง„ํ–‰๋œ๋‹ค.

 

 

๋นŒ๋“œ๊ฐ€ ์™„๋ฃŒ๋˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์ด ์ƒ์„ฑ๋œ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

 

3. Gradio Interface

์œ„ ์ฝ”๋“œ์—์„œ ์‚ฌ์šฉํ•œ gradio.Interface๋Š”

ML ๋ชจ๋ธ์„ ์‚ฌ์šฉํ•˜๋Š” web-based GUI๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” class์ด๋‹ค.

 

gradio.Interface๋Š” ์„ธ๊ฐ€์ง€์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

gradio.Interface(fn, inputs, outputs, ...)

 

์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ์ธ fn์€ ํ•ด๋‹น interface๊ฐ€ ์‹คํ–‰๋  ๋•Œ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์— ํ•ด๋‹นํ•œ๋‹ค. 

fn์˜ ์ž…๋ ฅ ํ˜•ํƒœ๋Š” inputs์—, fn์˜ ์ถœ๋ ฅ ํ˜•ํƒœ๋Š” outputs๋กœ ์ง€์ •ํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

inputs์™€ outputs๋Š” ํ•˜๋‚˜์˜ Gradio Component,

ํ˜น์€ ๊ฐ Gradio Component์— ํ•ด๋‹นํ•˜๋Š” string shortcut์œผ๋กœ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ๋‹ค. 

 

์˜ˆ์‹œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

import gradio as gr

def image_classifier(inp):
    return {'cat': 0.3, 'dog': 0.7}

demo = gr.Interface(fn=image_classifier, inputs="image", outputs="label")
demo.launch()

 

์œ„ ์ฝ”๋“œ๋Š” ์ด๋ฏธ์ง€(gradio.Image)๋ฅผ ์ž…๋ ฅ์œผ๋กœ ๋ฐ›๊ณ ,

 

 

classification label(gradio.label)์„ ์ถœ๋ ฅํ•œ๋‹ค.

 

 

์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์˜ ํ˜•ํƒœ๋ฅผ list๋กœ ์ฃผ์–ด์„œ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ž…๋ ฅ๊ณผ ์ถœ๋ ฅ์„ ์ง€์ •ํ•ด์ค„ ์ˆ˜๋„ ์žˆ๋‹ค.

import gradio as gr

def greet(name, is_morning, temperature):
    salutation = "Good morning" if is_morning else "Good evening"
    greeting = f"{salutation} {name}. It is {temperature} degrees today"
    celsius = (temperature - 32) * 5 / 9
    return greeting, round(celsius, 2)

demo = gr.Interface(
    fn=greet,
    inputs=["text", "checkbox", gr.Slider(0, 100)],
    outputs=["text", "number"],
)
demo.launch()

 

 

gradio.Interface์— ๋Œ€ํ•œ ๋ณด๋‹ค ์ž์„ธํ•œ ์„ค๋ช…๊ณผ ๊ฐ component์˜ ์ข…๋ฅ˜, shortcut์€ Gradio Docs์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

 

4. ํ•ซ๋„๊ทธ ํŒ๋ณ„๊ธฐ ๋งŒ๋“ค๊ธฐ

HuggingFace์˜ Models์—์„œ๋Š” ์—ฌ๋Ÿฌ ์‚ฌ๋žŒ๋“ค์ด ์˜ฌ๋ฆฐ ๋ชจ๋ธ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

Models - Hugging Face

 

huggingface.co

 

์ด ์ค‘ julien-c๊ฐ€ ์—…๋กœ๋“œํ•œ hotdog-not-hotdog ๋ชจ๋ธ์„ ์ด์šฉํ•ด ํ•ซ๋„๊ทธ ํŒ๋ณ„๊ธฐ Space๋ฅผ ๋งŒ๋“ค์–ด ๋ด…์‹œ๋‹ค.

 

julien-c/hotdog-not-hotdog · Hugging Face

This model can be loaded on the Inference API on-demand.

huggingface.co

 

#app.py

import gradio as gr
from transformers import pipeline

pipeline = pipeline(task="image-classification", model="julien-c/hotdog-not-hotdog")

def predict(image):
    predictions = pipeline(image)
    return {p["label"]: p["score"] for p in predictions}

gr.Interface(
    predict,
    inputs=gr.inputs.Image(label="Upload hot dog candidate", type="filepath"),
    outputs=gr.outputs.Label(num_top_classes=2),
    title="Hot Dog? Or Not?",
).launch()

 

transformers.pipeline์„ ์ด์šฉํ•˜๋ฉด ML ํŒŒ์ดํ”„๋ผ์ธ์„ ๊ฐ„ํŽธํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์‚ฌ์šฉํ•  ๋ชจ๋ธ๋กœ hotdog-not-hotdog๋ฅผ ์ง€์ •ํ•ด์ฃผ๊ณ ,

predict ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“  ํ›„

gr.Interface์— ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค.

Interface์˜ ์ž…๋ ฅ์œผ๋กœ๋Š” ์ด๋ฏธ์ง€, ์ถœ๋ ฅ์œผ๋กœ๋Š” classification label๋กœ ์ง€์ •ํ•ด์ค€๋‹ค.

 

์ด ๋•Œ HuggingFace Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—,

๋‹ค์Œ dependency๋ฅผ requirements.txt ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค.

#requirements.txt

transformers
torch

 

app.py ํŒŒ์ผ๊ณผ requirements.txt ํŒŒ์ผ์„ pushํ•˜๊ณ 

space๋ฅผ ์ƒˆ๋กœ๊ณ ์นจํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ํ™”๋ฉด์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

ํ•ซ๋„๊ทธ์ธ์ง€ ์•„๋‹Œ์ง€ ํŒ๋ณ„ํ•˜๋Š” ๋ฐ์— ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 

๋ฐ˜์‘ํ˜•