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