shreyaa64 commited on
Commit
e784fdd
1 Parent(s): 47f9454

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForCausalLM, AutoTokenizer
2
+ import gradio as gr
3
+ import torch
4
+
5
+
6
+ title = "????AI ChatBot"
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("microsoft/DialoGPT-large")
12
+ model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-large")
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
+ # append the new user input tokens to the chat history
21
+ bot_input_ids = torch.cat([torch.LongTensor(history), new_user_input_ids], dim=-1)
22
+
23
+ # generate a response
24
+ history = model.generate(
25
+ bot_input_ids, max_length=4000, pad_token_id=tokenizer.eos_token_id
26
+ ).tolist()
27
+
28
+ # convert the tokens to text, and then split the responses into lines
29
+ response = tokenizer.decode(history[0]).split("<|endoftext|>")
30
+ # print('decoded_response-->>'+str(response))
31
+ response = [
32
+ (response[i], response[i + 1]) for i in range(0, len(response) - 1, 2)
33
+ ] # convert to tuples of list
34
+ # print('response-->>'+str(response))
35
+ return response, history
36
+
37
+ gr.Interface(
38
+ fn=predict,
39
+ title=title,
40
+ description=description,
41
+ examples=examples,
42
+ inputs=["text", "state"],
43
+ outputs=["chatbot", "state"],
44
+ theme="finlaymacklon/boxy_violet",
45
+ ).launch()