Spaces:
Sleeping
Sleeping
| # ν¨ν€μ§ λΆλ¬μ€κΈ° | |
| 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)) | |