Tonic commited on
Commit
d483f6d
1 Parent(s): cb71bf6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -39
app.py CHANGED
@@ -2,48 +2,46 @@ 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
  tokenizer.padding_side = 'left'
12
- tokenizer.add_special_tokens({'pad_token': '[EOS]'})
13
- tokenizer.pad_token = tokenizer.eos_token
14
 
15
- # Model
16
- model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
17
-
18
- #predict
19
- def predict(input, history=[]):
20
- # tokenize the new input sentence
21
- new_user_input_ids = tokenizer.encode(
22
- input + tokenizer.eos_token, padding=True, truncation=True, return_tensors="pt"
23
- )
24
-
25
- # append the new user input tokens to the chat history
26
- bot_input_ids = torch.cat([torch.tensor(history), new_user_input_ids], dim=-1) if history else new_user_input_ids
27
-
28
- # generate a response
29
- chat_history_ids = model.generate(
30
- bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
31
- )
32
-
33
- # convert the tokens to text, and then split the responses into lines
34
- response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
35
-
36
- return response, chat_history_ids.tolist()[0]
37
-
38
-
39
- iface = gr.Interface(
40
- fn=predict,
41
- title=title,
42
- description=description,
43
- examples=examples,
44
- inputs=["text", gr.inputs.Slider(0, 4000, default=2000, label='Chat History')],
45
- outputs=["text", "text"],
46
- theme="ParityError/Anime",
47
- )
48
-
49
- iface.launch()
 
2
  import gradio as gr
3
  import torch
4
 
5
+ title = "👋🏻Welcome to Tonic's EZ Chat🚀"
6
+ description = "You can use this Space to test out the current model (DialoGPT-medium) or duplicate this Space and use it for anyother model on 🤗HuggingFace."
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
  tokenizer.padding_side = 'left'
 
 
12
 
13
+ from transformers import AutoModelForCausalLM, AutoTokenizer
14
+ import gradio as gr
15
+ import torch
16
+
17
+ title = "👋🏻Welcome to Tonic's EZ Chat🚀"
18
+ description = "You can use this Space to test out the current model (DialoGPT-medium) or duplicate this Space and use it for any other model on 🤗HuggingFace. Join me on [Discord](https://discord.gg/fpEPNZGsbt) to build together."
19
+ examples = [["How are you?"]]
20
+
21
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
22
+ tokenizer.padding_side = 'left'
23
+
24
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
25
+
26
+ def predict(input, history=[]):
27
+ new_user_input_ids = tokenizer.encode(input, return_tensors="pt")
28
+
29
+ bot_input_ids = torch.cat([torch.tensor(history), new_user_input_ids], dim=-1) if history else new_user_input_ids
30
+
31
+ chat_history_ids = model.generate(bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id)
32
+
33
+ response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
34
+
35
+ return response
36
+
37
+ iface = gr.Interface(
38
+ fn=predict,
39
+ title=title,
40
+ description=description,
41
+ examples=examples,
42
+ inputs="text",
43
+ outputs="text",
44
+ theme="ParityError/Anime",
45
+ )
46
+
47
+ iface.launch()