Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,21 @@
|
|
| 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 |
-
##
|
| 15 |
-
pipe = pipeline("
|
| 16 |
|
| 17 |
## assitant ์๋ฐํ ์ด๋ฏธ์ง ๊ฒฝ๋ก
|
| 18 |
-
assistant_icon_path = "
|
| 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 { /* ์ฑํ
๋ฉ์์ง๋ฅผ ๊ฐ์ธ๋ ์ปจํ
์ด๋๋ฅผ ์ค๋ช
ํฉ๋๋ค. */
|
|
@@ -70,8 +56,6 @@ st.markdown("""
|
|
| 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>
|
|
@@ -83,58 +67,38 @@ def assistant_message_style(assistant_icon_path, answer):
|
|
| 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 = "
|
| 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() ==
|
| 117 |
st.session_state.approval_state = "approved"
|
| 118 |
-
response = "
|
| 119 |
else:
|
| 120 |
-
response = "
|
| 121 |
# ์๋ต ์์ฑ
|
| 122 |
else:
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 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 |
-
|
|
|
|
| 1 |
# ํจํค์ง ๋ถ๋ฌ์ค๊ธฐ
|
| 2 |
import re
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
from transformers import pipeline
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
# ๊ธ๋ก๋ฒ ๋ณ์ ์ ์ธ
|
| 7 |
+
## ๋ณธ ์ฝ๋์์ ์ฌ์ฉ๋ ์ ๋ฌธ๊ธฐ์ฌ ์์ฝ์ฉ ๋ชจ๋ธ(https://huggingface.co/gangyeolkim/kobart-korean-summarizer-v2)์ ํ
์คํธ์ฉ ์
๋๋ค.
|
| 8 |
+
pipe = pipeline("{purpose}", model="{model/path}")
|
| 9 |
|
| 10 |
## assitant ์๋ฐํ ์ด๋ฏธ์ง ๊ฒฝ๋ก
|
| 11 |
+
assistant_icon_path = "icon/to/path"
|
| 12 |
|
| 13 |
## ์ธ์
์ํ์์ approval_state ๋ฐ messages ์ด๊ธฐํ
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
if "approval_state" not in st.session_state:
|
| 15 |
st.session_state.approval_state = "require"
|
| 16 |
if "messages" not in st.session_state:
|
| 17 |
st.session_state.messages = []
|
| 18 |
|
|
|
|
|
|
|
|
|
|
| 19 |
st.markdown("""
|
| 20 |
<style>
|
| 21 |
.chat-container { /* ์ฑํ
๋ฉ์์ง๋ฅผ ๊ฐ์ธ๋ ์ปจํ
์ด๋๋ฅผ ์ค๋ช
ํฉ๋๋ค. */
|
|
|
|
| 56 |
</style>
|
| 57 |
""", unsafe_allow_html=True)
|
| 58 |
|
|
|
|
|
|
|
| 59 |
def user_message_style(question):
|
| 60 |
return f"""<div class="chat-container user-container">
|
| 61 |
<div class="chat-bubble user-bubble">{question}</div>
|
|
|
|
| 67 |
<div class="chat-bubble assistant-bubble">{answer}</div>
|
| 68 |
</div>"""
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
for message in st.session_state.messages:
|
| 71 |
if message["role"] == "user":
|
| 72 |
st.markdown(user_message_style(message["content"]), unsafe_allow_html=True)
|
| 73 |
else:
|
| 74 |
st.markdown(assistant_message_style(assistant_icon_path, message["content"]), unsafe_allow_html=True)
|
| 75 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if not st.session_state.messages:
|
| 77 |
+
greeting = "{์ธ์ฌ๋ง๋ง}"
|
| 78 |
st.session_state.messages.append({"role": "assistant", "content": greeting})
|
| 79 |
st.markdown(assistant_message_style(assistant_icon_path, greeting), unsafe_allow_html=True)
|
| 80 |
|
|
|
|
|
|
|
| 81 |
if prompt := st.chat_input():
|
| 82 |
# ์ฌ์ฉ์ ์ง๋ฌธ ์ธ์
์ ์ ์ฅ
|
| 83 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 84 |
+
|
| 85 |
# ์ฌ์ฉ์ ์ง๋ฌธ ํ๋ฉด์ ํ์
|
| 86 |
st.markdown(user_message_style(prompt), unsafe_allow_html=True)
|
| 87 |
+
|
| 88 |
# ์ธ์ฆ
|
| 89 |
if st.session_state.approval_state == "require":
|
| 90 |
+
if prompt.lower() == {์ธ์ฆ ์ฝ๋}:
|
| 91 |
st.session_state.approval_state = "approved"
|
| 92 |
+
response = "{์ธ์ฆ ์๋ฃ ๋ฉ์ธ์ง}"
|
| 93 |
else:
|
| 94 |
+
response = "{์ธ์ฆ ์คํจ ๋ฉ์ธ์ง}"
|
| 95 |
# ์๋ต ์์ฑ
|
| 96 |
else:
|
| 97 |
+
result = re.sub(r'\s+', ' ', prompt)
|
| 98 |
+
summarized = pipe(result)
|
| 99 |
+
response = summarized[0]["summary_text"]
|
| 100 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# ์ด์์คํด์ค์ ๋ฉ์ธ์ง๋ฅผ ์ธ์
์ ์ ์ฅ
|
| 102 |
st.session_state.messages.append({"role": "assistant", "content": response})
|
| 103 |
# ์ด์์คํด์ค์ ๋ฉ์ธ์ง๋ฅผ ํ๋ฉด์ ํ์
|
| 104 |
st.markdown(assistant_message_style(assistant_icon_path, response), unsafe_allow_html=True)
|
|
|