• Complete!!

    Alan04/20/2023 at 15:04 0 comments

    Hello everyone! I would like to share an interesting project that I've been working on!

    This is almost everything I saw in the thumbnail. I will briefly explain the project.

    My current project is very similar to the one I created before. Therefore, please refer to my previous project for more detailed information.

    Chat GPT API + Youtube Comment + Weather

    Here's a simple flowchart! I only changed the LCD configuration from my previous project. I will bring some parts that I used in my previous project, such as web crawling and Chat GPT, albeit in a simplified manner.

    Web Crawling

    First of all, it's weather information, and I want to read it in a fun way as the weather caster tells me, not just weather information. So I decided to read it using the GPT API. GPT does not currently support real-time data services. So I decided to read it on Naver as WebCrawling.It started implementing in a Python environment that is relatively light and easy to implement.

    from bs4 import BeautifulSoup
    def getweather() :
        html = requests.get('http://search.naver.com/search.naver?query=수내+날씨')
        soup = BeautifulSoup(html.text, 'html.parser')
        global weather
        weather = ''
    
        address = "Bundang Sunae"
        weather += address + '*'
    
        weather_data = soup.find('div', {'class': 'weather_info'})
    
        # Current Temperature
        temperature = (str(weather_data.find('div', {'class': 'temperature_text'}).text.strip()[5:])[:-1])
        weather += temperature + '*'
    
        # Weather Status
        weatherStatus = weather_data.find('span', {'class': 'weather before_slash'}).text
    
        if weatherStatus == '맑음':
            weatherPrint = 'Sunny'
        elif '흐림':
            weatherPrint = 'Cloud'
    
        weather += weatherPrint

     WebCrawling can be done simply with a package called BeautifulSoup.

    <Screen that appears when searching for weather in the water on the Naver site>

    Let me deal with Crawling briefly.

    In the script, temperature was read from temperature_text.

    And weather was read in the span class of weather before_slash.

    I want to read it in English, but I couldn't find it, so I just hardcoded it...And when you run this, it will look like the following.

    It outputs temperature and weather like this.

    Chat GPT

    I will convert it to Chat GPT and get the data.Chat GPT 3.5 API was officially released recently, making it easy to implement.Chat GPT requires API Key just like YouTube.

    def Chat_GPT(api_key, query):
        global answer
    
        openai.api_key = api_key
    
        model = "gpt-3.5-turbo"
    
        messages = [
            {
                "role": "system",
                "content": "You are a very creative and interesting writer."
            },
            {
                "role": "user",
                "content": query
            }
        ]
    
        response = openai.ChatCompletion.create(
            model=model,
            messages=messages
        )
        answer = response['choices'][0]['message']['content']
        print(answer)

    I assigned the following role to Chat GPT and wrote a prompt accordingly.

    query = "The current weather is very {} with {} degrees. Create a creative story in this situation. No more than two sentences.".format(weather,temperature)
    print(query)
    
    Chat_GPT(api_key,query)

    And in the main code, I send the information obtained from web crawling to GPT by inputting it into the prompt.

    Dall-E-2

    DALL-E 2 is an image generation tool created by OpenAI.

    def dall_e2(answer):
        global image_url
        global image_data
        response = openai.Image.create(
            prompt=answer,
            n=1,
            size="256x256",
        )
        image_url = response['data'][0]['url']
        urllib.request.urlretrieve(image_url, "test.bmp")
    
        im = Image.open("test.bmp")
        im_resized = im.resize((220, 220))
    
        im_resized = im_resized.convert('P', palette=Image.ADAPTIVE, colors=16)
    
        im_resized.save("test_resized.bmp", format="BMP")
        with open("test_resized.bmp", "rb") as f:
            image_data = f.read()
        print(image_data)

    The code for DALL-E 2 is structured as follows. One thing to note here is the Image size section. Since I don't have separate memory, I will save the image in BMP format to the Pico board's Flash and display it later.

    Anyway, when sending and receiving...

    Read more »