thenHung commited on
Commit
30368ad
·
1 Parent(s): 1910006

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -28
app.py CHANGED
@@ -13,41 +13,58 @@ examples = [["How are you?"]]
13
  tokenizer = AutoTokenizer.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
14
  model = AutoModelForCausalLM.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
15
 
16
- def generate_response(input_text, chat_history=[]):
17
- # Tokenize the new input sentence
18
- new_user_input_ids = tokenizer.encode(input_text + tokenizer.eos_token, return_tensors="pt")
19
 
20
- # Append the new user input tokens to the chat history
21
- bot_input_ids = torch.cat([torch.tensor(chat_history), new_user_input_ids], dim=-1)
22
 
23
- # Generate a response
24
- chat_output = model.generate(bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id)
25
 
26
- # Decode the response tokens into text
27
- response = tokenizer.decode(chat_output[:, bot_input_ids.shape[-1]:][0], skip_special_tokens=True)
28
 
29
- return response
 
 
 
 
30
 
31
- def chatbot_interface(input_text):
32
- # Generate response based on input text and chat history
33
- response = generate_response(input_text, chat_history)
34
 
35
- # Append the input and response to the chat history
36
- chat_history.append(tokenizer.encode(input_text + response))
 
 
37
 
38
- return response
 
 
 
 
 
 
39
 
40
- chat_history = [] # Initialize chat history
41
 
42
- iface = gr.Interface(
43
- fn=chatbot_interface,
44
- inputs=gr.inputs.Textbox(lines=2, label="Chat"),
45
- outputs=gr.outputs.Textbox(label="Response"),
46
- layout="vertical",
47
- title=title,
48
- description=description,
49
- examples=examples,
50
- theme="london"
51
- )
52
 
53
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  tokenizer = AutoTokenizer.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
14
  model = AutoModelForCausalLM.from_pretrained("rinna/vicuna-13b-delta-finetuned-langchain-MRKL")
15
 
16
+ # tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
17
+ # model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
 
18
 
 
 
19
 
20
+ def user(message, history):
21
+ return "", history + [[message, None]]
22
 
 
 
23
 
24
+ def bot(history):
25
+ user_message = history[-1][0]
26
+ new_user_input_ids = tokenizer.encode(
27
+ user_message + tokenizer.eos_token, return_tensors="pt"
28
+ )
29
 
30
+ # append the new user input tokens to the chat history
31
+ bot_input_ids = torch.cat([torch.LongTensor([]), new_user_input_ids], dim=-1)
 
32
 
33
+ # generate a response
34
+ response = model.generate(
35
+ bot_input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id
36
+ ).tolist()
37
 
38
+ # convert the tokens to text, and then split the responses into lines
39
+ response = tokenizer.decode(response[0]).split("<|endoftext|>")
40
+ response = [
41
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
42
+ ] # convert to tuples of list
43
+ history[-1] = response[0]
44
+ return history
45
 
 
46
 
47
+ with gr.Blocks() as demo:
48
+ chatbot = gr.Chatbot()
49
+ msg = gr.Textbox()
50
+ clear = gr.Button("Clear")
51
+
52
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
53
+ bot, chatbot, chatbot
54
+ )
55
+ clear.click(lambda: None, None, chatbot, queue=False)
 
56
 
57
+ demo.launch()
58
+
59
+ # iface = gr.Interface(
60
+ # fn=chatbot_interface,
61
+ # inputs=gr.inputs.Textbox(lines=2, label="Chat"),
62
+ # outputs=gr.outputs.Textbox(label="Response"),
63
+ # layout="vertical",
64
+ # title=title,
65
+ # description=description,
66
+ # examples=examples,
67
+ # theme="london"
68
+ # )
69
+
70
+ # iface.launch()