File size: 5,026 Bytes
0967be1
 
44dcc41
b84a171
2a62aac
0967be1
91f6af8
9a3b8f0
0967be1
 
ca9fe96
44dcc41
0967be1
44dcc41
 
 
 
 
55218eb
 
0967be1
 
 
55218eb
 
0967be1
55218eb
 
0967be1
 
55218eb
0967be1
 
 
 
 
 
55218eb
0967be1
 
 
 
55218eb
 
0967be1
 
 
 
55218eb
0967be1
 
 
 
 
55218eb
 
 
 
0967be1
 
 
 
 
 
 
 
 
 
24ac80b
44dcc41
56f5bf5
0967be1
56f5bf5
0967be1
 
44dcc41
f701a00
0967be1
 
44dcc41
 
0967be1
 
91f6af8
0967be1
 
91f6af8
0967be1
44dcc41
f701a00
44dcc41
f701a00
44dcc41
f701a00
0967be1
 
91f6af8
 
 
 
0967be1
 
 
 
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# νŒ¨ν‚€μ§€ 뢈러였기
import re
import streamlit as st
from transformers import pipeline

# κΈ€λ‘œλ²Œ λ³€μˆ˜ μ„ μ–Έ
## λ³Έ μ½”λ“œμ—μ„œ μ‚¬μš©λœ 신문기사 μš”μ•½μš© λͺ¨λΈ(https://huggingface.co/gangyeolkim/kobart-korean-summarizer-v2)은 ν…ŒμŠ€νŠΈμš© μž…λ‹ˆλ‹€.
pipe = pipeline("summarization", model="gangyeolkim/kobart-korean-summarizer-v2")

## assitant 아바타 이미지 경둜
assistant_icon_path = "https://huggingface.co/spaces/randmimc/aitom/resolve/main/chat_icon/assistant.ico"

## μ„Έμ…˜ μƒνƒœμ—μ„œ approval_state 및 messages μ΄ˆκΈ°ν™”
if "approval_state" not in st.session_state:
    st.session_state.approval_state = "require"
if "messages" not in st.session_state:
    st.session_state.messages = []

st.markdown("""
    <style>
    .chat-container { /* μ±„νŒ… λ©”μ‹œμ§€λ₯Ό κ°μ‹ΈλŠ” μ»¨ν…Œμ΄λ„ˆλ₯Ό μ„€λͺ…ν•©λ‹ˆλ‹€. */
        display: flex; /* μ±„νŒ… μš”μ†Œ(아바타, 말풍선 λ“±)을 κ°€λ‘œ λ°©ν–₯으둜 μ •λ ¬ν•©λ‹ˆλ‹€. */
        margin-bottom: 10px; /*λ©”μ„Έμ§€κ°„ 간격을 10px둜 ν•©λ‹ˆλ‹€. */
    }
    .user-container {
        justify-content: flex-end; /* μ‚¬μš©μžμ˜ λ©”μ‹œμ§€λ₯Ό μš°μΈ‘μ •λ ¬ν•©λ‹ˆλ‹€.*/
    }
    .assistant-container {
        justify-content: flex-start; /* μ–΄μ‹œμŠ€ν„΄μŠ€ λ©”μ‹œμ§€λ₯Ό 쒌츑 μ •λ ¬ν•©λ‹ˆλ‹€.*/
        flex-direction: row; /* Avatar와 Bubble을 μˆ˜ν‰μœΌλ‘œ μ •λ ¬ν•©λ‹ˆλ‹€. */
    }
    .chat-bubble { /* 말풍선 μŠ€νƒ€μΌμ„ μ •μ˜ν•©λ‹ˆλ‹€. */
        padding: 10px; /* 말풍선 μ•ˆμ— λ©”μ‹œμ§€κ°„ 간격을 10px둜 ν•©λ‹ˆλ‹€. */
        border-radius: 10px; /*λ§ν’μ„ μ˜ λͺ¨μ„œλ¦¬λ₯Ό λ™κΈ€κ²Œ λ§Œλ“­λ‹ˆλ‹€.*/
        max-width: 70%; /* ν•œμ€„μ΄ λ„ˆλ¬΄ κΈΈμ§€ μ•Šκ²Œ μ œν•œν•©λ‹ˆλ‹€. */
        word-wrap: break-word; /* ν…μŠ€νŠΈλ₯Ό 단어 λ‹¨μœ„λ‘œ μ€„λ‚΄λ¦Όν•©λ‹ˆλ‹€. */
        white-space: pre-wrap; /* ν…μŠ€νŠΈλ₯Ό 단어 λ‹¨μœ„λ‘œ μ€„λ‚΄λ¦Όν•©λ‹ˆλ‹€. */
    }
    .user-bubble { /* μ‚¬μš©μž λ©”μ‹œμ§€ μŠ€νƒ€μΌμ„ μ •μ˜ν•©λ‹ˆλ‹€. */
        font-size: 14px; /* 폰트 μ‚¬μ΄μ¦ˆλ₯Ό μ§€μ •ν•©λ‹ˆλ‹€. */
        color: #333;  /* 폰트 색상을 μ§€μ •ν•©λ‹ˆλ‹€. */
        background-color: #e5e5e5; /* 말풍선 배경색을 μ§€μ •ν•©λ‹ˆλ‹€. */
    }
    .assistant-bubble {
        font-size: 14px; /* 폰트 μ‚¬μ΄μ¦ˆλ₯Ό μ§€μ •ν•©λ‹ˆλ‹€. */
        color: #333;  /* 폰트 색상을 μ§€μ •ν•©λ‹ˆλ‹€. */
        text-align: left; /* λ©”μ„Έμ§€λ₯Ό μ™Όμͺ½ μ •λ ¬ ν•©λ‹ˆλ‹€. */
        background-color: #feeddf; /* 말풍선 배경색을 μ§€μ •ν•©λ‹ˆλ‹€. */
    }
    .avatar { /* ν”„λ‘œν•„ 이미지(아바타) μŠ€νƒ€μΌμ„ μ§€μ •ν•©λ‹ˆλ‹€. */
        width: 30px; /* 아바타 폭을 μ§€μ •ν•©λ‹ˆλ‹€. */
        height: 30px; /* 아바타 λ„ˆμ„ μ§€μ •ν•©λ‹ˆλ‹€. */
        border-radius: 50%; /* 아바타λ₯Ό μ›κ²½μœΌλ‘œ λ§Œλ“­λ‹ˆλ‹€. */
        margin-right: 10px; /*λ©”μ„Έμ§€κ°„ 간격을 10px둜 ν•©λ‹ˆλ‹€. */
    }
    </style>
""", unsafe_allow_html=True)

def user_message_style(question):
    return f"""<div class="chat-container user-container">
        <div class="chat-bubble user-bubble">{question}</div>
    </div>"""

def assistant_message_style(assistant_icon_path, answer):
    return f"""<div class="chat-container assistant-container">
        <img src="{assistant_icon_path}" class="avatar">
        <div class="chat-bubble assistant-bubble">{answer}</div>
    </div>"""

for message in st.session_state.messages:
    if message["role"] == "user":
        st.markdown(user_message_style(message["content"]), unsafe_allow_html=True)
    else:
        st.markdown(assistant_message_style(assistant_icon_path, message["content"]), unsafe_allow_html=True)

if not st.session_state.messages:
    greeting = '신문기사 μš”μ•½ λͺ¨λΈ μž…λ‹ˆλ‹€. μ‚¬μš©μ„ μœ„ν•΄μ„œλŠ” "yes"λ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”'
    st.session_state.messages.append({"role": "assistant", "content": greeting})
    st.markdown(assistant_message_style(assistant_icon_path, greeting), unsafe_allow_html=True)

if prompt := st.chat_input():
    # μ‚¬μš©μž 질문 μ„Έμ…˜μ— μ €μž₯
    st.session_state.messages.append({"role": "user", "content": prompt})
    
    # μ‚¬μš©μž 질문 화면에 ν‘œμ‹œ
    st.markdown(user_message_style(prompt), unsafe_allow_html=True)
    
    # 인증 
    if st.session_state.approval_state == "require":
        if prompt.lower() == "yes":
            st.session_state.approval_state = "approved"
            response = "이제 μ‚¬μš©μ΄ κ°€λŠ₯ν•©λ‹ˆλ‹€. 단, λͺ¨λΈ 크기가 μž‘μ€ λͺ¨λΈμ΄μ—¬μ„œ μ„±λŠ₯이 κΈ°λŒ€μ— λͺ»λ―ΈμΉ  수 μžˆμŠ΅λ‹ˆλ‹€."
        else:
            response = "인증에 μ‹€νŒ¨ν•˜μ˜€μŠ΅λ‹ˆλ‹€. λ‹€μ‹œ yesλ₯Ό μž…λ ₯ν•΄ μ£Όμ„Έμš”"
    # 응닡 생성
    else:
        result = re.sub(r'\s+', ' ', prompt)
        summarized = pipe(result)
        response = summarized[0]["summary_text"]
    
    # μ–΄μ‹œμŠ€ν„΄μŠ€μ˜ λ©”μ„Έμ§€λ₯Ό μ„Έμ…˜μ— μ €μž₯
    st.session_state.messages.append({"role": "assistant", "content": response})
    # μ–΄μ‹œμŠ€ν„΄μŠ€μ˜ λ©”μ„Έμ§€λ₯Ό 화면에 ν‘œμ‹œ
    st.markdown(assistant_message_style(assistant_icon_path, response), unsafe_allow_html=True)