[개발이야기#016] 파이썬/인공지능 코드 예제를 실행해 보자. - 트랜스포머모델
안녕하세요. 가야태자 @talkit 입니다.
오늘은 인공지능 책을 좀 보고 있는데 나와 있는 예제를 한번 수행해보겠습니다.
우선 인공지능 에제를 수행하기 전에 conda 환경을 만들어야해서 ..
conda create -n aimodel1 python=3.12
를 실행해서 빈 가상환경을 하나 만들어 줍니다.
가상환경으로 접근 하기 위해서
conda activate aimodel1
위 명령어를 이용해서 접근하실 수 있습니다.
이제 책에 나오 있는 예제를 한번 수행해보겠습니다.
지금 제가 보고 있는 책은
https://product.kyobobook.co.kr/detail/S000213661263
트랜스포머로 시작하는 자연어 처리, Denis Rothman 저자(글) · 김윤기 , 박지성 , 임창대 , 하헌규 번역, 영진출판사
입니다.
일단 이게 첫번째 예제인지는 모르겠지만, 책 읽다가 실행해보고 싶어졌습니다.
코드 세네 줄 정도면 영어에서 프랑스어 번역을 할 수 있다네요 ^^
pip install transformers
저는 위 명령어로 잘 수행이 되었습니다.
vi test2.py
from transformers import pipeline
translater = pipeline("translation_en_to_fr")
print("ORG : " + "I love You.")
print("TRS : " + translater("I love You."))
위와 같이 python 코드를 test2.py로 저장 했습니다.
책의 예제는 2줄인데 실제로 변역이 되는지 보려고 한줄 더 추가 해보았습니다. ^^
python3 test2.py
(aimodel1) user@user-VirtualBox:~$ python3 test2.py
None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
No model was supplied, defaulted to google-t5/t5-base and revision 686f1db (https://huggingface.co/google-t5/t5-base).
Using a pipeline without specifying a model name and revision in production is not recommended.
config.json: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 1.21k/1.21k [00:00<00:00, 14.0MB/s]
Traceback (most recent call last):
File "/home/user/test2.py", line 3, in <module>
translater = pipeline("translation_en_to_fr")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/anaconda3/envs/aimodel1/lib/python3.12/site-packages/transformers/pipelines/__init__.py", line 895, in pipeline
framework, model = infer_framework_load_model(
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/user/anaconda3/envs/aimodel1/lib/python3.12/site-packages/transformers/pipelines/base.py", line 237, in infer_framework_load_model
raise RuntimeError(
RuntimeError: At least one of TensorFlow 2.0 or PyTorch should be installed. To install TensorFlow 2.0, read the instructions at https://www.tensorflow.org/install/ To install PyTorch, read the instructions at https://pytorch.org/.
위와 같이 오류가나네요 ^^
텐서 플로우 2.0이나 PyTorch라는 애가 필요하다네요 T.T
어차피 인공지능을 공부하려면 저 두가지를 설치해야한다고는 생각하고 있었습니다. ^^
그래서 PyTorch를 설치해보겠습니다.
가상환경은 잘 만들어져 있어서
conda install pytorch==1.9.0 torchvision==0.10.0 torchaudio==0.9.0 -c pytorch
위 명령어로 가상환경에 pytorch를 설치할 수 있다고 합니다.
헉 또 오류 입니다.
제가 python을 3.12버전을 썼는데 3.6~3.9까지만 되는 것 같습니다.
책이 좀 오래 된 것인가요 ^^
파이토치 페이지에서는 아래와 같이 설치하라고 하네요 T.T
버전을 낮출 뻔 했습니다.
conda install pytorch torchvision torchaudio cpuonly -c pytorch
그래픽 카드가 ^^ AMD꺼라서 조만간 CPU로 먼저 해보고 엔비디아로 하나 장만 해야 하나 고민중입니다.
오 설치가 끝났습니다.
다시
python3 test2.py
명령어를 날려보겠습니다.
오 뭔가를 다운로는 하는 것 같습니다.
제코드에 오류가 있네요.
가독성을 조금 높였더니 ^^
from transformers import pipeline
translater = pipeline("translation_en_to_fr")
print("ORG : " + "I love You.")
print("TRS : ")
print(translater("I love You."))
오 실행이되고 잘 나오네요 ^^
No model was supplied, defaulted to google-t5/t5-base and revision 686f1db (https://huggingface.co/google-t5/t5-base).
Using a pipeline without specifying a model name and revision in production is not recommended.
/home/user/anaconda3/envs/aimodel1/lib/python3.12/site-packages/transformers/tokenization_utils_base.py:1601: FutureWarning: `clean_up_tokenization_spaces` was not set. It will be set to `True` by default. This behavior will be depracted in transformers v4.45, and will be then set to `False` by default. For more details check this issue: https://github.com/huggingface/transformers/issues/31884
warnings.warn(
ORG : I love You.
TRS :
[{'translation_text': 'Je vous aime.'}]
이렇게 나오네요 혹시나 한국어로도 번역이 가능한지 한번 해봐야겠습니다.
from transformers import pipeline
translater = pipeline("translation_en_to_kr")
print("ORG : " + "I love You.")
print("TRS : ")
print(translater("I love You."))
이건 실패 입니다.
kr도 해보고 ko로도 해봤는데 둘다 옵션에 없다고 하네요 T.T
제가 모델 추론 파일을 만들지는 못하겠지만, 영어번역 프로그램을 만들어 보고 싶은데 일단
저 위에 책을 공부하면서 한번 해봐야겠습니다.
감사합니다.
Posted through the ECblog app (https://blog.etain.club)
[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.
[광고] STEEM 개발자 커뮤니티에 참여 하시면, 다양한 혜택을 받을 수 있습니다.
고맙습니다! 저도 열씸히 따라가고 있습니다~ ^^
글 잘 봐주셔서 감사 합니다.
지금 부터 영어를 한글로 번역하는 걸 해보려고 합니다.
글로 적어두겠습니다.
다음주는 아마 그 다음주가 감리라서 T.T 개발글은 쉬어가고 추석 쉬고 나서 다시 글을 적을 수 있을 것 같습니다. T.T