colab:: 구글코랩에서 python dotenv 처럼 변수 사용하려면

 

코랩의 왼쪽 메뉴에서 '열쇠' 모양을 선택.

 

- 여기에 변수, api를 저장하고

- 변수를 불러오는 코드를 실행하려는 코랩 첫 부분에서 실행한다.






다른 방법

아래처럼 code block을 사용하는 방법도 추천한다 



#@title API KEY IMPORT
 
from google.colab import userdata
 
select_api_key = 'GEMINI_API_KEY / OPENAI_API_KEY'  # @param {type: "string"}
 
try:
  TEMP_COLAB_API_KEY=userdata.get(select_api_key)
 
except userdata.SecretNotFoundError as e:
   print(f'''Secret not found\n\nThis expects you to create a secret named {TEMP_COLAB_API_KEY} in Colab\n\nVisit https://makersuite.google.com/app/apikey to create an API key\n\nStore that in the secrets section on the left side of the notebook (key icon)\n\nName the secret {gemini_api_secret_name}''')
   raise e
except userdata.NotebookAccessError as e:
  print(f'''You need to grant this notebook access to the {TEMP_COLAB_API_KEY} secret in order for the notebook to access Gemini on your behalf.''')
  raise e
except Exception as e:
  # unknown error
  print(f"There was an unknown error. Ensure you have a secret {TEMP_COLAB_API_KEY} stored in Colab and it's a valid key from https://makersuite.google.com/app/apikey")
  raise e

댓글 쓰기 · 수정

0 댓글