Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,108 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import re
|
| 4 |
|
| 5 |
-
def replace_multiple_spaces(text):
|
| 6 |
-
return re.sub(r'\s+', ' ', text)
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
assistant_icon_path = "https://huggingface.co/spaces/randmimc/aitom/resolve/main/chat_icon/assistant.ico"
|
| 12 |
-
user_icon_path = "https://huggingface.co/spaces/randmimc/aitom/resolve/main/chat_icon/user01.png"
|
| 13 |
|
| 14 |
-
# ์ธ์
์ํ์์ approval_state ๋ฐ messages ์ด๊ธฐํ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
if "approval_state" not in st.session_state:
|
| 16 |
st.session_state.approval_state = "require"
|
| 17 |
if "messages" not in st.session_state:
|
| 18 |
st.session_state.messages = []
|
| 19 |
|
| 20 |
-
#
|
|
|
|
|
|
|
| 21 |
st.markdown("""
|
| 22 |
<style>
|
| 23 |
-
.chat-container {
|
| 24 |
-
display: flex;
|
| 25 |
-
|
| 26 |
-
margin-bottom: 10px;
|
| 27 |
}
|
| 28 |
.user-container {
|
| 29 |
-
justify-content: flex-end;
|
| 30 |
}
|
| 31 |
.assistant-container {
|
| 32 |
-
justify-content: flex-start;
|
| 33 |
-
flex-direction: row; /* Avatar์ Bubble์ ์ํ ์ ๋ ฌ */
|
| 34 |
}
|
| 35 |
-
.chat-bubble {
|
| 36 |
-
padding: 10px;
|
| 37 |
-
border-radius: 10px;
|
| 38 |
-
max-width: 70%;
|
| 39 |
-
word-wrap: break-word;
|
| 40 |
-
white-space: pre-wrap;
|
| 41 |
}
|
| 42 |
-
.user-bubble {
|
| 43 |
-
font-size: 14px;
|
| 44 |
-
color: #333;
|
| 45 |
-
background-color: #e5e5e5;
|
| 46 |
-
text-align: left;
|
| 47 |
}
|
| 48 |
.assistant-bubble {
|
| 49 |
-
font-size: 14px;
|
| 50 |
-
color: #333;
|
| 51 |
-
text-align: left; /* ์ผ์ชฝ ์ ๋ ฌ */
|
| 52 |
-
background-color: #feeddf;
|
| 53 |
}
|
| 54 |
-
.avatar {
|
| 55 |
-
width: 30px;
|
| 56 |
-
height: 30px;
|
| 57 |
-
border-radius: 50%;
|
| 58 |
-
margin-right: 10px; /*
|
| 59 |
}
|
| 60 |
</style>
|
| 61 |
""", unsafe_allow_html=True)
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
#
|
|
|
|
|
|
|
|
|
|
| 65 |
for message in st.session_state.messages:
|
| 66 |
if message["role"] == "user":
|
| 67 |
-
st.markdown(
|
| 68 |
-
f"""
|
| 69 |
-
<div class="chat-container user-container">
|
| 70 |
-
<div class="chat-bubble user-bubble">{message["content"]}</div>
|
| 71 |
-
</div>
|
| 72 |
-
""", unsafe_allow_html=True)
|
| 73 |
else:
|
| 74 |
-
st.markdown(
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# Start with an assistant message
|
| 83 |
if not st.session_state.messages:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
"content": """๋์์ด์ ์จ์ aitom์ ํ์ฌ ๊ณต์ฌ์ค์ ์์ต๋๋ค.
|
| 88 |
-
ํ์ง๋ง ! ๊ฐ๋จํ ํ์ฉํด ๋ณด์ค ์ ์๋ ์์ฝ ๋ชจ๋ธ์ ์ ๊ณตํ๊ณ ์์ต๋๋ค.
|
| 89 |
-
์งง์ ์ ๋ฌธ๊ธฐ์ฌ๋ฅผ ๋งํด ์ฃผ์๋ฉด ์์ฝ๋ฌธ์ ์ ๊ณตํฉ๋๋ค.
|
| 90 |
-
๊ณ์ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด "yes" ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.
|
| 91 |
-
"""})
|
| 92 |
-
st.rerun()
|
| 93 |
|
|
|
|
|
|
|
| 94 |
if prompt := st.chat_input():
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
if st.session_state.approval_state == "require":
|
| 97 |
if prompt.lower() == "yes":
|
| 98 |
st.session_state.approval_state = "approved"
|
| 99 |
-
|
| 100 |
else:
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ํจํค์ง ๋ถ๋ฌ์ค๊ธฐ
|
| 2 |
+
import re
|
| 3 |
+
import requests
|
| 4 |
import streamlit as st
|
| 5 |
from transformers import pipeline
|
|
|
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
url = "http://llm.theimc.co.kr:8003/item_post"
|
| 9 |
+
headers = {"Content-Type": "application/x-www-form-urlencoded"}
|
| 10 |
+
|
| 11 |
|
| 12 |
|
| 13 |
+
# ๊ธ๋ก๋ฒ ๋ณ์ ์ ์ธ
|
| 14 |
+
## ๋ชจ๋ธ ๋ถ๋ฌ์ค๊ธฐ(https://huggingface.co/gangyeolkim/kobart-korean-summarizer-v2) ๋ ํ
์คํธ์ฉ ๋ชจ๋ธ ์
๋๋ค.
|
| 15 |
+
pipe = pipeline("summarization", model="gangyeolkim/kobart-korean-summarizer-v2")
|
| 16 |
+
|
| 17 |
+
## assitant ์๋ฐํ ์ด๋ฏธ์ง ๊ฒฝ๋ก
|
| 18 |
assistant_icon_path = "https://huggingface.co/spaces/randmimc/aitom/resolve/main/chat_icon/assistant.ico"
|
|
|
|
| 19 |
|
| 20 |
+
## ์ธ์
์ํ์์ approval_state ๋ฐ messages ์ด๊ธฐํ
|
| 21 |
+
## โป ์ธ์
(session) :
|
| 22 |
+
## ์ฌ์ฉ์์ ์ธํฐ๋์
์ ๊ด๋ฆฌํ๋ ๊ธฐ๋ฅ์
๋๋ค.
|
| 23 |
+
## Streamlit์ ๊ธฐ๋ณธ์ ์ผ๋ก ์ฌ์ฉ์๊ฐ ์ฑ๊ณผ ์ํธ์์ฉํ ๋๋ง๋ค ์ ์ฒด ์ฝ๋๊ฐ ๋ค์ ์คํ๋๋ ๊ตฌ์กฐ์ธ๋ฐ,
|
| 24 |
+
## ์ธ์
์ํ(Session State)๋ฅผ ํ์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ์ ์งํ๋ฉด์๋ ํจ์จ์ ์ธ ์ฑ์ ๋ง๋ค ์ ์์ต๋๋ค.
|
| 25 |
if "approval_state" not in st.session_state:
|
| 26 |
st.session_state.approval_state = "require"
|
| 27 |
if "messages" not in st.session_state:
|
| 28 |
st.session_state.messages = []
|
| 29 |
|
| 30 |
+
# CSS ์์ฑ
|
| 31 |
+
## st.markdown๋ ํ
์คํธ๋ฅผ ๋งํฌ๋ค์ด ํ์์ผ๋ก ๋๋๋ง ํ๋ ํจ์์ด์ง๋ง, unsafe_allow_html=True ์ต์
์ ์ฌ์ฉํ๋ฉด HTML/CSS๋ฅผ ์ง์ ์ ์ฉํ ์ ์์ต๋๋ค.
|
| 32 |
+
## ์คํธ๋ฆผ๋ฆฟ์์ ๋ํดํธ๋ก ์ ์ฉํ๋ ๋์์ธ์ด ๋ง์ ๋ค์ง ์๋๋ค๋ฉด CSS๋ฅผ ์์ฑํ์ฌ ๋ณ๊ฒฝํ ์ ์์ต๋๋ค.
|
| 33 |
st.markdown("""
|
| 34 |
<style>
|
| 35 |
+
.chat-container { /* ์ฑํ
๋ฉ์์ง๋ฅผ ๊ฐ์ธ๋ ์ปจํ
์ด๋๋ฅผ ์ค๋ช
ํฉ๋๋ค. */
|
| 36 |
+
display: flex; /* ์ฑํ
์์(์๋ฐํ, ๋งํ์ ๋ฑ)์ ๊ฐ๋ก ๋ฐฉํฅ์ผ๋ก ์ ๋ ฌํฉ๋๋ค. */
|
| 37 |
+
margin-bottom: 10px; /*๋ฉ์ธ์ง๊ฐ ๊ฐ๊ฒฉ์ 10px๋ก ํฉ๋๋ค. */
|
|
|
|
| 38 |
}
|
| 39 |
.user-container {
|
| 40 |
+
justify-content: flex-end; /* ์ฌ์ฉ์์ ๋ฉ์์ง๋ฅผ ์ฐ์ธก์ ๋ ฌํฉ๋๋ค.*/
|
| 41 |
}
|
| 42 |
.assistant-container {
|
| 43 |
+
justify-content: flex-start; /* ์ด์์คํด์ค ๋ฉ์์ง๋ฅผ ์ข์ธก ์ ๋ ฌํฉ๋๋ค.*/
|
| 44 |
+
flex-direction: row; /* Avatar์ Bubble์ ์ํ์ผ๋ก ์ ๋ ฌํฉ๋๋ค. */
|
| 45 |
}
|
| 46 |
+
.chat-bubble { /* ๋งํ์ ์คํ์ผ์ ์ ์ํฉ๋๋ค. */
|
| 47 |
+
padding: 10px; /* ๋งํ์ ์์ ๋ฉ์์ง๊ฐ ๊ฐ๊ฒฉ์ 10px๋ก ํฉ๋๋ค. */
|
| 48 |
+
border-radius: 10px; /*๋งํ์ ์ ๋ชจ์๋ฆฌ๋ฅผ ๋๊ธ๊ฒ ๋ง๋ญ๋๋ค.*/
|
| 49 |
+
max-width: 70%; /* ํ์ค์ด ๋๋ฌด ๊ธธ์ง ์๊ฒ ์ ํํฉ๋๋ค. */
|
| 50 |
+
word-wrap: break-word; /* ํ
์คํธ๋ฅผ ๋จ์ด ๋จ์๋ก ์ค๋ด๋ฆผํฉ๋๋ค. */
|
| 51 |
+
white-space: pre-wrap; /* ํ
์คํธ๋ฅผ ๋จ์ด ๋จ์๋ก ์ค๋ด๋ฆผํฉ๋๋ค. */
|
| 52 |
}
|
| 53 |
+
.user-bubble { /* ์ฌ์ฉ์ ๋ฉ์์ง ์คํ์ผ์ ์ ์ํฉ๋๋ค. */
|
| 54 |
+
font-size: 14px; /* ํฐํธ ์ฌ์ด์ฆ๋ฅผ ์ง์ ํฉ๋๋ค. */
|
| 55 |
+
color: #333; /* ํฐํธ ์์์ ์ง์ ํฉ๋๋ค. */
|
| 56 |
+
background-color: #e5e5e5; /* ๋งํ์ ๋ฐฐ๊ฒฝ์์ ์ง์ ํฉ๋๋ค. */
|
|
|
|
| 57 |
}
|
| 58 |
.assistant-bubble {
|
| 59 |
+
font-size: 14px; /* ํฐํธ ์ฌ์ด์ฆ๋ฅผ ์ง์ ํฉ๋๋ค. */
|
| 60 |
+
color: #333; /* ํฐํธ ์์์ ์ง์ ํฉ๋๋ค. */
|
| 61 |
+
text-align: left; /* ๋ฉ์ธ์ง๋ฅผ ์ผ์ชฝ ์ ๋ ฌ ํฉ๋๋ค. */
|
| 62 |
+
background-color: #feeddf; /* ๋งํ์ ๋ฐฐ๊ฒฝ์์ ์ง์ ํฉ๋๋ค. */
|
| 63 |
}
|
| 64 |
+
.avatar { /* ํ๋กํ ์ด๋ฏธ์ง(์๋ฐํ) ์คํ์ผ์ ์ง์ ํฉ๋๋ค. */
|
| 65 |
+
width: 30px; /* ์๋ฐํ ํญ์ ์ง์ ํฉ๋๋ค. */
|
| 66 |
+
height: 30px; /* ์๋ฐํ ๋์ ์ง์ ํฉ๋๋ค. */
|
| 67 |
+
border-radius: 50%; /* ์๋ฐํ๋ฅผ ์๊ฒฝ์ผ๋ก ๋ง๋ญ๋๋ค. */
|
| 68 |
+
margin-right: 10px; /*๋ฉ์ธ์ง๊ฐ ๊ฐ๊ฒฉ์ 10px๋ก ํฉ๋๋ค. */
|
| 69 |
}
|
| 70 |
</style>
|
| 71 |
""", unsafe_allow_html=True)
|
| 72 |
|
| 73 |
+
# ๋ฉ์ธ์ง wrapper
|
| 74 |
+
# html ํ
๊ทธ๋ฅผ ์ด์ฉํ์ฌ ์ฑํ
๋งค์ธ์ง๋ง๋ค css ์คํ์ผ์ ์ ์ฉํ๊ธฐ ์ํด ์์ฑ๋ ํจ์์
๋๋ค.
|
| 75 |
+
def user_message_style(question):
|
| 76 |
+
return f"""<div class="chat-container user-container">
|
| 77 |
+
<div class="chat-bubble user-bubble">{question}</div>
|
| 78 |
+
</div>"""
|
| 79 |
+
|
| 80 |
+
def assistant_message_style(assistant_icon_path, answer):
|
| 81 |
+
return f"""<div class="chat-container assistant-container">
|
| 82 |
+
<img src="{assistant_icon_path}" class="avatar">
|
| 83 |
+
<div class="chat-bubble assistant-bubble">{answer}</div>
|
| 84 |
+
</div>"""
|
| 85 |
|
| 86 |
+
# BACK
|
| 87 |
+
## ๊ณผ๊ฑฐ ์ฑํ
๋ด์ฉ ์ถ๋ ฅ
|
| 88 |
+
## ์คํธ๋ฆผ๋ฆฟ์ st.chat_input() ๋ฉ์๋๋ ์ฌ์ฉ์๊ฐ ์
๋ ฅ์ ์ฃผ๋ฉด ์ธ์
๋ด์ฉ์ ๊ธฐ์ตํ๋ฉด์ ์ฝ๋๋ฅผ ๋ค์ ์คํํ๋ ํน์ฑ์ด ์์ต๋๋ค.
|
| 89 |
+
## ๋ฐ๋ผ์ ์ธ์
์ ์ ์ฅ๋ ๊ณผ๊ฑฐ ๋ํ ๋ด์ฉ์ ํ๋ฉด์ ์ถ๋ ฅํ๊ธฐ ์ํด์ ์๋ก ์คํ๋ ์ฝ๋์ ๊ณผ๊ฑฐ ๋ด์ฉ์ ๋ฏธ๋ฆฌ ๋ฟ๋ ค๋ ์ ์๊ฒ ์๋์ ๊ฐ์ ์ฝ๋๋ฅผ ์์ฑํด ๋์ด์ผ ํฉ๋๋ค.
|
| 90 |
for message in st.session_state.messages:
|
| 91 |
if message["role"] == "user":
|
| 92 |
+
st.markdown(user_message_style(message["content"]), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
else:
|
| 94 |
+
st.markdown(assistant_message_style(assistant_icon_path, message["content"]), unsafe_allow_html=True)
|
| 95 |
+
|
| 96 |
+
# ์ธ์ฆ ๋ฉ์ธ์ง
|
| 97 |
+
## ๋ณธ ์ฝ๋๋ ๋ค๋ฅธ ์ฑ๋ด๊ณผ ๋ฌ๋ฆฌ ์ธ์ฆ์ ์ํด ์ฑ๋ด์ด ๋จผ์ ์ฌ์ฉ์์๊ฒ ๋ง์ ๊ฑฐ๋ ์ฃผ๊ณ ๋ฅผ ๊ฐ์ง๋๋ค.
|
| 98 |
+
## ์ธ์
์ด ์๋ฌด ๋ฉ์ธ์ง๋ ๊ฐ์ง๊ณ ์์ง ์๋ค๋ฉด ์ธ์ฆ์ ์ํ ๋ง์ ๋จผ์ ๊ฑธ๊ฒ ๊ตฌ์ฑํฉ๋๋ค.
|
| 99 |
+
|
| 100 |
+
## ์ธ์ฆ ๋ฉ์ธ์ง
|
|
|
|
|
|
|
| 101 |
if not st.session_state.messages:
|
| 102 |
+
greeting = "๋์์ด์ ์จ์ aitom์ ํ์ฌ ๊ณต์ฌ์ค์ ์์ต๋๋ค.\n๊ฐ๋จํ ์ ๋ฌธ๊ธฐ์ฌ๋ฅผ ์
๋ ฅํ์๋ฉด ์์ฝ์ ์ ๊ณตํฉ๋๋ค.\n๊ณ์ ์ฌ์ฉํ๋ ค๋ฉด \"yes\"๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 103 |
+
st.session_state.messages.append({"role": "assistant", "content": greeting})
|
| 104 |
+
st.markdown(assistant_message_style(assistant_icon_path, greeting), unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
## ์ธ์ฆ ๋ฐ ์๋ต ์์ฑ
|
| 107 |
+
## prompt :=`๋ "์๋ฌ์ค ์ฐ์ฐ์" (:=, walrus operator)๋ก, ์
๋ ฅ๊ฐ์ด ์์ผ๋ฉด ์ด๋ฅผ prompt ๋ณ์์ ํ ๋นํ๋ฉด์ ๋์์ ์กฐ๊ฑด๋ฌธ์ ์คํํ๋ ๊ธฐ๋ฅ์ ํฉ๋๋ค.
|
| 108 |
if prompt := st.chat_input():
|
| 109 |
+
# ์ฌ์ฉ์ ์ง๋ฌธ ์ธ์
์ ์ ์ฅ
|
| 110 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 111 |
+
# ์ฌ์ฉ์ ์ง๋ฌธ ํ๋ฉด์ ํ์
|
| 112 |
+
st.markdown(user_message_style(prompt), unsafe_allow_html=True)
|
| 113 |
+
|
| 114 |
+
# ์ธ์ฆ
|
| 115 |
if st.session_state.approval_state == "require":
|
| 116 |
if prompt.lower() == "yes":
|
| 117 |
st.session_state.approval_state = "approved"
|
| 118 |
+
response = "์ด์ ์์ฝ ๋ชจ๋ธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค. ์ ๋ฌธ๊ธฐ์ฌ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 119 |
else:
|
| 120 |
+
response = "์น์ธ๋์ง ์์์ต๋๋ค. ๊ณ์ํ๋ ค๋ฉด \"yes\"๋ฅผ ์
๋ ฅํด์ฃผ์ธ์."
|
| 121 |
+
# ์๋ต ์์ฑ
|
| 122 |
+
else:
|
| 123 |
+
data = {
|
| 124 |
+
"name": "string",
|
| 125 |
+
"description": prompt,
|
| 126 |
+
"price": 0
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
response = requests.post(url, headers=headers, data=data)
|
| 130 |
+
response = response.text
|
| 131 |
+
print(response)
|
| 132 |
+
|
| 133 |
+
# result = re.sub(r'\s+', ' ', prompt)
|
| 134 |
+
# summarized = pipe(result)
|
| 135 |
+
# response = summarized[0]["summary_text"]
|
| 136 |
+
# ์ด์์คํด์ค์ ๋ฉ์ธ์ง๋ฅผ ๏ฟฝ๏ฟฝ์
์ ์ ์ฅ
|
| 137 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 138 |
+
# ์ด์์คํด์ค์ ๋ฉ์ธ์ง๋ฅผ ํ๋ฉด์ ํ์
|
| 139 |
+
st.markdown(assistant_message_style(assistant_icon_path, response), unsafe_allow_html=True)
|
| 140 |
+
|