๋ฐ˜์‘ํ˜•

๐Ÿ”น ํ—ˆ๊น…ํŽ˜์ด์Šค API

Hugging Face๋Š” ๋‹ค์–‘ํ•œ NLP ๋ชจ๋ธ๊ณผ ๋„๊ตฌ๋ฅผ ๊ฐœ๋ฐœํ•˜๊ณ  ๊ณต์œ ํ•จ์œผ๋กœ์จ ๊ฐœ๋ฐœ์ž๋“ค์ด ์‰ฝ๊ฒŒ NLP ๊ธฐ์ˆ ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค. Hugging Face์˜ API ์„œ๋น„์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋‹ค์–‘ํ•œ ์‚ฌ์ „ ํ›ˆ๋ จ๋œ ์–ธ์–ด ๋ชจ๋ธ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ด๋Ÿฌํ•œ ๋ชจ๋ธ์€ ๋Œ€๊ทœ๋ชจ ๋ฐ์ดํ„ฐ์…‹์—์„œ ์‚ฌ์ „์— ํ•™์Šต๋œ ์ƒํƒœ๋กœ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค.
Hugging Face Model Hub์—๋Š” ๋‹ค์–‘ํ•œ ์ข…๋ฅ˜์˜ ์–ธ์–ด ๋ชจ๋ธ์ด ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๋ชจ๋ธ๋“ค์€ ์ด๋ฏธ ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ์…‹์œผ๋กœ ์‚ฌ์ „ ํ›ˆ๋ จ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ผ๋ฐ˜์ ์ธ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ์ž‘์—…์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
API๋ฅผ ํ†ตํ•ด Hugging Face์—์„œ ์ œ๊ณตํ•˜๋Š” ๋ฏธ๋ฆฌ ํ•™์Šต๋œ AI ๋ชจ๋ธ์„ ํ˜ธ์ถœํ•˜๊ณ  ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค๋ฉด, ๋ฌธ์žฅ ๋ถ„๋ฅ˜, ๊ฐ์„ฑ ๋ถ„์„, ๊ธฐ๊ณ„ ๋ฒˆ์—ญ, ์งˆ๋ฌธ-๋‹ต๋ณ€ ๋“ฑ๊ณผ ๊ฐ™์€ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
API ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ์ž…๋ ฅ ๋ฌธ์žฅ์ด๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํ•ด๋‹น ๋ชจ๋ธ๋กœ ์ „๋‹ฌ๋˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋กœ ์˜ˆ์ธก๊ฐ’์ด๋‚˜ ๋ถ„์„ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ API๋ฅผ ํ†ตํ•ด ๋ฏธ๋ฆฌ ํ•™์Šต๋œ AI ๋ชจ๋ธ์„ ํ™œ์šฉํ•˜๋ฉด ๋ณ„๋„์˜ ํ•™์Šต ๊ณผ์ • ์—†์ด๋„ ์‰ฝ๊ฒŒ ์ž์—ฐ์–ด ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๐Ÿ”น API Key ๋ฐœ๊ธ‰

1. ํšŒ์›๊ฐ€์ž…

ํ—ˆ๊น…ํŽ˜์ด์Šค ์‚ฌ์ดํŠธ์—์„œ ํšŒ์›๊ฐ€์ž…์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๊ธ€์„ ์“ฐ๋Š” ์‹œ์ ์—์„œ ์•„์ง oauth ์†Œ์…œ ๋กœ๊ทธ์ธ ๊ธฐ๋Šฅ์€ ์ œ๊ณตํ•˜์ง€ ์•Š์•˜๊ณ , ํšŒ์› ๊ฐ€์ž…์€ ๋งŽ์€ ์ •๋ณด์—†์ด ๊ฐ„๋‹จํžˆ ์ด๋ฃจ์–ด์กŒ์Šต๋‹ˆ๋‹ค..

https://huggingface.co/join

 

Hugging Face – The AI community building the future.

 

huggingface.co

 

2. ํšŒ์›๊ฐ€์ž…ํ›„ ์ด๋ฉ”์ผ ์ธ์ฆ

ํšŒ์›๊ฐ€์ž… ์งํ›„ ์‚ฌ์ดํŠธ ์ด์šฉ์ด ๊ฐ€๋Šฅํ•˜์ง€๋งŒ, API Key๋ฅผ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ๋Š” ๊ฐ€์ž…๋•Œ ์ž…๋ ฅํ•œ ๋ฉ”์ผ๋กœ ์ธ์ฆํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

 

3. API Key ๋ฐœ๊ธ‰

์šฐ์ธก ์ƒ๋‹จ์˜ ๋™๊ทธ๋ผ๋ฏธ๋ฅผ ํด๋ฆญํ•˜์—ฌ ๋‚˜์˜ค๋Š” Settings๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ์šฐ์ธก๋ฐ”์— Access Tokens๋ฅผ ํด๋ฆญํ•˜์—ฌ ๋‚˜์˜ค๋Š” ํ™”๋ฉด์—์„œ New token์„ ํด๋ฆญํ•˜๋ฉด token์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

New token ๋ฒ„ํŠผ์ด ๋น„ํ™œ์„ฑํ™” ๋˜์–ด์žˆ์œผ๋ฉด ๋ฉ”์ผ ์ธ์ฆ์ด ์•ˆ๋œ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

๐Ÿ”น ์ถ”๋ก  API ์‚ฌ์šฉ ์˜ˆ์ œ

์งˆ๋ฌธ ๋‹ต๋ณ€

import json
import requests

API_TOKEN = 'ํ† ํฐ ์ž…๋ ฅ'

headers = {"Authorization": f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/deepset/roberta-base-squad2"
def query(payload):
    data = json.dumps(payload)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))
data = query(
    {
        "inputs": {
            "question": "What's my name?",
            "context": "My name is Clara and I live in Berkeley.",
        }
    }
)

print(data)
"""
* ์ถœ๋ ฅ ๊ฒฐ๊ณผ
{'score': 0.9326562285423279, 'start': 11, 'end': 16, 'answer': 'Clara'}
start, end๋Š” ์ •๋‹ต index
"""

 

๊ฐ์„ฑ ๋ถ„์„

import json
import requests

API_TOKEN = 'ํ† ํฐ ์ž…๋ ฅ'

headers = {"Authorization": f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/sentence-transformers/all-MiniLM-L6-v2"
def query(payload):
    data = json.dumps(payload)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))
data = query(
    {
        "inputs": {
            "source_sentence": "That is a happy person",
            "sentences": ["That is a happy dog", "That is a very happy person", "Today is a sunny day"],
        }
    }
)

print(data)
"""
* ์ถœ๋ ฅ ๊ฒฐ๊ณผ
[0.6945773363113403, 0.9429150223731995, 0.256876140832901]
"""

 

๋ฌธ์žฅ ๋ถ„๋ฅ˜

import json
import requests

API_TOKEN = 'ํ† ํฐ ์ž…๋ ฅ'
headers = {"Authorization": f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/distilbert-base-uncased-finetuned-sst-2-english"

def query(payload):
    data = json.dumps(payload)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))

data = query({"inputs": "I like you. I love you"})

print(data)
"""
์ถœ๋ ฅ ๊ฒฐ๊ณผ
[[{'label': 'POSITIVE', 'score': 0.9998738765716553}, {'label': 'NEGATIVE', 'score': 0.00012611268903128803}]]
"""

 

Text ์ƒ์„ฑ

import json
import requests

API_TOKEN = 'ํ† ํฐ ์ž…๋ ฅ'
headers = {"Authorization": f"Bearer {API_TOKEN}"}
API_URL = "https://api-inference.huggingface.co/models/gpt2"
def query(payload):
    data = json.dumps(payload)
    response = requests.request("POST", API_URL, headers=headers, data=data)
    return json.loads(response.content.decode("utf-8"))
data = query({"inputs": "The answer to the universe is"})
print(data)
"""
์ถœ๋ ฅ ๊ฒฐ๊ณผ
[{'generated_text': 'The answer to the universe is more than that. It tells the story of how 
people have found the answer. If there is a universal God, then there must be an infinite god. 
If we choose our answers too many ways to live, then the'}]
"""

 

๋”๋งŽ์€ ๋ชจ๋ธ์€ ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•˜์„ธ์š”.

https://huggingface.co/docs/api-inference/detailed_parameters

 

Detailed parameters

 

huggingface.co

 

๋ฐ˜์‘ํ˜•

BELATED ARTICLES

more