Tonic commited on
Commit
5acdf7e
1 Parent(s): aae1bd9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -24
app.py CHANGED
@@ -1,30 +1,29 @@
1
- from transformers import AutoModelForCausalLM, AutoTokenizer
2
- import gradio as gr
3
- import torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
6
- tokenizer.padding_side = 'left'
7
- model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
8
-
9
- class ChatBot:
10
- def __init__(self):
11
- self.history = []
12
-
13
- def predict(self, input):
14
- new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
15
- flat_history = [item for sublist in self.history for item in sublist]
16
- flat_history_tensor = torch.tensor(flat_history).unsqueeze(dim=0) # convert list to 2-D tensor
17
- bot_input_ids = torch.cat([flat_history_tensor, new_user_input_ids], dim=-1) if self.history else new_user_input_ids
18
- chat_history_ids = model.generate(bot_input_ids, max_length=2000, pad_token_id=tokenizer.eos_token_id)
19
- self.history.append(chat_history_ids[:, bot_input_ids.shape[-1]:].tolist()[0])
20
- response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
21
- return response
22
-
23
-
24
- bot = ChatBot()
25
 
26
  title = "👋🏻Welcome to Tonic's EZ Chat🚀"
27
- 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."
28
  examples = [["How are you?"]]
29
 
30
  iface = gr.Interface(
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+ import torch
4
+
5
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
6
+ tokenizer.padding_side = 'left'
7
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
8
+
9
+ class ChatBot:
10
+ def __init__(self):
11
+ self.history = []
12
+
13
+ def predict(self, input):
14
+ new_user_input_ids = tokenizer.encode(input + tokenizer.eos_token, return_tensors="pt")
15
+ flat_history = [item for sublist in self.history for item in sublist]
16
+ flat_history_tensor = torch.tensor(flat_history).unsqueeze(dim=0) # convert list to 2-D tensor
17
+ bot_input_ids = torch.cat([flat_history_tensor, new_user_input_ids], dim=-1) if self.history else new_user_input_ids
18
+ chat_history_ids = model.generate(bot_input_ids, max_length=2000, pad_token_id=tokenizer.eos_token_id)
19
+ self.history.append(chat_history_ids[:, bot_input_ids.shape[-1]:].tolist()[0])
20
+ response = tokenizer.decode(chat_history_ids[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
21
+ return response
22
 
23
+ bot = ChatBot()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
25
  title = "👋🏻Welcome to Tonic's EZ Chat🚀"
26
+ 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."
27
  examples = [["How are you?"]]
28
 
29
  iface = gr.Interface(