summary / app.py
randmimc's picture
Update app.py
ca9fe96 verified
# νŒ¨ν‚€μ§€ 뢈러였기
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)