randmimc commited on
Commit
91f6af8
ยท
verified ยท
1 Parent(s): 0967be1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -49
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
- ## ๋ชจ๋ธ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ(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 { /* ์ฑ„ํŒ… ๋ฉ”์‹œ์ง€๋ฅผ ๊ฐ์‹ธ๋Š” ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. */
@@ -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 = "๋”์•„์ด์— ์”จ์— 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
-
 
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)