Tonic commited on
Commit
d0dbbcc
·
1 Parent(s): 5debb29

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -26
app.py CHANGED
@@ -2,46 +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 Qwen's 7B-Chat"
7
  examples = [["How are you?"]]
8
 
9
- tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True)
10
- model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen-7B-Chat", trust_remote_code=True).eval()
11
-
12
- history = [] # Initialize chat history
13
 
14
- def predict(input, history=history):
15
- if input is not None and tokenizer.eos_token is not None:
16
- combined_input = input + tokenizer.eos_token
17
- new_user_input_ids = tokenizer.encode(combined_input, return_tensors="pt")
18
 
19
- # Append the new user input tokens to the chat history
20
- bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
21
 
22
- # Generate a response
23
- generated_response_ids = model.generate(
24
- bot_input_ids, max_length=20, pad_token_id=tokenizer.eos_token_id
25
- )
 
26
 
27
- # Convert the generated response tokens to text
28
- response = tokenizer.decode(generated_response_ids[0], skip_special_tokens=True)
29
 
30
- # Append the user input and generated response to the chat history
31
- history.extend(new_user_input_ids[0].tolist())
32
- history.extend(generated_response_ids[0].tolist())
 
33
 
34
- return response, history
 
 
 
 
 
 
 
35
 
36
- else:
37
- print("Input or eos_token is None. Cannot concatenate.")
38
 
39
  gr.Interface(
40
  fn=predict,
41
  title=title,
42
  description=description,
43
  examples=examples,
44
- inputs=["text", "text"],
45
- outputs=["text", "text"],
46
  theme="ParityError/Anime",
47
- ).launch()
 
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)"
8
  examples = [["How are you?"]]
9
 
 
 
 
 
10
 
11
+ tokenizer = AutoTokenizer.from_pretrained("PygmalionAI/pygmalion-2-7b")
12
+ model = AutoModelForCausalLM.from_pretrained("PygmalionAI/pygmalion-2-7b")
 
 
13
 
 
 
14
 
15
+ def predict(input, history=[]):
16
+ # tokenize the new input sentence
17
+ new_user_input_ids = tokenizer.encode(
18
+ input + tokenizer.eos_token, return_tensors="pt"
19
+ )
20
 
21
+ # append the new user input tokens to the chat history
22
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
23
 
24
+ # generate a response
25
+ history = model.generate(
26
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
27
+ ).tolist()
28
 
29
+ # convert the tokens to text, and then split the responses into lines
30
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
31
+ # print('decoded_response-->>'+str(response))
32
+ response = [
33
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
34
+ ] # convert to tuples of list
35
+ # print('response-->>'+str(response))
36
+ return response, history
37
 
 
 
38
 
39
  gr.Interface(
40
  fn=predict,
41
  title=title,
42
  description=description,
43
  examples=examples,
44
+ inputs=["text", "state"],
45
+ outputs=["chatbot", "state"],
46
  theme="ParityError/Anime",
47
+ ).launch()