cEight_demo / app.py
randmimc's picture
Update app.py
6e13d79 verified
Raw
History Blame Contribute Delete
7.26 kB
# νŒ¨ν‚€μ§€ 뢈러였기
import re
import requests
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 μ΄ˆκΈ°ν™”
## β€» μ„Έμ…˜(session) :
## μ‚¬μš©μžμ˜ μΈν„°λž™μ…˜μ„ κ΄€λ¦¬ν•˜λŠ” κΈ°λŠ₯μž…λ‹ˆλ‹€.
## Streamlit은 기본적으둜 μ‚¬μš©μžκ°€ μ•±κ³Ό μƒν˜Έμž‘μš©ν•  λ•Œλ§ˆλ‹€ 전체 μ½”λ“œκ°€ λ‹€μ‹œ μ‹€ν–‰λ˜λŠ” ꡬ쑰인데,
## μ„Έμ…˜ μƒνƒœ(Session State)λ₯Ό ν™œμš©ν•˜λ©΄ 데이터λ₯Ό μœ μ§€ν•˜λ©΄μ„œλ„ 효율적인 앱을 λ§Œλ“€ 수 μžˆμŠ΅λ‹ˆλ‹€.
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 = []
if "number" not in st.session_state:
st.session_state.number = []
# CSS μž‘μ„±
## st.markdownλŠ” ν…μŠ€νŠΈλ₯Ό λ§ˆν¬λ‹€μš΄ ν˜•μ‹μœΌλ‘œ λžœλ”λ§ ν•˜λŠ” ν•¨μˆ˜μ΄μ§€λ§Œ, unsafe_allow_html=True μ˜΅μ…˜μ„ μ‚¬μš©ν•˜λ©΄ HTML/CSSλ₯Ό 직접 μ μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
## μŠ€νŠΈλ¦Όλ¦Ώμ—μ„œ λ””ν΄νŠΈλ‘œ μ μš©ν•˜λŠ” λ””μžμΈμ΄ λ§˜μ— λ“€μ§€ μ•ŠλŠ”λ‹€λ©΄ CSSλ₯Ό μž‘μ„±ν•˜μ—¬ λ³€κ²½ν•  수 μžˆμŠ΅λ‹ˆλ‹€.
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)
# λ©”μ„Έμ§€ wrapper
# html ν…Œκ·Έλ₯Ό μ΄μš©ν•˜μ—¬ μ±—νŒ… λ§€μ„Έμ§€λ§ˆλ‹€ css μŠ€νƒ€μΌμ„ μ μš©ν•˜κΈ° μœ„ν•΄ μž‘μ„±λœ ν•¨μˆ˜μž…λ‹ˆλ‹€.
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>"""
# BACK
## κ³Όκ±° μ±„νŒ… λ‚΄μš© 좜λ ₯
## 슀트림릿의 st.chat_input() λ©”μ„œλ“œλŠ” μ‚¬μš©μžκ°€ μž…λ ₯을 μ£Όλ©΄ μ„Έμ…˜λ‚΄μš©μ„ κΈ°μ–΅ν•˜λ©΄μ„œ μ½”λ“œλ₯Ό λ‹€μ‹œ μ‹€ν–‰ν•˜λŠ” νŠΉμ„±μ΄ μžˆμŠ΅λ‹ˆλ‹€.
## λ”°λΌμ„œ μ„Έμ…˜μ— μ €μž₯된 κ³Όκ±° λŒ€ν™” λ‚΄μš©μ„ 화면에 좜λ ₯ν•˜κΈ° μœ„ν•΄μ„œ μƒˆλ‘œ μ‹€ν–‰λœ μ½”λ“œμ— κ³Όκ±° λ‚΄μš©μ„ 미리 λΏŒλ €λ‘˜ 수 μž‡κ²Œ μ•„λž˜μ™€ 같은 μ½”λ“œλ₯Ό μž‘μ„±ν•΄ 두어야 ν•©λ‹ˆλ‹€.
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 = "접속 μ½”λ“œλ₯Ό μž…λ ₯ν•΄μ£Όμ„Έμš”"
st.session_state.messages.append({"role": "assistant", "content": greeting})
st.markdown(assistant_message_style(assistant_icon_path, greeting), unsafe_allow_html=True)
## 인증 및 응닡 생성
## prompt :=`λŠ” "μ›”λŸ¬μŠ€ μ—°μ‚°μž" (:=, walrus operator)둜, μž…λ ₯값이 있으면 이λ₯Ό prompt λ³€μˆ˜μ— ν• λ‹Ήν•˜λ©΄μ„œ λ™μ‹œμ— 쑰건문을 μ‹€ν–‰ν•˜λŠ” κΈ°λŠ₯을 ν•©λ‹ˆλ‹€.
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":
url = "http://llm.theimc.co.kr/reference/"
st.session_state.number = prompt
response = requests.get(url, data={'number':st.session_state.number})
if response.text == "true":
st.session_state.approval_state = "approved"
response = "이제 8b λͺ¨λΈμ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. 단 λ””μžμΈμ€ μ±„νŒ… ν˜•νƒœμ΄μ§€λ§Œ λ©€ν‹°ν„΄(말이 λŒ€ν™”μ²˜λŸΌ μ—°κ²°λ˜λŠ” κΈ°λŠ₯)을 μ§€μ›ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μ°¨ν›„ μ—…λ°μ΄νŠΈ μ˜ˆμ •μž…λ‹ˆλ‹€."
else:
response = "μŠΉμΈλ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€. 접속 μ½”λ“œλ₯Ό λ‹€μ‹œ μž…λ ₯ν•΄μ£Όμ„Έμš”"
# 응닡 생성
else:
url = "http://llm.theimc.co.kr/reference/base_sllm"
headers = {"Content-Type": "application/x-www-form-urlencoded",
"secret-key" : st.session_state.number}
data = {
"question": prompt
}
response = requests.post(url, headers=headers, data=data)
response = response.json()
print(response)
# μ–΄μ‹œμŠ€ν„΄μŠ€μ˜ λ©”μ„Έμ§€λ₯Ό μ„Έμ…˜μ— μ €μž₯
st.session_state.messages.append({"role": "assistant", "content": response})
# μ–΄μ‹œμŠ€ν„΄μŠ€μ˜ λ©”μ„Έμ§€λ₯Ό 화면에 ν‘œμ‹œ
st.html(assistant_message_style(assistant_icon_path, response))