Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,49 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
# Add user input to the conversation history
|
9 |
-
gr.Interface.cache["conversation"].append("User: " + message)
|
10 |
-
|
11 |
-
# Define the bot's responses based on user input
|
12 |
-
if message.lower() == "hello":
|
13 |
-
response = "Hi, how can I help you?"
|
14 |
-
elif message.lower() == "how are you?":
|
15 |
-
response = "I'm doing well, thank you!"
|
16 |
-
elif message.lower() == "goodbye":
|
17 |
-
response = "Goodbye! Have a great day."
|
18 |
-
else:
|
19 |
-
response = "Sorry, I didn't understand that. Can you please rephrase?"
|
20 |
-
|
21 |
-
# Add bot response to the conversation history
|
22 |
-
gradio.Interface.cache["conversation"].append("Bot: " + response)
|
23 |
-
|
24 |
-
# Return the bot's response
|
25 |
-
return response
|
26 |
|
27 |
-
iface = gr.Interface(fn=chatbot, inputs="text", outputs="text")
|
28 |
|
29 |
-
#
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
<button id="submit_button">Send</button>
|
35 |
-
</div>
|
36 |
-
<div id="conversation" style="margin-top: 10px;"></div>
|
37 |
-
</div>
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
submitButton.addEventListener("click", function() {
|
56 |
-
var inputText = document.getElementById("input_text").value;
|
57 |
-
if (inputText) {
|
58 |
-
gradio.interfaceInstances[0].input_interfaces[0].setValue(inputText);
|
59 |
-
gradio.interfaceInstances[0].submit();
|
60 |
-
}
|
61 |
-
});
|
62 |
-
|
63 |
-
// Update the conversation display on page load
|
64 |
-
window.onload = function() {
|
65 |
-
updateConversation();
|
66 |
-
}
|
67 |
-
</script>
|
68 |
-
"""
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import transformers
|
2 |
import gradio as gr
|
3 |
+
import git
|
4 |
+
import os
|
5 |
+
os.system("pip install --upgrade pip")
|
6 |
|
7 |
+
#Load arabert preprocessor
|
8 |
+
import git
|
9 |
+
git.Git("arabert").clone("https://github.com/aub-mind/arabert")
|
10 |
+
from arabert.preprocess import ArabertPreprocessor
|
11 |
+
arabert_prep = ArabertPreprocessor(model_name="bert-base-arabert", keep_emojis=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
|
|
13 |
|
14 |
+
#Load Model
|
15 |
+
from transformers import EncoderDecoderModel, AutoTokenizer
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained("tareknaous/bert2bert-empathetic-response-msa")
|
17 |
+
model = EncoderDecoderModel.from_pretrained("tareknaous/bert2bert-empathetic-response-msa")
|
18 |
+
model.eval()
|
|
|
|
|
|
|
|
|
19 |
|
20 |
+
def generate_response(text, minimum_length, p, temperature):
|
21 |
+
text_clean = arabert_prep.preprocess(text)
|
22 |
+
inputs = tokenizer.encode_plus(text_clean,return_tensors='pt')
|
23 |
+
outputs = model.generate(input_ids = inputs.input_ids,
|
24 |
+
attention_mask = inputs.attention_mask,
|
25 |
+
do_sample = True,
|
26 |
+
min_length=minimum_length,
|
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 = 'Empathetic Response Generation in Arabic'
|
38 |
+
# description = 'This demo is for a BERT2BERT model trained for single-turn open-domain empathetic dialogue response generation in Modern Standard Arabic'
|
39 |
+
|
40 |
+
with gr.Blocks() as demo:
|
41 |
+
gr.Markdown("Empathetic Response Generation in Arabic")
|
42 |
+
chatbot = gr.Chatbot()
|
43 |
+
output-slider=gr.Slider(5, 20, step=1, label='Minimum Output Length')
|
44 |
+
top-p-slider=gr.Slider(0.7, 1, step=0.1, label='Top-P')
|
45 |
+
temperature-slider=gr.Slider(1, 3, step=0.1, label='Temperature')
|
46 |
+
msg = gr.Textbox()
|
47 |
+
clear = gr.Button("Clear")
|
48 |
+
msg.submit(generate_response, [msg, chatbot,output-slider,top-p-slider,temperature-slider], [msg, chatbot])
|
49 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|