Spaces:
Runtime error
Runtime error
okoliechykwuka
commited on
Commit
·
18e1e47
1
Parent(s):
c0cb1ef
Add chatbot application file
Browse files- Dockerfile +14 -0
- chatbot.py +76 -0
- requirements.txt +5 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
RUN python3 -m pip install --no-cache-dir --upgrade pip
|
7 |
+
RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["panel", "serve", "/code/chatbot.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "chukypedro-chatbot.hf.space", "--allow-websocket-origin", "0.0.0.0:7860"]
|
12 |
+
|
13 |
+
RUN mkdir /.cache
|
14 |
+
RUN chmod 777 /.cache
|
chatbot.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.prompts import (
|
2 |
+
ChatPromptTemplate,
|
3 |
+
MessagesPlaceholder,
|
4 |
+
SystemMessagePromptTemplate,
|
5 |
+
HumanMessagePromptTemplate
|
6 |
+
)
|
7 |
+
from langchain.chains import ConversationChain
|
8 |
+
from langchain.chat_models import ChatOpenAI
|
9 |
+
from langchain.memory import ConversationBufferMemory
|
10 |
+
import panel as pn
|
11 |
+
import os
|
12 |
+
|
13 |
+
from dotenv import load_dotenv
|
14 |
+
load_dotenv()
|
15 |
+
|
16 |
+
panels = [] # collect display
|
17 |
+
|
18 |
+
pn.extension('texteditor', template="bootstrap", sizing_mode='stretch_width')
|
19 |
+
pn.state.template.param.update(
|
20 |
+
main_max_width="690px",
|
21 |
+
header_background="green",
|
22 |
+
title='Conversational Chatbot Application'
|
23 |
+
)
|
24 |
+
|
25 |
+
#Widgets
|
26 |
+
openaikey = pn.widgets.PasswordInput(
|
27 |
+
value="", placeholder="Enter your OpenAI API Key here...", width=300,
|
28 |
+
)
|
29 |
+
inp = pn.widgets.TextInput(value="Hi", placeholder='Enter text here…', toolbar=False, height=50, width=500)
|
30 |
+
button_conversation = pn.widgets.Button(name="Chat!", button_type='primary')
|
31 |
+
|
32 |
+
spacer = pn.Spacer(width=100)
|
33 |
+
|
34 |
+
#LLM Model
|
35 |
+
def chat_bot(input):
|
36 |
+
prompt = ChatPromptTemplate.from_messages([
|
37 |
+
SystemMessagePromptTemplate.from_template("The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know."),
|
38 |
+
MessagesPlaceholder(variable_name="history"),
|
39 |
+
HumanMessagePromptTemplate.from_template("{input}")
|
40 |
+
])
|
41 |
+
|
42 |
+
llm = ChatOpenAI(temperature=0)
|
43 |
+
memory = ConversationBufferMemory(return_messages=True)
|
44 |
+
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)
|
45 |
+
response = conversation.predict(input=input)
|
46 |
+
|
47 |
+
return response
|
48 |
+
|
49 |
+
#Message function
|
50 |
+
def collect_messages(_):
|
51 |
+
os.environ["OPENAI_API_KEY"] = openaikey.value
|
52 |
+
prompt = inp.value_input
|
53 |
+
inp.value = ''
|
54 |
+
if prompt:
|
55 |
+
response = chat_bot(input= inp.value_input)
|
56 |
+
panels.append(
|
57 |
+
pn.Row('User:', pn.pane.Markdown(prompt, width=600)))
|
58 |
+
panels.append(
|
59 |
+
pn.Row('Assistant:', pn.pane.Markdown(response, width=600, style={'background-color': '#F6F6F6'})))
|
60 |
+
|
61 |
+
return pn.Column(*panels)
|
62 |
+
|
63 |
+
#layout
|
64 |
+
interactive_conversation = pn.bind(collect_messages, button_conversation)
|
65 |
+
pn.Column(
|
66 |
+
pn.pane.Markdown("""
|
67 |
+
## \U0001F60A! A friendly Conversational AI Chatbot
|
68 |
+
1) Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account).
|
69 |
+
"""
|
70 |
+
),
|
71 |
+
pn.Row(inp,spacer,openaikey),
|
72 |
+
pn.Row(button_conversation, width=200, margin=(5,150)),
|
73 |
+
pn.panel(interactive_conversation, loading_indicator=True, height=200),
|
74 |
+
).servable()
|
75 |
+
|
76 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
python-dotenv
|
2 |
+
langchain
|
3 |
+
panel
|
4 |
+
tiktoken
|
5 |
+
openai
|