Notice
Recent Posts
Recent Comments
ยซ   2024/11   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Archives
Today
In Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

A Joyful AI Research Journey๐ŸŒณ๐Ÿ˜Š

[8] 241108 Team Project Results: Stock Headline Sentiment Analysis [Goorm All-In-One Pass! AI Project Master 4๊ธฐ 8์ผ] ๋ณธ๋ฌธ

๐ŸŒณAI Project Mastery Bootcamp 2024โœจ/Project

[8] 241108 Team Project Results: Stock Headline Sentiment Analysis [Goorm All-In-One Pass! AI Project Master 4๊ธฐ 8์ผ]

yjyuwisely 2024. 11. 8. 10:27

241108 Fri 8th class

๋…ธ์…˜ ๋ฐœํ‘œ ์ž๋ฃŒ ๋งŒ๋“ค์—ˆ๋‹ค!

https://acoustic-hexagon-e6d.notion.site/Streamlit-1361e9b916c3808c8d37e3bf079ea6df

 

์ฃผ์‹ ์ •๋ณด ์ œ๊ณต ๋ฐ ์‹œ๊ฐํ™”๋ฅผ ์œ„ํ•œ Streamlit ํ™œ์šฉ | Notion

Joyeux (์ฃผ์•„์œ , ํ”„๋ž‘์Šค์–ด: ์œ ์พŒํ•œ, ํ†ต์พŒํ•œ) ํŒ€ ํ”„๋กœ์ ํŠธ (๋ฐœํ‘œ: 2024.11.08)

acoustic-hexagon-e6d.notion.site

์•„์ง URL, ์Šคํฌ๋ฆฐ์ƒท์€ ์—†๋‹ค.


๊ฐ์„ฑ ๋ถ„์„ Streamlit ๋ฒ„์ „์„ ๋งŒ๋“ค์—ˆ๋‹ค

๋‰ด์Šค ๊ฐ์ • ๋ถ„์„ ๋ฐ ์ด๋ฒคํŠธ ์˜ํ–ฅ ํ‘œ์‹œ

  • ๋‰ด์Šค ํ—ค๋“œ๋ผ์ธ ๊ฐ์ • ๋ถ„์„: ์ตœ๊ทผ ์ฃผ์‹๊ณผ ๊ด€๋ จ๋œ ๋‰ด์Šค ๊ธฐ์‚ฌ๋‚˜ ์†Œ์…œ ๋ฏธ๋””์–ด ๋ฐ์ดํ„ฐ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ, ๊ฐ์ • ๋ถ„์„์„ ํ†ตํ•ด ๊ธ์ •์ , ๋ถ€์ •์ , ์ค‘๋ฆฝ์ ์ธ ์˜๊ฒฌ์„ ๋ถ„๋ฅ˜ํ•  ์ˆ˜ ์žˆ์–ด์š”. ์ด๋ฅผ ํ†ตํ•ด ์‚ฌ์šฉ์ž์—๊ฒŒ ์ตœ๊ทผ ๋‰ด์Šค๊ฐ€ ์ฃผ์‹์— ์–ด๋–ค ์˜ํ–ฅ์„ ๋ฏธ์น˜๋Š”์ง€ ๋ณด์—ฌ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฐฐํฌํ•œ ์›น์‚ฌ์ดํŠธ

https://stock-sentiment-app.streamlit.app/


์ฝ”๋“œ)

app.py

import streamlit as st
import requests
from bs4 import BeautifulSoup
from transformers import pipeline
import time

def get_stock_buy_recommendation(stock_name):
    """
    Fetch news headlines and provide stock buy recommendations
    """
    # Add a spinner while processing
    with st.spinner(f'๋ถ„์„ ์ค‘... {stock_name}์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.'):
        url = f'https://search.naver.com/search.naver?ie=utf8&sm=nws_hty&query={stock_name}'
        response = requests.get(url)
        soup = BeautifulSoup(response.content, 'html.parser')

        # Extract headlines
        headlines = [headline.get_text() for headline in soup.find_all('a', class_='news_tit')[:10]]

        # Initialize sentiment analyzer
        sentiment_analyzer = pipeline('sentiment-analysis')

        # Analyze sentiments
        results = []
        positive_count = 0
        negative_count = 0
        
        for headline in headlines:
            sentiment = sentiment_analyzer(headline)[0]['label']
            
            if sentiment == 'POSITIVE':
                sentiment_emoji = '๐Ÿ˜Š'
                positive_count += 1
            elif sentiment == 'NEGATIVE':
                sentiment_emoji = '๐Ÿ˜ฐ'
                negative_count += 1
            else:
                sentiment_emoji = '๐Ÿ˜'
                
            results.append((headline, sentiment_emoji))

        # Determine recommendation
        if positive_count > negative_count:
            buy_recommendation = f'{stock_name}์„(๋ฅผ) ๋งค์ˆ˜ํ•˜์„ธ์š” ๐Ÿ˜Š'
        elif positive_count < negative_count:
            buy_recommendation = f'{stock_name}์„(๋ฅผ) ๋งค์ˆ˜ํ•˜์ง€ ๋งˆ์„ธ์š” ๐Ÿ˜ฐ'
        else:
            buy_recommendation = f'{stock_name}์— ๋Œ€ํ•ด ์ค‘๋ฆฝ์ ์ธ ์ž…์žฅ์ž…๋‹ˆ๋‹ค ๐Ÿ˜'

        return results, buy_recommendation

# Set up the Streamlit page
st.set_page_config(page_title="์ฃผ์‹ ํˆฌ์ž ์˜๊ฒฌ ๋ถ„์„๊ธฐ", page_icon="๐Ÿ“ˆ")

# Add title and description
st.title("๐Ÿ“ˆ ์ฃผ์‹ ํˆฌ์ž ์˜๊ฒฌ ๋ถ„์„๊ธฐ")
st.markdown("""
์ด ์•ฑ์€ ๋„ค์ด๋ฒ„ ๋‰ด์Šค ํ—ค๋“œ๋ผ์ธ์„ ๋ถ„์„ํ•˜์—ฌ ์ฃผ์‹ ํˆฌ์ž ์˜๊ฒฌ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.
""")

# Create input field
stock_name = st.text_input("์ข…๋ชฉ๋ช…์„ ์ž…๋ ฅํ•˜์„ธ์š”:", placeholder="์˜ˆ: ์‚ผ์„ฑ์ „์ž")

# Create analyze button
if st.button("ํˆฌ์ž์˜๊ฒฌ ๋ถ„์„"):
    if stock_name:
        try:
            # Get recommendation
            headline_results, buy_recommendation = get_stock_buy_recommendation(stock_name)
            
            # Display results
            st.subheader(f"{stock_name}์— ๋Œ€ํ•œ ๋ถ„์„ ๊ฒฐ๊ณผ")
            
            # Display buy recommendation in a highlighted box
            st.info(buy_recommendation)
            
            # Display headlines in an expandable section
            with st.expander("๋‰ด์Šค ํ—ค๋“œ๋ผ์ธ ์ƒ์„ธ ๋ถ„์„ ๋ณด๊ธฐ"):
                for headline, emoji in headline_results:
                    st.write(f"{emoji} {headline}")
                    
        except Exception as e:
            st.error(f"์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค: {str(e)}")
    else:
        st.warning("์ข…๋ชฉ๋ช…์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")

# Add footer
st.markdown("---")
st.markdown("Made with โค๏ธ using Streamlit")

requirement.txt

streamlit
requests
beautifulsoup4
transformers
torch
cd stock_analyzer
pip install -r requirements.txt
streamlit run app.py

  You can now view your Streamlit app in your browser.

  Local URL: http://localhost:8501

๊ฒฐ๊ณผ)


  • ๊ฐ์„ฑ ๋ถ„์„
    • ์‚ฌ์šฉํ•œ ๋ชจ๋ธ: DistilBERT ๊ธฐ๋ฐ˜ ๊ฐ์ • ๋ถ„์„ ๋ชจ๋ธ
      • DistilBERT๋Š” BERT์˜ ๊ฒฝ๋Ÿ‰ํ™” ๋ฒ„์ „์œผ๋กœ, ์•ฝ 60% ๋” ์ž‘๊ณ  2๋ฐฐ ๋น ๋ฅธ ์ฒ˜๋ฆฌ ์†๋„๋ฅผ ์ž๋ž‘ํ•˜๋ฉฐ, ์„ฑ๋Šฅ์€ ์›๋ž˜ BERT์˜ 97% ์ˆ˜์ค€์„ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.
      • SST-2 ๋ฐ์ดํ„ฐ์…‹(Stanford Sentiment Treebank 2)์œผ๋กœ ๋ฏธ์„ธ ์กฐ์ •๋œ ๊ฐ์ • ๋ถ„์„ ๋ชจ๋ธ๋กœ, **๊ธ์ •(positive)**๊ณผ ๋ถ€์ •(negative) ๊ฐ์ • ๋ถ„๋ฅ˜๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
      • "base-uncased":
        • base: BERT์˜ ๊ธฐ๋ณธ ํฌ๊ธฐ์ด๋ฉฐ, 12๊ฐœ์˜ Transformer ๋ ˆ์ด์–ด์™€ 110M๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ.
        • uncased: ์ž…๋ ฅ ํ…์ŠคํŠธ์—์„œ ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š์Œ.
      • ์ฃผ์š” ํŠน์ง•:
        • ์˜ํ™” ๋ฆฌ๋ทฐ, ์†Œ์…œ ๋ฏธ๋””์–ด ๊ฒŒ์‹œ๋ฌผ ๋“ฑ์—์„œ ๋น ๋ฅด๊ณ  ํšจ์œจ์ ์ธ ๊ฐ์ • ๋ถ„์„์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ.
        • ์ง€์‹ ์ฆ๋ฅ˜(Knowledge Distillation) ๊ธฐ๋ฒ•์„ ํ†ตํ•ด ๋ชจ๋ธ ํฌ๊ธฐ๋ฅผ ์ค„์ด๋ฉด์„œ๋„ ์„ฑ๋Šฅ์„ ์œ ์ง€ํ•˜์—ฌ, ๋ชจ๋ฐ”์ผ ๋ฐ ์›น ์„œ๋น„์Šค์™€ ๊ฐ™์€ ์ปดํ“จํŒ… ์ž์›์ด ์ œํ•œ๋œ ํ™˜๊ฒฝ์—์„œ๋„ ํ™œ์šฉ ๊ฐ€๋Šฅ.
    • ๋ชจ๋ธ ์ด๋ฆ„: distilbert-base-uncased-finetuned-sst-2-english
    • ํ•œ๊ณ„: ํ•œ๊ตญ์–ด ๋ชจ๋ธ(snunlp/KR-FinBert)์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ ๋ชจ๋“  ๊ฒฐ๊ณผ๊ฐ€ ์ค‘๋ฆฝ(Neutral)์œผ๋กœ ๋‚˜์™”์œผ๋ฉฐ, ์˜์–ด ๋ชจ๋ธ๋ณด๋‹ค ์„ฑ๋Šฅ์ด ๋–จ์–ด์ ธ์„œ DistilBERT ๊ธฐ๋ฐ˜ ๊ฐ์ • ๋ถ„์„ ๋ชจ๋ธ ์‚ฌ์šฉ**.**

 

728x90
๋ฐ˜์‘ํ˜•
Comments