반응형
좋아, 오늘도 힘차게 들어가자! 🎯
✅ Day 9: Hugging Face 파이프라인 실전 ②
오늘은 파이프라인을 조금 더 활용해서 문장 요약, 번역, 질문-응답 등을 체험해볼 거야.
기초지만 실무에 매우 가까운 흐름이니 집중해보자.
🔧 사전 준비 (코랩에서 실행)
from transformers import pipeline
# 필요한 파이프라인 준비
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-ko")
qa_pipeline = pipeline("question-answering")
🔢 문제 1: 문장 요약
다음 긴 문장을 요약해보자:
text = """
Hugging Face is an open-source AI company that provides a platform for sharing machine learning models.
It has become a central hub for many in the AI community, enabling researchers and developers to collaborate
and use each other's models. Their Transformers library supports thousands of pretrained models in over 100 languages.
"""
👉 요약한 결과를 print()로 출력해줘.
🔢 문제 2: 번역
문제 1에서 쓴 원문(text)을 한국어로 번역해보자.
단, 너무 길면 번역 오류가 날 수 있으니 앞 문장 하나만 번역해도 OK!
🔢 문제 3: 질문-응답
다음 context에 대해 질문을 던져보자:
context = "지구는 태양계에서 세 번째 행성으로, 생명체가 존재하는 유일한 곳이다. 표면은 약 71%가 물로 덮여 있으며, 대기는 질소와 산소가 주를 이룬다."
question = "지구의 대기는 어떤 성분으로 이루어져 있나요?"
👉 qa_pipeline을 사용해서 답을 추출해 출력해줘!
🔢 문제 4: 내 문장 요약
너가 직접 임의로 한글 긴 문장을 써보고, 그것을 영어로 번역한 뒤 다시 요약해봐.
요약은 영어든 한국어든 OK!
🔢 문제 5: 혼합 활용
text, question, summarization, translation, qa_pipeline을 조합해서
요약 → 번역 → 질문하기의 흐름을 만들어보자.
예:
- 긴 영어 문장을 요약
- 그 요약을 한국어로 번역
- 번역된 내용에서 정보 하나를 질문하여 답을 얻기
원하면 예시 코드도 제공해줄게.
시작해볼까? 😊
# problem 01
text = """
Hugging Face is an open-source AI company that provides a platform for sharing machine learning models.
It has become a central hub for many in the AI community, enabling researchers and developers to collaborate
and use each other's models. Their Transformers library supports thousands of pretrained models in over 100 languages.
"""
summary = summarizer(text , max_length = 45 , min_length = 20 , do_sample = False)
print(summary[0]['summary_text'])
# problem 02
text = """
Hugging Face is an open-source AI company that provides a platform for sharing machine learning models.
It has become a central hub for many in the AI community, enabling researchers and developers to collaborate
and use each other's models. Their Transformers library supports thousands of pretrained models in over 100 languages.
"""
translated = translator(text , max_length = 1000)
print(translated[0]["translation_text"])
# problem 03
context = "지구는 태양계에서 세 번째 행성으로, 생명체가 존재하는 유일한 곳이다. 표면은 약 71%가 물로 덮여 있으며, 대기는 질소와 산소가 주를 이룬다."
question = "지구의 대기는 어떤 성분으로 이루어져 있나요?"
result = qa_pipeline(question = question , context = context)
print(result)
# problem 04
text = '''
반갑다 여러분 난 대한민국 전 대통령 이명박이다. 나는 환승 제도를 만들었고, 국밥을 매우 맛있게 먹지.
모든 국민은 나를 좋아한다. 그럼 안녕!
'''
translated = translator(text , max_length = 1000)
translated_text = translated[0]["translation_text"]
print(translated_text)
summary = summarizer(translated_text , max_length = 45 , min_length = 20 , do_sample = False)
print(summary[0]['summary_text'])
'2025 SCG' 카테고리의 다른 글
20250510_ChatGpt와 공부하기_Day11 (0) | 2025.05.10 |
---|---|
20250509_ChatGpt와 공부하기_Day10 (0) | 2025.05.09 |
20250507_ChatGpt와 공부하기_Day8 (0) | 2025.05.07 |
20250506_ChatGpt와 공부하기_Day7 (0) | 2025.05.06 |
20250505_ChatGpt와 공부하기_Day6 (0) | 2025.05.05 |