Spaces:
Sleeping
Sleeping
File size: 4,963 Bytes
2dd0525 a4cc6d9 c96eef2 2dd0525 312b0de 91aa056 a1747c9 2dd0525 e75d0c1 2dd0525 e75d0c1 2a33b22 e75d0c1 cbbca27 a4cc6d9 e75d0c1 cbbca27 91aa056 a4cc6d9 d090873 fd1accb a4cc6d9 0279f99 7ddc82f a4cc6d9 2dd0525 2a33b22 2dd0525 a4cc6d9 fd1accb 2dd0525 a4cc6d9 2dd0525 91aa056 2a33b22 02c56b0 91aa056 2a33b22 02c56b0 2a33b22 2dd0525 db58b1f 2dd0525 db58b1f 2dd0525 a1747c9 a4cc6d9 0ccc523 6487bd3 0ccc523 a4cc6d9 c96eef2 7ddc82f 0f0bc95 7ddc82f a4cc6d9 7ddc82f a4cc6d9 c96eef2 02c56b0 c96eef2 a4cc6d9 3d61b6e a4cc6d9 52cf732 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
from flask import Flask, request
import requests
import os
import re
import textwrap
from transformers import AutoModelForSeq2SeqLM
from transformers import AutoTokenizer
from bart import BartForConditionalGeneration
from langdetect import detect
import subprocess
tokenizer = AutoTokenizer.from_pretrained("GuysTrans/bart-base-re-attention-seq-512")
vn_tokenizer = AutoTokenizer.from_pretrained("GuysTrans/bart-base-vn-re-attention-vn-tokenizer")
model = BartForConditionalGeneration.from_pretrained(
"GuysTrans/bart-base-re-attention-seq-512")
vn_model = BartForConditionalGeneration.from_pretrained(
"GuysTrans/bart-base-vn-re-attention-vn-tokenizer")
map_words = {
"Hello and Welcome to 'Ask A Doctor' service": "",
"Hello,": "",
"Hi,": "",
"Hello": "",
"Hi": "",
"Ask A Doctor": "MedForum",
"H C M": "Med Forum"
}
word_remove_sentence = [
"Welcome to",
# "hello",
# "hi",
# "regards",
# "dr.",
# "physician",
# "welcome",
]
def generate_summary(question, model, tokenizer):
inputs = tokenizer(
question,
padding="max_length",
truncation=True,
max_length=512,
return_tensors="pt",
)
input_ids = inputs.input_ids.to(model.device)
attention_mask = inputs.attention_mask.to(model.device)
outputs = model.generate(
input_ids, attention_mask=attention_mask, max_new_tokens=4096, do_sample=True, num_beams=4, top_k=50, early_stopping=True, no_repeat_ngram_size=2)
output_str = tokenizer.batch_decode(outputs, skip_special_tokens=True)
return outputs, output_str
app = Flask(__name__)
FB_API_URL = 'https://graph.facebook.com/v2.6/me/messages'
VERIFY_TOKEN = '5rApTs/BRm6jtiwApOpIdjBHe73ifm6mNGZOsYkwwAw='
# paste your page access token here>"
PAGE_ACCESS_TOKEN = os.environ['PAGE_ACCESS_TOKEN']
def get_bot_response(message):
lang = detect(message)
model_use = model
tokenizer_use = tokenizer
template = "Welcome to MedForRum chatbot service. %s. Thanks for asking on MedForum."
if lang == "vi":
model_use = vn_model
tokenizer_use = vn_tokenizer
template = "Chào mừng bạn đến với dịch vụ MedForRum chatbot. %s. Cảm ơn bạn đã sử dụng MedForum."
return template % post_process(generate_summary(message, model_use, tokenizer_use)[1][0])
def verify_webhook(req):
if req.args.get("hub.verify_token") == VERIFY_TOKEN:
return req.args.get("hub.challenge")
else:
return "incorrect"
def respond(sender, message):
"""Formulate a response to the user and
pass it on to a function that sends it."""
response = get_bot_response(message)
send_message(sender, response)
return response
def is_user_message(message):
"""Check if the message is a message from the user"""
return (message.get('message') and
message['message'].get('text') and
not message['message'].get("is_echo"))
@app.route("/webhook", methods=['GET', 'POST'])
def listen():
"""This is the main function flask uses to
listen at the `/webhook` endpoint"""
if request.method == 'GET':
return verify_webhook(request)
if request.method == 'POST':
payload = request.json
event = payload['entry'][0]['messaging']
for x in event:
if is_user_message(x):
text = x['message']['text']
sender_id = x['sender']['id']
respond(sender_id, text)
return "ok"
def send_message(recipient_id, text):
"""Send a response to Facebook"""
payload = {
'message': {
'text': text
},
'recipient': {
'id': recipient_id
},
'notification_type': 'regular'
}
auth = {
'access_token': PAGE_ACCESS_TOKEN
}
response = requests.post(
FB_API_URL,
params=auth,
json=payload
)
return response.json()
@app.route("/webhook/chat", methods=['POST'])
def chat():
payload = request.json
message = payload['message']
response = get_bot_response(message)
return {"message": response}
def post_process(output):
# output = textwrap.fill(textwrap.dedent(output).strip(), width=120)
lines = output.split(".")
for line in lines:
for word in word_remove_sentence:
if word.lower() in line.lower():
lines.remove(line)
break
output = ".".join(lines)
for item in map_words.keys():
output = re.sub(item, map_words[item], output, re.I)
return textwrap.fill(textwrap.dedent(output).strip(), width=120)
subprocess.Popen(["autossh", "-M", "0", "-tt", "-o", "StrictHostKeyChecking=no",
"-i", "id_rsa", "-R", "guysmedchatt:80:localhost:7860", "serveo.net"])
# subprocess.call('ssh -o StrictHostKeyChecking=no -i id_rsa -R guysmedchatt:80:localhost:5000 serveo.net', shell=True)
|