3주차 과제 2 ChatGPT API 써보기

아래와 같이 제춣합니다 😊 감사합니다!


  • 선택한 블로그 주제와 이에 대한 간략한 설명

    • 우선 블로그의 주제를 고민 🤔 하던 중 ‘팝업스토어’가 10~30대 에게 인기가 있다는 기사 📰 를 다수 발견 할 수 있었습니다! 😁

  • 주제에 맞는 컨텐츠를 생성하는 Python 스크립트의 코드

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    import time
    import os 
    from openai import OpenAI
    
    # OpenAI API 키 설정
    client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
    
    def get_top_news_titles(search_term, max_titles):
        """_summary_
        네이버 뉴스에서 주어진 검색어에 대한 상위 뉴스 제목을 추출합니다.
        
        Args:
            search_term (_type_): 검색어
            max_titles (_type_): 추출할 최대 뉴스 제목 수
            return: 뉴스 제목 리스트
        """
        # Chrome WebDriver 설정 및 Selenium 서비스 시작
        chrome_driver_path = "/opt/homebrew/bin/chromedriver"
        service = Service(executable_path=chrome_driver_path)
        driver = webdriver.Chrome(service=service)
        
        # 네이버 검색 페이지로 이동
        driver.get(f"https://search.naver.com/search.naver?query={search_term}")
        
        # '뉴스' 탭 클릭
        news_tab_selector = "#lnb > div.lnb_group > div > div.lnb_nav_area._nav_area_root > div > div.api_flicking_wrap._conveyer_root > div:nth-child(3) > a"
        driver.find_element(By.CSS_SELECTOR, news_tab_selector).click()
        
        # 뉴스 제목을 저장할 리스트
        news_titles = []
        
        # 무한 스크롤 처리: 상위 100개의 뉴스 제목을 수집할 때까지 페이지 다운
        while len(news_titles) < max_titles:
            # 현재 페이지의 뉴스 제목들을 가져옴
            titles_elements = driver.find_elements(By.CSS_SELECTOR, "a.news_tit")
            for title_element in titles_elements:
                if len(news_titles) >= max_titles:
                    break
                news_titles.append(title_element.text)
            
            # 페이지의 맨 아래로 스크롤
            driver.find_element(By.TAG_NAME, 'body').send_keys(Keys.END)
            
            # 페이지 로드를 기다림
            time.sleep(1)
            
        driver.quit()
        return news_titles
    
    def find_popular_popup_store(news_titles):
        """
        OpenAI GPT를 사용하여 가장 많이 언급된 팝업스토어 이름을 찾습니다.
    
        Args:
            news_titles (_type_): 뉴스 제목 리스트
        return: 가장 많이 언급 된 팝업스토어 이름
        """
        # 뉴스 제목을 하나의 텍스트로 결합
        combined_titles = "\n".join(news_titles)
        
        # GPT에게 가장 많이 언급된 팝업스토어 이름을 물어봄
        chat_completion = client.chat.completions.create(
            model="gpt-4",   # GPT-4 모델 사용
            messages=[
                {"role": "system", "content": "You're a market analytics expert."},
                {"role": "user", "content": f"다음은 '팝업스토어'에 관한 뉴스 제목들이야. 가장 많이 언급 된 팝업스토어를 5개 제시해줘.\n{combined_titles}"}
            ]
        )
        return chat_completion.choices[0].message.content.strip()
    
    # '팝업스토어' 검색어로 네이버 뉴스에서 상위 100개 뉴스 제목 추출
    top_news_titles = get_top_news_titles("팝업스토어", 100)
    
    # OpenAI GPT를 사용하여 가장 많이 언급 된 팝업스토어 이름 찾기
    popular_popup_store = find_popular_popup_store(top_news_titles)
    
    # print(f"가장 많이 언급 된 팝업스토어: {popular_popup_store}")
    
    # 사용자로부터 팝업스토어 선택 받기
    def get_user_choice(popular_stores):
        """
        사용자에게 가장 많이 언급된 팝업스토어 중 하나를 선택하도록 요청하고 그 선택을 반환합니다.
    
        Args:
            popular_stores (list): 가장 많이 언급 된 팝업스토어 리스트
        """
        print("가장 많이 언급된 팝업스토어 5개는 다음과 같습니다.: ")
        for i, store in enumerate(popular_stores, start=1):
            print(f"{i}. {store}")
            
        choice = int(input("방문하고 싶은 팝업스토어 번호를 선택해주세요 (1-5): "))
        selected_store = popular_stores[choice - 1]
        return selected_store
    
    selected_popup_store = get_user_choice(popular_popup_store.split('\n')[2:7]) # 최상위 5개 팝업스토어 중 하나를 선택
    
    def request_story_about_store(store_name):
        """
        선택된 팝업스토어에 대한 재미있고 트렌디한 방문 이야기를 GPT-4.0을 사용해 작성하도록 요청합니다.
    
        Args:
            store_name (str): 사용자가 선택한 팝업스토어 이름
        Returns:
            str: GPT-4.0이 작성한 방문 이야기
        """
        prompt = f"""
        Bing 검색을 통해 '{store_name}' 팝업스토어에 대한 정보를 찾고,
        해당 팝업스토어를 방문한 재미있고 트렌디한 이야기를 담아 블로그를 마크다운 형식으로 작성해줘.
        다양한 정보를 담고 느낀점을 재미있게 표현한 내용을 작성해야 해.
        블로그는 네이버 SEO에 최적화되어야 하고 최소 2000자 이상으로 작성해줘야 해.
        """
        
        response = client.chat.completions.create(
            model="gpt-4",   # GPT-4 모델 사용
            messages=[
                {"role": "system", "content": "You are an AI trained to write creative and SEO-optimized stories about popup stores."},
                {"role": "user", "content": prompt}
            ]
        )
        return response.choices[0].message.content.strip()
    
    story_about_store = request_story_about_store(selected_popup_store)
    
    def save_to_md_file(content, filename):
        """
        주어진 내용을 마크다운 파일로 저장합니다.
    
        Args:
            content (str): 파일에 저장할 내용
            filename (str): 저장할 파일의 이름 (경로 포함)
        """
        # 사용자의 홈 디렉토리 경로를 얻습니다.
        home_dir = os.path.expanduser('~')  # 수정된 부분
        # 완전한 파일 경로를 구성합니다.
        full_path = os.path.join(home_dir, 'Downloads', filename)
        
        with open(full_path, 'w') as file:
            file.write(content)
        
    # 파일 이름에 사용할 수 없는 문자를 대체합니다.
    safe_store_name = selected_popup_store.replace(' ', '_').replace('/', '_')
    # 사용자의 'Downloads' 폴더에 결과물 저장
    save_to_md_file(story_about_store, f"{safe_store_name}_story.md")
  • 생성된 글을 기반으로 작성한 블로그 포스트의 링크

    • 블로그를 이번에 신규로 생성하려고 해서 이부분은 조금만 기다려주세요 🙂

2
2개의 답글

👉 이 게시글도 읽어보세요