-
Notifications
You must be signed in to change notification settings - Fork 22
/
chatbot.py
185 lines (152 loc) Β· 5.73 KB
/
chatbot.py
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
175
176
177
178
179
180
181
182
183
184
185
import os
import gradio as gr
from typing import Optional, Tuple
import bentoml
from datasets import Dataset, Audio
from langchain.chains import ConversationChain
from langchain.agents import load_tools, initialize_agent, AgentType
from langchain.memory import ConversationBufferMemory
from langchain.llms import OpenAI
from threading import Lock
PLAYBACK_SAMPLE_RATE = 16000
def load_chain():
"""Logic for loading the chain you want to use should go here."""
memory = ConversationBufferMemory(memory_key="chat_history")
llm = OpenAI(temperature=0)
tools = load_tools(["wikipedia"], llm=llm)
chain = initialize_agent(
tools,
llm,
agent=AgentType.CONVERSATIONAL_REACT_DESCRIPTION,
verbose=True,
memory=memory,
)
return chain
def set_openai_api_key(api_key: str):
"""Set the api key and return chain.
If no api_key, then None is returned.
"""
if api_key:
os.environ["OPENAI_API_KEY"] = api_key
chain = load_chain()
os.environ["OPENAI_API_KEY"] = ""
return chain
class ChatWrapper:
def __init__(self, generate_speech, generate_text):
self.lock = Lock()
self.generate_speech = generate_speech
self.generate_text = generate_text
self.s2t_processor_ref = bentoml.models.get("whisper_processor:latest")
self.processor = bentoml.transformers.load_model(self.s2t_processor_ref)
def __call__(
self,
api_key: str,
audio_path: str,
text_message: str,
history: Optional[Tuple[str, str]],
chain: Optional[ConversationChain],
):
"""Execute the chat functionality."""
self.lock.acquire()
print(f"audio_path : {audio_path} ({type(audio_path)})")
print(f"text_message : {text_message} ({type(text_message)})")
try:
if audio_path is None and text_message is not None:
transcription = text_message
elif audio_path is not None and text_message in [None, ""]:
audio_dataset = Dataset.from_dict({"audio": [audio_path]}).cast_column(
"audio",
Audio(sampling_rate=16000),
)
sample = audio_dataset[0]["audio"]
if sample is not None:
input_features = self.processor(
sample["array"],
sampling_rate=sample["sampling_rate"],
return_tensors="pt",
).input_features
transcription = self.generate_text(input_features)
else:
transcription = None
speech = None
if transcription is not None:
history = history or []
# If chain is None, that is because no API key was provided.
if chain is None:
response = "Please paste your Open AI key."
history.append((transcription, response))
speech = (PLAYBACK_SAMPLE_RATE, self.generate_speech(response))
return history, history, speech, None, None
# Set OpenAI key
import openai
openai.api_key = api_key
# Run chain and append input.
output = chain.run(input=transcription)
speech = (PLAYBACK_SAMPLE_RATE, self.generate_speech(output))
history.append((transcription, output))
except Exception as e:
raise e
finally:
self.lock.release()
return history, history, speech, None, None
def create_block(chat: ChatWrapper):
"""Create the gradio block."""
block = gr.Blocks(css=".gradio-container")
with block:
with gr.Row():
gr.Markdown("<h3><center>BentoML LangChain Demo</center></h3>")
openai_api_key_textbox = gr.Textbox(
placeholder="Paste your OpenAI API key (sk-...)",
show_label=False,
lines=1,
type="password",
)
chatbot = gr.Chatbot()
audio = gr.Audio(label="Chatbot Voice", elem_id="chatbox_voice")
with gr.Row():
audio_message = gr.Audio(
label="User voice message",
source="microphone",
type="filepath",
)
text_message = gr.Text(
label="User text message",
placeholder="Give me 5 gift ideas for my mother",
)
gr.HTML("Demo BentoML application of a LangChain chain.")
gr.HTML(
"<center>Powered by <a href='https://github.com/bentoml/BentoML'>BentoML π±</a> and <a href='https://github.com/hwchase17/langchain'>LangChain π¦οΈπ</a></center>"
)
state = gr.State()
agent_state = gr.State()
audio_message.change(
chat,
inputs=[
openai_api_key_textbox,
audio_message,
text_message,
state,
agent_state,
],
outputs=[chatbot, state, audio, audio_message, text_message],
show_progress=False,
)
text_message.submit(
chat,
inputs=[
openai_api_key_textbox,
audio_message,
text_message,
state,
agent_state,
],
outputs=[chatbot, state, audio, audio_message, text_message],
show_progress=False,
)
openai_api_key_textbox.change(
set_openai_api_key,
inputs=[openai_api_key_textbox],
outputs=[agent_state],
show_progress=False,
)
return block