Spaces:
Running
Running
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์ ์ด์ฉํ๋ค๋ฉด ์ด ๊ณผ์ ์ ๋จ์ํํ๊ณ ์๊ฐ์ ์ ์ฝํ ์ ์์ ๊ฒ ๊ฐ์ต๋๋ค.
|