python + google sheet + ChatGPT 재미있네요!

2023.02.14 14:59 3,009 Views

안녕하세요.


Why?

활용 도중 에러 많이 발생

google sheet + ChatGPT를 사용중 장문 메세지의 경우 google sheet 에서 응답시간 오류가 납니다.

따라서 응답시간을 파이썬에서 받아주면 에러없이 여러가지 질문을 할 수 있지 않을까? 입니다.

GOAL

일상생활에 적극 사용

python + google cloud + google sheet + ChatGPT 연결하여 토이 서비스를 만들고자 합니다. (업데이트 중)


GPT PLUS 결제 완료

API 에서 secret key 카피

  1. 첫번째 python + google sheet

Python을 사용하여 Google Spreadsheet에서 데이터를 읽으려면 Google Sheets API를 통해

Google Sheets와 연결 가능한  gspread 라이브러리를 사용할 수 있습니다.



주피터 노트북에서 이용해 보세요 신세계가 열립니다.

  • 구글 드라이브 연결 전 구글 클라우드 설정


계속 버튼 누름



구글 드라이브 API 설치

구글 스프레드 시트 API 설치

구글 드라이브 와 구글 스프레드시트 설치

구글 스프레드시트로 이동


Python + Google Spread sheet 주피터 노트북 코드 작성

아래는 gspread를 사용하여 Google 스프레드시트에서 데이터를 읽는 방법을 보여주는 샘플 코드입니다.

코드를 짜도록 ChatGpt에 시켰습니다. 시키는 것도 일이네요..할일을 잘 쪼개서 물어봐야 합니다.

몇번의 시도에 아래와 같이 완성이 되었습니다.

다만 응용프로그램으로서 좋은지는 계속 확인해 봐야 합니다.


pip3 install --upgrade pip
pip isntall gsspread
pip install oauth2client


import openai
import gspread
from oauth2client.service_account import ServiceAccountCredentials


# Use your Google API credentials to authorize access to your Google Sheets account
scope = ['https://spreadsheets.google.com/feeds',
         'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name("/Users/dacon/Desktop/____  키______.json", scope)
gc = gspread.authorize(credentials)


# Open the desired spreadsheet and get the first worksheet
spreadsheet = gc.open('Chat GPT for DACON')
worksheet = spreadsheet.get_worksheet(0)
prompt2 = "Write a long articles in markdown format. and include subtitles and detail description, bullet points. The title of the blog is:"
## Keep track of the current row number
row_num = 1


# Continuously run the code until the user decides to stop
while True:
    # Read the first cell of the current row of the worksheet
    cell_value = quest + worksheet.cell(row_num, 1).value


    # If there is no text in the first cell, exit the program
    if not cell_value:
        break


    # Use the OpenAI API to ask ChatGPT a question with the text from the first cell
    openai.api_key = "____  키______"
    response = openai.Completion.create(
        engine="text-davinci-002",
        prompt=prompt2 + cell_value,
        max_tokens=3999,
        n=1,
        stop=None,
        temperature=0.7,
    )


    # Get the answer from OpenAI
    answer = response["choices"][0]["text"]


    # Record the answer in the next cell of the same line of the worksheet
    worksheet.update_cell(row_num, 2, answer)


    # Move to the next row
    row_num += 1



완성 마무리...


로그인이 필요합니다
0 / 1000
쥬혁이
2023.02.15 03:08

스프레드시트에 질문을 입력하면 알아서 답변이 채워지는 식인가요?

DACON.edu
2023.02.15 07:39

https://youtu.be/R2IxUDVM48g 이분 영상을 참고 했습니다. 구글시트로 직접 연결하다보면 장문 답변에서 에러가 많이 납니다.

쥬혁이
2023.02.15 09:39

좋은 정보 감사합니다! ChatGPT 원래 페이지에서 쓰는것보다 구글 시트에서 쓰면 어떤 장점이 있다 생각하시나요?

DACON.edu
2023.02.17 16:51

영업이나 기획 직군에서 노코드로 직관적으로 사용할 수 있을 것 같습니다.  
=GOOGLETRANSLATE 라는 함수를 사용해서 영어로 질문하고 한글로 번역하는데 좋은 것 같습니다.

쥬혁이
2023.02.15 10:54

감사합니다!

이오렉
2023.02.17 16:44

오! 구글시트에서 바로 불러오면 긴 내용이나 처리가 늦어지면 에러를 띄우는데 외부에서 계산해서 가져오는 방법이군요. 멋있어요. 따라 해볼게요

DACON.edu
2023.02.20 18:13

네 맞습니다.~