Spaces:
Runtime error
Runtime error
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)
|