Tonic commited on
Commit
e1547c3
1 Parent(s): 1d00be7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -51
app.py CHANGED
@@ -1,51 +1,46 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer
2
- import gradio as gr
3
- import torch
4
-
5
-
6
- title = "EZChat"
7
- description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT-medium)"
8
- examples = [["How are you?"]]
9
-
10
- # Set the padding token to be used and initialize the model
11
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium", padding_side='left')
12
- model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
13
- tokenizer.add_special_tokens({'pad_token': '[EOS]'})
14
- tokenizer.pad_token = tokenizer.eos_token
15
-
16
- #predict
17
- def predict(input, history=[]):
18
- # tokenize the new input sentence
19
- new_user_input_ids = tokenizer.encode(
20
- input + tokenizer.eos_token, padding=True, truncation=True, return_tensors="pt"
21
- )
22
- #Attention Mask For Reliable Results
23
- attention_mask = inputs['attention_mask']
24
-
25
- # append the new user input tokens to the chat history
26
- bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
27
-
28
- # generate a response
29
- history = model.generate(
30
- bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
31
- ).tolist()
32
-
33
- # convert the tokens to text, and then split the responses into lines
34
- response = tokenizer.decode(history[0]).split("<|endoftext|>")
35
- # print('decoded_response-->>'+str(response))
36
- response = [
37
- (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
38
- ] # convert to tuples of list
39
- # print('response-->>'+str(response))
40
- return response, history
41
-
42
-
43
- gr.Interface(
44
- fn=predict,
45
- title=title,
46
- description=description,
47
- examples=examples,
48
- inputs=["text", "state"],
49
- outputs=["chatbot", "state"],
50
- theme="ParityError/Anime",
51
- ).launch()
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+ import torch
4
+
5
+ title = "EZChat"
6
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT-medium)"
7
+ examples = [["How are you?"]]
8
+
9
+ # Set the padding token to be used and initialize the model
10
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
11
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
12
+ tokenizer.add_special_tokens({'pad_token': '[EOS]'})
13
+ tokenizer.pad_token = tokenizer.eos_token
14
+
15
+ #predict
16
+ def predict(input, history=[]):
17
+ # tokenize the new input sentence
18
+ new_user_input_ids = tokenizer.encode(
19
+ input + tokenizer.eos_token, return_tensors="pt"
20
+ )
21
+
22
+ # append the new user input tokens to the chat history
23
+ bot_input_ids = torch.cat([torch.tensor(history), new_user_input_ids], dim=-1) if history else new_user_input_ids
24
+
25
+ # generate a response
26
+ chat_history_ids = model.generate(
27
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
28
+ )
29
+
30
+ # convert the tokens to text, and then split the responses into lines
31
+ response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
32
+
33
+ return response, chat_history_ids.tolist()[0]
34
+
35
+
36
+ iface = gr.Interface(
37
+ fn=predict,
38
+ title=title,
39
+ description=description,
40
+ examples=examples,
41
+ inputs=["text", gr.inputs.Slider(0, 4000, default=2000, label='Chat History')],
42
+ outputs=["text", "text"],
43
+ theme="ParityError/Anime",
44
+ )
45
+
46
+ iface.launch()