Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| from transformers import pipeline | |
| import re | |
| def replace_multiple_spaces(text): | |
| return re.sub(r'\s+', ' ', text) | |
| pipe = pipeline("summarization", model="gangyeolkim/kobart-korean-summarizer-v2") | |
| assistant_icon_path = "https://huggingface.co/spaces/randmimc/aitom/resolve/main/chat_icon/assistant.ico" | |
| user_icon_path = "https://huggingface.co/spaces/randmimc/aitom/resolve/main/chat_icon/user01.png" | |
| # ์ธ์ ์ํ์์ 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 = [] | |
| # Custom CSS to align messages and wrap text | |
| st.markdown(""" | |
| <style> | |
| .chat-container { | |
| display: flex; | |
| align-items: flex-start; | |
| margin-bottom: 10px; | |
| } | |
| .user-container { | |
| justify-content: flex-end; | |
| } | |
| .assistant-container { | |
| justify-content: flex-start; | |
| flex-direction: row; /* Avatar์ Bubble์ ์ํ ์ ๋ ฌ */ | |
| } | |
| .chat-bubble { | |
| padding: 10px; | |
| border-radius: 10px; | |
| max-width: 70%; | |
| word-wrap: break-word; | |
| white-space: pre-wrap; | |
| } | |
| .user-bubble { | |
| background-color: #e5e5e5; | |
| text-align: left; | |
| } | |
| .assistant-bubble { | |
| font-size: 14px; | |
| text-align: left; /* ์ผ์ชฝ ์ ๋ ฌ */ | |
| background-color: #f8d7da; | |
| } | |
| .avatar { | |
| width: 30px; | |
| height: 30px; | |
| border-radius: 50%; | |
| margin-right: 10px; /* Bubble๊ณผ ๊ฐ๊ฒฉ */ | |
| } | |
| </style> | |
| """, unsafe_allow_html=True) | |
| # Display chat messages from history | |
| for message in st.session_state.messages: | |
| if message["role"] == "user": | |
| st.markdown( | |
| f""" | |
| <div class="chat-container user-container"> | |
| <div class="chat-bubble user-bubble">{message["content"]}</div> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| else: | |
| st.markdown( | |
| f""" | |
| <div class="chat-container assistant-container"> | |
| <img src="{assistant_icon_path}" class="avatar"> | |
| <div class="chat-bubble assistant-bubble">{message["content"]}</div> | |
| </div> | |
| """, unsafe_allow_html=True) | |
| # Start with an assistant message | |
| if not st.session_state.messages: | |
| st.session_state.messages.append({ | |
| "role": "assistant", | |
| "avatar": assistant_icon_path, | |
| "content": """๋์์ด์ ์จ์ aitom์ ํ์ฌ ๊ณต์ฌ์ค์ ์์ต๋๋ค. | |
| ํ์ง๋ง ! ๊ฐ๋จํ ํ์ฉํด ๋ณด์ค ์ ์๋ ์์ฝ ๋ชจ๋ธ์ ์ ๊ณตํ๊ณ ์์ต๋๋ค. | |
| ์งง์ ์ ๋ฌธ๊ธฐ์ฌ๋ฅผ ๋งํด ์ฃผ์๋ฉด ์์ฝ๋ฌธ์ ์ ๊ณตํฉ๋๋ค. | |
| ๊ณ์ ์ฌ์ฉํ๊ณ ์ถ๋ค๋ฉด "yes" ๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์. | |
| """}) | |
| st.rerun() | |
| if prompt := st.chat_input(): | |
| st.session_state.messages.append({"role": "user", "avatar": user_icon_path, "content": prompt}) | |
| if st.session_state.approval_state == "require": | |
| if prompt.lower() == "yes": | |
| st.session_state.approval_state = "approved" | |
| st.session_state.messages.append({"role": "assistant", "avatar": assistant_icon_path, "content": "์ด์ ๊ฐ๋จํ ์์ฝ ๋ชจ๋ธ์ ์ฌ์ฉํ์ค ์ ์์ต๋๋ค. ๊ฐ๋จํ ์ ๋ฌธ๊ธฐ์ฌ๋ฅผ ์ ๋ ฅํด ์ฃผ์ธ์."}) | |
| else: | |
| st.session_state.messages.append({"role": "assistant", "avatar": assistant_icon_path, "content": '์น์ธํ์ง ๋ชปํ์์ต๋๋ค. ์น์ธ์ ์ํด "yes"๋ฅผ ์ ๋ ฅํด์ฃผ์ธ์.'}) | |
| elif st.session_state.approval_state == "approved": | |
| result = replace_multiple_spaces(prompt) | |
| summarized = pipe(result) | |
| st.session_state.messages.append({"role": "assistant", "avatar": assistant_icon_path, "content": summarized[0]["summary_text"]}) | |
| st.rerun() | |