< Hugging Face API 활용>
Hugging Face
- AI/NLP 오픈소스 생태계의 중심 역할을 하는 회사이자 커뮤니티
- 시작은 챗봇 스타트업 → 자연어처리 모델 공유 플랫폼으로 전환
- 지금은 머신러닝 모델 허브 + 오픈소스 라이브러리 + 협업 생태계로 자리 잡음
주요 서비스와 생태계
- Transformers 라이브러리
- 가장 유명한 오픈소스 패키지
- BERT, GPT, T5, BLOOM 등 최신 모델을 한줄 코드로 불러와 사용 가능
- PyTorch,TensforFlow 모두 지원
- Datasets 라이브러리
- 공개 데이터셋을 쉽게 불러올 수 있는 툴
- NLP 뿐 아니라 이미지, 오디오, 데이터까지 포함
- Model Hub
- 수십만 개의 오픈소스 모델이 업로드된 모델저장소
- GitHub처럼 모델 버전관리 및 공유 가능
- Spaces
- Streamlit, Gradio 같은 프레임워크로 데모 웹앱을 바로 만들고 공유 가능
<허깅페이스 로그인 인증>
from huggingface_hub import notebook_login
notebook_login()
# 위 방법이 안될 시
from huggingface_hub import login
from dotenv import load_dotenv
import os
## 1️⃣ .env 파일의 내용을 로드
load_dotenv(override=True)
## 2️⃣ 환경 변수 가져오기
HF_READ_TOKEN = os.getenv("HF_READ_TOKEN")
# # print(HF_READ_TOKEN)
## 로그인 실행
login(HF_READ_TOKEN)
from huggingface_hub import notebook_login
notebook_login(new_session=True, write_permission=False)
HuggingFace Transformers 라이브러리 활용
from transformers import pipeline
자연어 인식 모델
# 문장 생성 모델 , 허깅페이스 서버에서 모델을 받아옴
pipe = pipeline("text-generation")
# 오류가 발생하면, pytorch 설치
pip install torch torchvision --index-url https://download.pytorch.org/whl/cu126
# 문장 생성 모델 , 허깅페이스 서버에서 모델을 받아옴
# 모델을 지정하지 않으면, default openai-gpt2를 다운 받음.
pipe = pipeline("text-generation")
# 여전히 문제, tf-keras 설치
# TensorFlow/Keras 기반으로 실행하고 싶으시다면 아래 명령어로 해결
# 이 패키지는 Keras 3 환경에서 Transformers가 기대하는 tf_keras 모듈을 만들어줍
# pip install tf-keras
pipe = pipeline("text-generation")
pipe("인공지능은 무엇인가?")
- gpt-2는 한국어를 잘 못하기 때문에 영어 사용
result = pipe("what is AI")
result[0]['generated_text']
한국어 인식 잘 되는 모델
pipe = pipeline("text-generation", model="skt/kogpt2-base-v2")
pipe("인공지능에 대해 설명해줘")
- LGAI-EXAONE/EXAONE-4.0-1.2B : 2B
# LGAI-EXAONE/EXAONE-4.0.1-2B
pipe = pipeline("text-generation", model="LGAI-EXAONE/EXAONE-4.0-1.2B")
pipe("인공지능에 대해 설명해줘")
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "LGAI-EXAONE/EXAONE-4.0-1.2B"
model = AutoModelForCausalLM.from_pretrained(
model_name,
dtype="bfloat16",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
# choose your prompt
prompt = "Explain how wonderful you are"
prompt = "Explica lo increíble que eres"
prompt = "너가 얼마나 대단한지 설명해 봐"
messages = [
{"role": "user", "content": prompt}
]
input_ids = tokenizer.apply_chat_template(
messages,
tokenize=True,
add_generation_prompt=True,
return_tensors="pt"
)
output = model.generate(
input_ids.to(model.device),
max_new_tokens=128,
do_sample=False,
)
print(tokenizer.decode(output[0]))
감성 분석 모델
classifier = pipeline("sentiment-analysis")
result = classifier("I love using Huggin Face transformers")
result
data = ['This restaurant serves delicious food.', "Idon't like to wake up early."]
result = classifier(data)
result
data = ['햄버거가 맛이 별로다', '나는 수영을 잘하지 못한다.', ' 이 카페의 커피맛이 예술이네.']
classifier(data)
# 한국어 인식을 잘 못함, 한국어 감석 분석 모델 사용 필요
한국어 감성 분석
- WhitePeak/bert-base-cased-Korean-sentiment
# 기본 모델을 파인 튜닝한 모델
classifier = pipeline("sentiment-analysis", model = "WhitePeak/bert-base-cased-Korean-sentiment")
data = ['햄버거가 맛이 별로다', '나는 수영을 잘하지 못한다.', ' 이 카페의 커피맛이 예술이네.']
# label_0 : 부정
# label_1 : 긍정
classifier(data)
좋은 모델 소개
- google/gemma-3-270m-it -> 한글이 잘됨
- google 승인 받기
# google/gemma-3-270m-it, 승인 필요
generation = pipeline("text-generation", model="google/gemma-3-270m-it")
generation("인공지능이란?")[0]['generated_text']
문장요약
summ = pipeline('summarization', framework="pt")
sample_test= """
임영웅의 출연으로 화제를 모은 '불후의 명곡'에 '테크노 여전사' '원조 멀티테이너'로 불리는 이정현이 아티스트로 출격한다.
12일 아이즈(IZE) 취재 결과, 오는 22일 진행되는 KBS 2TV '불후의 명곡'(이하 '불후') 녹화가 '아티스트 이정현 편'으로 꾸며진다.
"""
summ(sample_test)
# psyche/KoT5-summarization
summ = pipeline('summarization', model="psyche/KoT5-summarization", framework="pt")
sample_test= """
임영웅의 출연으로 화제를 모은 '불후의 명곡'에 '테크노 여전사' '원조 멀티테이너'로 불리는 이정현이 아티스트로 출격한다.
12일 아이즈(IZE) 취재 결과, 오는 22일 진행되는 KBS 2TV '불후의 명곡'(이하 '불후') 녹화가 '아티스트 이정현 편'으로 꾸며진다.
"""
summ(sample_test)
텍스트 생성
from transformers import pipeline
pipe = pipeline("text-generation", model="gpt2", framework="pt")
print(pipe("Hello, my name is")[0]["generated_text"])
Huggingface를 활용한 이미지 인식
from transformers import pipeline
classifier = pipeline("image-classification")
# 이미지
img_path = "datas/사과.jpg"
classifier(img_path)
객체 탐지
# object detection
detector = pipeline("object-detection")
pip install timm
import timm
print(timm.__version__)
# 객체 탐지
image_path = 'datas/도로.jpg'
results = detector(image_path)
results
결과 그리기
import matplotlib.pyplot as plt
import matplotlib.patches as patches
image = plt.imread(image_path)
fig, ax = plt.subplots(1)
ax.imshow(image)
for result in results:
xmin = int(result['box']['xmin'])
ymin = int(result['box']['ymin'])
xmax = int(result['box']['xmax'])
ymax = int(result['box']['ymax'])
width = xmax - xmin
height = ymax - ymin
score = result['score']
label = result['label']
rect = patches.Rectangle((xmin, ymin), width, height, linewidth=2, edgecolor='r', facecolor='none')
ax.add_patch(rect)
ax.text(xmin, ymin, f'{label}: {score:.2f}', color='r', fontsize=10, bbox=dict(facecolor='white', alpha=0.5))
plt.axis('off')
plt.show()
이미지 생성 (text to image)
pip install diffusers
pip install huggingface_hub[all]
from diffusers import DiffusionPipeline
import torch
# 오류 발생함
# runwayml/stable-diffusion-v1-5
pipe = DiffusionPipeline.from_pretrained('runwayml/stable-diffusion-v1-5')
# 텍스트 투 이미지
# 한글 잘 안됨
# result = pipe("바다를 바라보는 여성을 그려줘")
# 영어로 하기
result = pipe("Draw a woman looking at the sea")
result
# 이미지 추출
image = result.images[0] # 첫 번째 이미지 선택
# 이미지 보여주기
image.show() # 기본 이미지 뷰어에서 확인
# 이미지 저장 (선택)
image.save("output.png")
GPU에 stable-diffusion 실행
from diffusers import DiffusionPipeline
import torch
# 1. GPU 사용 가능 확인
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Using device: {device}")
# 2. 파이프라인 로드 (float16으로 메모리 절약)
pipe = DiffusionPipeline.from_pretrained(
'runwayml/stable-diffusion-v1-5',
torch_dtype=torch.float16 # GPU 메모리 절약
)
# 3. GPU로 이동
pipe = pipe.to(device)
# 4. 이미지 생성
result = pipe("Draw a woman looking at the sea")
# 5. 이미지 보여주기
image.show() # 기본 이미지 뷰어에서 확인
# 6. 이미지 저장
result.images[0].save("output.png")