hrishikeshagi commited on
Commit
0e54263
1 Parent(s): caa6b6f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
2
+ import torch
3
+
4
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
5
+ model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/GODEL-v1_1-base-seq2seq")
6
+
7
+ def predict(input, history=[]):
8
+
9
+ instruction = 'Instruction: given a dialog context, you need to response empathically'
10
+
11
+ knowledge = ' '
12
+
13
+ s = list(sum(history, ()))
14
+
15
+ s.append(input)
16
+
17
+ #print(s)
18
+
19
+ dialog = ' EOS ' .join(s)
20
+
21
+ #print(dialog)
22
+
23
+ query = f"{instruction} [CONTEXT] {dialog} {knowledge}"
24
+
25
+ top_p = 0.9
26
+ min_length = 8
27
+ max_length = 64
28
+
29
+
30
+ # tokenize the new input sentence
31
+ new_user_input_ids = tokenizer.encode(f"{query}", return_tensors='pt')
32
+
33
+
34
+ output = model.generate(new_user_input_ids, min_length=int(
35
+ min_length), max_length=int(max_length), top_p=top_p, do_sample=True).tolist()
36
+
37
+
38
+ response = tokenizer.decode(output[0], skip_special_tokens=True)
39
+
40
+
41
+ history.append((input, response))
42
+
43
+ return history, history
44
+
45
+ import gradio as gr
46
+
47
+
48
+ gr.Interface(fn=predict,
49
+ inputs=["text",'state'],
50
+ outputs=["chatbot",'state']).launch()