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