카테고리 없음

[Python] 파이썬으로 챗봇 만들기 (보카딜로 웹프레임워크, 웹소켓 사용) 2편

DKel 2021. 2. 3. 19:21
반응형

이전 글

[Python] 파이썬으로 챗봇 만들기 (보카딜로 웹프레임워크, 웹소켓 사용) 1편

 

# chatbot.py
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

somename = ChatBot("Somename")

trainer = ChatterBotCorpusTrainer(somename)
trainer.train(
    "chatterbot.corpus.english.greetings", "chatterbot.corpus.english.conversations"
)

chatbot.py

 

chatbot.py 파일을 작성하고 나서는 Python 인터프리터 환경에서 챗봇이 정상적인지 확인해볼 수 있습니다.

 

>>> from chatbot import somename  # Be patient — this may take a few seconds to load!
>>> somename.get_response("Hi, there!")
<Statement text:There should be one-- and preferably only one --obvious way to do it.>

이제 somename을 WebSocket을 사용하여 Endpoint에 연결해보겠습니다.

새로운 메시지를 받을 때마다, 우리는 somename에게 메시지를 주고, 챗봇의 답장을 받을 것입니다.

 

# app.py
from chatbot import somename

...


@app.websocket_route("/conversation")
async def converse(ws):
    async for message in ws:
        response = somename.get_response(message)
        await ws.send(str(response))

chatbot.py

위와 같이 코드를 작성하게 되면

app.py 를 활용하여 웹소켓 route인 /conversation을 정의했기 때문에

해당 Endpoint로 요청을 보내면 적절한 답변이 somename에게서 돌아올 것입니다.

 

$ python client.py
> Hi there!
I am a chat bot. I am the original chat bot. Did you know that I am incapable of error?
> Where are you?
I am on the Internet.
>

 

[참고 URL]

Bocadilo 파이썬 웹프레임워크 URL: bocadilloproject.github.io/

 

Bocadillo | Python async web framework

 

bocadilloproject.github.io

 

lemondkel - Overview

4-Year Web programmer. lemondkel has 38 repositories available. Follow their code on GitHub.

github.com

반응형