Using Facebook blenderbot for conversational chatbot
Browse files
app.py
CHANGED
@@ -1,50 +1,16 @@
|
|
1 |
-
from
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
prompt = "<s>"
|
8 |
-
for user_prompt, bot_response in history:
|
9 |
-
prompt += f"[INST] {user_prompt} [/INST]"
|
10 |
-
prompt += f" {bot_response}</s> "
|
11 |
-
if system_prompt:
|
12 |
-
prompt += f"[SYS] {system_prompt} [/SYS]"
|
13 |
-
prompt += f"[INST] {message} [/INST]"
|
14 |
-
return prompt
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
)
|
19 |
-
|
20 |
-
if temperature < 1e-2:
|
21 |
-
temperature = 1e-2
|
22 |
-
top_p = float(top_p)
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
max_new_tokens=max_new_tokens,
|
27 |
-
top_p=top_p,
|
28 |
-
repetition_penalty=repetition_penalty,
|
29 |
-
do_sample=True,
|
30 |
-
seed=42,
|
31 |
-
)
|
32 |
-
|
33 |
-
formatted_prompt = format_prompt(prompt, history, system_prompt)
|
34 |
-
|
35 |
-
stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
|
36 |
-
output = ""
|
37 |
-
|
38 |
-
for response in stream:
|
39 |
-
output += response.token.text
|
40 |
-
yield output
|
41 |
-
return output
|
42 |
-
|
43 |
-
mychatbot = gr.Chatbot( bubble_full_width=False, show_label=False, show_copy_button=True, likeable=True,)
|
44 |
-
|
45 |
-
demo = gr.ChatInterface(
|
46 |
-
fn=generate,
|
47 |
-
chatbot=mychatbot
|
48 |
-
)
|
49 |
-
|
50 |
-
demo.queue().launch(show_api=False)
|
|
|
1 |
+
from transformers import pipeline
|
2 |
+
from transformers import Conversation
|
3 |
import gradio as gr
|
4 |
|
5 |
+
message_list = []
|
6 |
+
response_list = []
|
7 |
|
8 |
+
chatbot = pipeline("conversational", model = "facebook/blenderbot-400M-distill")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
def vanilla_chatbot(message, history):
|
11 |
+
conversation = Conversation(text = message, past_user_inputs = message_list, generated_responses = response_list)
|
12 |
+
conversation = chatbot(conversation)
|
13 |
+
return conversation.generate_response[-1]
|
|
|
|
|
|
|
14 |
|
15 |
+
demo_chatbot = gr.ChatInterface(vanilla_chatbot, title = "Vanilla Chatbot", description = "Enter text to start chatting.")
|
16 |
+
demo_chatbot.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|