sarasad commited on
Commit
c8201c6
·
1 Parent(s): 965e1be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -1,26 +1,49 @@
 
 
 
 
 
 
1
 
2
-
 
 
3
  from arabert.preprocess import ArabertPreprocessor
 
 
 
 
4
  from transformers import EncoderDecoderModel, AutoTokenizer
5
  tokenizer = AutoTokenizer.from_pretrained("tareknaous/bert2bert-empathetic-response-msa")
6
  model = EncoderDecoderModel.from_pretrained("tareknaous/bert2bert-empathetic-response-msa")
7
- model.to("cuda")
8
  model.eval()
9
- arabert_prep = ArabertPreprocessor(model_name="bert-base-arabert", keep_emojis=False)
10
- def generate_response(text):
11
  text_clean = arabert_prep.preprocess(text)
12
  inputs = tokenizer.encode_plus(text_clean,return_tensors='pt')
13
- outputs = model.generate(input_ids = inputs.input_ids.to("cuda"),
14
- attention_mask = inputs.attention_mask.to("cuda"),
15
  do_sample = True,
16
- min_length=10,
17
- top_k = 0,
18
- top_p = 0.9,
19
- temperature = 0.5)
20
  preds = tokenizer.batch_decode(outputs)
21
  response = str(preds)
22
  response = response.replace("\'", '')
23
  response = response.replace("[[CLS]", '')
24
  response = response.replace("[SEP]]", '')
25
  response = str(arabert_prep.desegment(response))
26
- return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Import transformers and gradio
2
+ import transformers
3
+ import gradio as gr
4
+ import git
5
+ import os
6
+ os.system("pip install --upgrade pip")
7
 
8
+ #Load arabert preprocessor
9
+ import git
10
+ git.Git("arabert").clone("https://github.com/aub-mind/arabert")
11
  from arabert.preprocess import ArabertPreprocessor
12
+ arabert_prep = ArabertPreprocessor(model_name="bert-base-arabert", keep_emojis=False)
13
+
14
+
15
+ #Load Model
16
  from transformers import EncoderDecoderModel, AutoTokenizer
17
  tokenizer = AutoTokenizer.from_pretrained("tareknaous/bert2bert-empathetic-response-msa")
18
  model = EncoderDecoderModel.from_pretrained("tareknaous/bert2bert-empathetic-response-msa")
 
19
  model.eval()
20
+
21
+ def generate_response(text, minimum_length, p, temperature):
22
  text_clean = arabert_prep.preprocess(text)
23
  inputs = tokenizer.encode_plus(text_clean,return_tensors='pt')
24
+ outputs = model.generate(input_ids = inputs.input_ids,
25
+ attention_mask = inputs.attention_mask,
26
  do_sample = True,
27
+ min_length=minimum_length,
28
+ top_p = p,
29
+ temperature = temperature)
 
30
  preds = tokenizer.batch_decode(outputs)
31
  response = str(preds)
32
  response = response.replace("\'", '')
33
  response = response.replace("[[CLS]", '')
34
  response = response.replace("[SEP]]", '')
35
  response = str(arabert_prep.desegment(response))
36
+ return response
37
+
38
+ title = 'Empathetic Response Generation in Arabic'
39
+ description = 'This demo is for a BERT2BERT model trained for single-turn open-domain empathetic dialogue response generation in Modern Standard Arabic'
40
+ gr.Interface(fn=generate_response,
41
+ inputs=[
42
+ gr.inputs.Textbox(),
43
+ gr.inputs.Slider(5, 20, step=1, label='Minimum Output Length'),
44
+ gr.inputs.Slider(0.7, 1, step=0.1, label='Top-P'),
45
+ gr.inputs.Slider(1, 3, step=0.1, label='Temperature'),
46
+ ],
47
+ outputs="text",
48
+ title=title,
49
+ description=description).launch()