Today's

길을 나서지 않으면 그 길에서 만날 수 있는 사람을 만날 수 없다

파이썬 스크립트

파이썬 스크립트 (feat GEMINI API) 번역기로 사용해 볼까요?

Billcorea 2024. 2. 14. 15:15
반응형
# Gemini API Key AIzaSyDyghKoKR6ZiYmkX72DpATJOkMylieenPU

import google.generativeai as genai

GOOGLE_API_KEY='AIza*********************************PU'
genai.configure(api_key=GOOGLE_API_KEY)

# for m in genai.list_models():
#   if 'generateContent' in m.supported_generation_methods:
#     print(m.name)

model = genai.GenerativeModel('gemini-pro')
prompt = '''
You are a competent translator. Please convert the following English words. The next word is {0}. 
The country codes that need to be converted are 'ar', bn', 'cn','de','es','fr','hi','in','it','ja','ko','ms',' nl','pt','ru','th','tr','vi','zh'
Please indicate how to print in the format ‘Country Code: Converted Word’.
'''.format('Written By')
response = model.generate_content(prompt)
print(response.text)

 

안드로이드 앱을 구현하는 동안 다국어 버전을 만들기 위해서는 번역을 해야 하는 경우가 발생하게 됩니다.  그래서 이번에는 GEMINI API을 활용해서 간단한 번역기(?)를 만들어 보려고 합니다. 

 

번역 예시

 

GEMINI API 관련된 여러글에서 강조했던 것처럼 prompt을 어떻게 작성하는 가에 따라서 달라질 수 있습니다.  예시 코드의 prompt는 간략하게 만들어 본 것이기는 합니다. 또한 AI의 단점은 간혹 거짓말(?)을 할 수 있기도 합니다. 이런 부분만을 유의해서 잘 활용해 보면 좋을 것 같습니다. 

 

예전에 했던 Translate API 을 활용했던 것보다 훨씬 단순하게 번역을 해 볼 수 있었습니다.  이제 다국어 버전의 앱을 조금은 더 쉽게 만들어 보도록 하겠습니다.

 

반응형