File size: 6,505 Bytes
2872c06
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174


<h2 align="center">
Streamlit Chatbot
</h2>

<div align="center">
  <img src="https://img.shields.io/badge/python-v3.9.16-blue.svg"/>
  <img src="https://img.shields.io/badge/streamlit-v1.20.0-blue.svg"/>
  <img src="https://img.shields.io/badge/streamlit_chat-v0.0.2.2-blue.svg"/>
</div>

**streamlit-chat**์€ Streamlit์„ ์ด์šฉํ•˜์—ฌ ์‹ค์‹œ๊ฐ„ ๋Œ€ํ™”ํ˜• Web ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‰ฝ๊ฒŒ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ํŒจํ‚ค์ง€์ž…๋‹ˆ๋‹ค. ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ ๋งค์šฐ ๊ฐ„๋‹จํ•˜๋ฉฐ [์˜ˆ์ œ](https://share.streamlit.io/ai-yash/st-chat/main/examples/chatbot.py) ํ™•์ธ๋„ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.



<div align="center">
<img src="https://blog.kakaocdn.net/dn/cACrfC/btr65p6tNGQ/kPoXKMh4LAmUtsuhGXOHOk/img.gif" width="70%">
</div>

streamlit-chat์œผ๋กœ Hugging Face์—์„œ ์ œ๊ณตํ•˜๋Š” Facebook AI BlenderBot๊ณผ OpenAI์˜ GPT-3 ๋ชจ๋ธ๋กœ ์ฑ—๋ด‡ ํ”„๋กœ๊ทธ๋žจ์„ ๋งŒ๋“ค์–ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

------

### 1. ์„ค์น˜ (Installation)

streamlit๊ณผ streamlit-chat ํŒจํ‚ค์ง€ ์„ค์น˜๋Š” pip ๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

```bash
pip install streamlit
pip install streamlit-chat
```

streamlit ์„ค์น˜์™€ ๊ด€๋ จ๋œ ๋‚ด์šฉ์€ ์ด์ „ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค.

 [Streamlit ์‹œ์ž‘ํ•˜๊ธฐ (์„ค์น˜๋ฐฉ๋ฒ•)](https://yunwoong.tistory.com/226)

### 2. BlenderBot ์ฑ—๋ด‡

BlenderBot์€ Hugging Face์˜ Transformers ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋จผ์ € Hugging Face Inference API Key ๋ฐœ๊ธ‰์„ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๊ธ€์„ ์ฐธ๊ณ ํ•˜์„ธ์š”.

  [Hugging Face Inference API Key ๋ฐœ๊ธ‰](https://yunwoong.tistory.com/225)

Python ํŒŒ์ผ blenderbot_app.py ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. API_TOKEN์€ ์ž์‹ ์˜ Hugging Face Inference API Key ๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: hf_xxxxxxxxxxxxxxxxxxxxx)

```python
import streamlit as st
from streamlit_chat import message
import requests
 
API_URL = "https://api-inference.huggingface.co/models/facebook/blenderbot-400M-distill"
API_TOKEN = "YOUR API TOKEN HERE"
headers = {"Authorization": f"Bearer {API_TOKEN}"}
 
st.header("๐Ÿค–Yunwoong's BlenderBot (Demo)")
st.markdown("[Be Original](https://yunwoong.tistory.com/)")
 
if 'generated' not in st.session_state:
    st.session_state['generated'] = []
 
if 'past' not in st.session_state:
    st.session_state['past'] = []
 
def query(payload):
	response = requests.post(API_URL, headers=headers, json=payload)
	return response.json()
 
 
with st.form('form', clear_on_submit=True):
    user_input = st.text_input('You: ', '', key='input')
    submitted = st.form_submit_button('Send')
 
if submitted and user_input:
    output = query({
        "inputs": {
            "past_user_inputs": st.session_state.past,
            "generated_responses": st.session_state.generated,
            "text": user_input,
        },
        "parameters": {"repetition_penalty": 1.33},
    })
 
    st.session_state.past.append(user_input)
    st.session_state.generated.append(output["generated_text"])
 
if st.session_state['generated']:
    for i in range(len(st.session_state['generated'])-1, -1, -1):
        message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
        message(st.session_state["generated"][i], key=str(i))
```

ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด Streamlit ์•ฑ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

```bash
streamlit run blenderbot_app.py
```

<div align="center">
<img src="https://blog.kakaocdn.net/dn/nwBzi/btr7cISnZwI/JK6JWPNCeEUNtBGRShOGFK/img.gif" width="70%">
</div>

### 3. GPT-3 ์ฑ—๋ด‡

๋‹ค์Œ์œผ๋กœ OpenAI API๋ฅผ ์ด์šฉํ•œ GPT-3 ์ฑ—๋ด‡์„ ๋งŒ๋“ค๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋จผ์ € OpenAI API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด API ํ‚ค ๋ฐœ๊ธ‰์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € [OpenAI API ์‚ฌ์ดํŠธ](https://platform.openai.com/)๋กœ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. OpenAI ๊ณ„์ •์ด ํ•„์š”ํ•˜๋ฉฐ ๊ณ„์ •์ด ์—†๋‹ค๋ฉด ๊ณ„์ • ์ƒ์„ฑ์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. ๊ฐ„๋‹จํžˆ Google์ด๋‚˜ Microsoft ๊ณ„์ •์„ ์—ฐ๋™ ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฏธ ๊ณ„์ •์ด ์žˆ๋‹ค๋ฉด ๋กœ๊ทธ์ธ ํ›„ ์ง„ํ–‰ํ•˜์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค.

๋กœ๊ทธ์ธ์ด ๋˜์—ˆ๋‹ค๋ฉด ์šฐ์ธก ์ƒ๋‹จ Personal -> [ View API Keys ] ๋ฅผ ํด๋ฆญํ•ฉ๋‹ˆ๋‹ค.

![img](https://blog.kakaocdn.net/dn/xKSqg/btr62GPoKvC/OF7uLj6YZhmv1VkVyDOJN0/img.png)

[ + Create new secret key ] ๋ฅผ ํด๋ฆญํ•˜์—ฌ API Key๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. API key generated ์ฐฝ์ด ํ™œ์„ฑํ™”๋˜๋ฉด Key ๋ฅผ ๋ฐ˜๋“œ์‹œ ๋ณต์‚ฌํ•˜์—ฌ ๋‘์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์ฐฝ์„ ๋‹ซ์œผ๋ฉด ๋‹ค์‹œ ํ™•์ธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. (๋งŒ์•ฝ ๋ณต์‚ฌํ•˜์ง€ ๋ชปํ–ˆ๋‹ค๋ฉด ๋‹ค์‹œ Create new secret key ๋ฒ„ํŠผ์„ ๋ˆŒ๋Ÿฌ ์ƒ์„ฑํ•˜๋ฉด ๋˜๋‹ˆ ๊ฑฑ์ •ํ•˜์ง€ ์•Š์œผ์…”๋„ ๋ฉ๋‹ˆ๋‹ค.)

Python ํŒŒ์ผ chatgpt_app.py ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค. openai.api_key๋Š” ์ž์‹ ์˜ OpenAI API Key๋ฅผ ์ž…๋ ฅํ•ฉ๋‹ˆ๋‹ค. (์˜ˆ: sk-xxxxxxxxxxxxxxxxxxxxx)

```python
import openai
import streamlit as st
from streamlit_chat import message
 
openai.api_key = 'YOUR API KEY HERE'
 
def generate_response(prompt):
    completions = openai.Completion.create (
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        stop=None,
        temperature=0,
        top_p=1,
    )
 
    message = completions["choices"][0]["text"].replace("\n", "")
    return message
 
 
st.header("๐Ÿค–Yunwoong's ChatGPT-3 (Demo)")
st.markdown("[Be Original](https://yunwoong.tistory.com/)")
 
if 'generated' not in st.session_state:
    st.session_state['generated'] = []
 
if 'past' not in st.session_state:
    st.session_state['past'] = []
 
with st.form('form', clear_on_submit=True):
    user_input = st.text_input('You: ', '', key='input')
    submitted = st.form_submit_button('Send')
 
if submitted and user_input:
    output = generate_response(user_input)
    st.session_state.past.append(user_input)
    st.session_state.generated.append(output)
 
if st.session_state['generated']:
    for i in range(len(st.session_state['generated'])-1, -1, -1):
        message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
        message(st.session_state["generated"][i], key=str(i))
```

ํ„ฐ๋ฏธ๋„์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž…๋ ฅํ•˜๋ฉด Streamlit ์•ฑ์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.

```bash
streamlit run chatgpt_app.py
```

<div align="center">
<img src="https://blog.kakaocdn.net/dn/bceZgF/btr7cdZnqLq/FpszCkE2k72fsQrlS8FSrk/img.gif" width="70%">
</div>

------

๋งค์šฐ ๊ฐ„๋‹จํ•˜๊ฒŒ Web ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค์–ด ์‹œ๋ฎฌ๋ ˆ์ด์…˜์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์ผ ๊ฐœ๋ฐœ์ž๊ฐ€ ์•„๋‹Œ ๋ฐ์ดํ„ฐ ๊ณผํ•™์ž๋‚˜ AI ๋ชจ๋ธ๋Ÿฌ์ธ ๊ฒฝ์šฐ ์‹œ๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๋ ค๋ฉด ์‹œ๊ฐ„๊ณผ ๋…ธ๋ ฅ์ด ๋งŽ์ด ๋“ค ์ˆ˜ ์žˆ์ง€๋งŒ Streamlit์„ ์ด์šฉํ•œ๋‹ค๋ฉด ์ด ๊ณผ์ •์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ์‹œ๊ฐ„์„ ์ ˆ์•ฝํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.