Spaces:
Sleeping
Sleeping
Commit
·
b17b792
1
Parent(s):
0317a5d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
|
5 |
+
title = 'ChatBot'
|
6 |
+
description = 'This is a test model i created to learn how to create one haha'
|
7 |
+
examples = [['What is life?']]
|
8 |
+
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained('microsoft/DialoGPT-large')
|
10 |
+
model = AutoModelForCausalLM.from_pretrained('microsoft/DialoGPT-large')
|
11 |
+
|
12 |
+
def predict(input, history=[]):
|
13 |
+
new_user_input_ids = tokenizer.encode(
|
14 |
+
input + tokenizer.eos_token, return_tensors='pt'
|
15 |
+
)
|
16 |
+
bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
|
17 |
+
|
18 |
+
history = model.generate(
|
19 |
+
bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
|
20 |
+
).tolist()
|
21 |
+
|
22 |
+
response = tokenizer.decode(history[0]).split('<|endoftext|>')
|
23 |
+
|
24 |
+
response = [
|
25 |
+
(response[i], response[i+1]) for i in range(0, len(response) - 1, 2)
|
26 |
+
]
|
27 |
+
return response, history
|
28 |
+
|
29 |
+
gr.Interface(
|
30 |
+
fn=predict,
|
31 |
+
title=title,
|
32 |
+
description=description,
|
33 |
+
examples=examples,
|
34 |
+
inputs=['text', 'state'],
|
35 |
+
outputs=['chatbot', 'state'],
|
36 |
+
).launch()
|