Spaces:
Build error
Build error
Update Chatbot/app.py
Browse files- Chatbot/app.py +35 -0
Chatbot/app.py
CHANGED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
|
| 5 |
+
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def predict(input, history=[]):
|
| 9 |
+
# tokenize the new input sentence
|
| 10 |
+
new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors='pt')
|
| 11 |
+
|
| 12 |
+
# append the new user input tokens to the chat history
|
| 13 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
| 14 |
+
|
| 15 |
+
# generate a response
|
| 16 |
+
history = model.generate(bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id).tolist()
|
| 17 |
+
|
| 18 |
+
# convert the tokens to text, and then split the responses into the right format
|
| 19 |
+
response = tokenizer.decode(history[0]).split("<|endoftext|>")
|
| 20 |
+
response = [(response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)] # convert to tuples of list
|
| 21 |
+
return response, history
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
import gradio as gr
|
| 25 |
+
|
| 26 |
+
interface = gr.Interface(
|
| 27 |
+
fn=predict,
|
| 28 |
+
theme="default",
|
| 29 |
+
css=".footer {display:none !important}",
|
| 30 |
+
inputs=["text", "state"],
|
| 31 |
+
outputs=["chatbot", "state"],
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == '__main__':
|
| 35 |
+
interface.launch()
|