sarves commited on
Commit
937b0c7
1 Parent(s): 582328f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+ import gradio as gr
5
+ import torch
6
+
7
+
8
+ title = "🤖AI ChatBot"
9
+ description = "A State-of-the-Art Large-scale Pretrained Response generation model (DialoGPT)"
10
+ examples = [["How are you?"]]
11
+
12
+
13
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-large")
14
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
15
+
16
+
17
+ def predict(input, history=[]):
18
+ # tokenize the new input sentence
19
+ new_user_input_ids = tokenizer.encode(
20
+ input + tokenizer.eos_token, return_tensors="pt"
21
+ )
22
+
23
+ # append the new user input tokens to the chat history
24
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
25
+
26
+ # generate a response
27
+ history = model.generate(
28
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
29
+ ).tolist()
30
+
31
+ # convert the tokens to text, and then split the responses into lines
32
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
33
+ # print('decoded_response-->>'+str(response))
34
+ response = [
35
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
36
+ ] # convert to tuples of list
37
+ # print('response-->>'+str(response))
38
+ return response, history
39
+
40
+
41
+ gr.Interface(
42
+ fn=predict,
43
+ title=title,
44
+ description=description,
45
+ examples=examples,
46
+ inputs=["text", "state"],
47
+ outputs=["chatbot", "state"],
48
+ theme="finlaymacklon/boxy_violet",
49
+ ).launch()