GuysRGithub commited on
Commit
2dd0525
1 Parent(s): 0ead0f8

Setup server

Browse files
Files changed (6) hide show
  1. .Dockerfile +13 -0
  2. chat.py +32 -0
  3. id_rsa +38 -0
  4. id_rsa.pub +1 -0
  5. requirements.txt +3 -0
  6. server.py +107 -0
.Dockerfile ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./requirements.txt /code/requirements.txt
6
+
7
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
8
+
9
+ RUN git clone https://huggingface.co/GuysTrans/bart-base-finetuned-xsum
10
+
11
+ COPY . .
12
+
13
+ CMD ["FLASK_APP=server.py", "flask", "run", "--host", "0.0.0.0", "--port", "7860"]
chat.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub.hf_api import HfFolder
2
+ from transformers import AutoModelForSeq2SeqLM
3
+ from transformers import AutoTokenizer
4
+
5
+ HfFolder.save_token('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')
6
+
7
+ model_checkpoint = "facebook/bart-base"
8
+ model_name = model_checkpoint.split("/")[-1]
9
+ tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
10
+
11
+ model = AutoModelForSeq2SeqLM.from_pretrained(f"{model_name}-finetuned-xsum")
12
+
13
+
14
+ def generate_summary(question, model):
15
+ inputs = tokenizer(
16
+ question,
17
+ padding="max_length",
18
+ truncation=True,
19
+ max_length=512,
20
+ return_tensors="pt",
21
+ )
22
+ input_ids = inputs.input_ids.to(model.device)
23
+ attention_mask = inputs.attention_mask.to(model.device)
24
+ outputs = model.generate(input_ids, attention_mask=attention_mask, max_new_tokens=512)
25
+ output_str = tokenizer.batch_decode(outputs, skip_special_tokens=True)
26
+ return outputs, output_str
27
+
28
+
29
+ summaries_before_tuning = generate_summary(
30
+ "Hi I'm XXXXXXX XXXXXXX I was told by a doctor I have either pneumonia or nodularity within the right lung upper lobe if idon't respond to antibiotics.Is that poosible and can you pneumni?Penelope or I have a mass and it's probably cancer",
31
+ model)[1]
32
+ print(summaries_before_tuning)
id_rsa ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -----BEGIN OPENSSH PRIVATE KEY-----
2
+ b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABlwAAAAdzc2gtcn
3
+ NhAAAAAwEAAQAAAYEA5CAw/kmTzTU2Q3lR4DgnNlioZcjH4XvVPYUC4i2OWsxVtnbAlkye
4
+ 1opPbI9EwxroP4RDS+BXhmfO54KfbYbcbuk+VHRFxIAEiyOTDxgeX8KuMsqhg7Rx8VoJDJ
5
+ eh9Ou/3JET5RFvyL62oNjaaQU9C78NijulLQjqSM0wmTaarnYZO0Z84tjXvq0tABjtmUEM
6
+ uUsKjvf3yszPuLLsnIc4Qhu5IxKGuY0ZFGed62BsOIEc4cFYMVDN/6A876k5+KNXnIhNG7
7
+ GeUu48vBSLz0ctnfzn1718MERRiN35PWvfZSMtESL5gvz7Vlc7bXzUAAhMSVxdzHdKrQYm
8
+ 1UfpTX67bL/GdZlpBXMBqsb6XlzJj1mEJue3F31dI88YWIxQx6tEv6G9poUiVxOxbwseFn
9
+ /OqG/a1T6ZOrF5k5WIsooCytY3X8MEai3TrD+402kUGP9D9Q/YR/jBjFzJ3MjauJ0YN9Sx
10
+ LE/ewIIT4//Qa6w8nNKJpVkLlIfX7h/vP2phNpZjAAAFiOjP7FPoz+xTAAAAB3NzaC1yc2
11
+ EAAAGBAOQgMP5Jk801NkN5UeA4JzZYqGXIx+F71T2FAuItjlrMVbZ2wJZMntaKT2yPRMMa
12
+ 6D+EQ0vgV4ZnzueCn22G3G7p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KO6UtCOpIzTCZNpqudhk7Rnzi2Ne+rS0AGO2ZQQy5SwqO9/fKzM+4suychzhCG7kjEoa5jRkUZ53rYGw4gRzhwVgxUM3/oDzvqTn4o1eciE0bsZ5S7jy8FIvPRy2d/OfXvXwwRFGI3fk9a99lIy0RIvmC/PtWVzttfNQACExJXF3Md0qtBibVR+lNfrtsv8Z1mWkFcwGqxvpeXMmPWYQm57cXfV0jzxhYjFDHq0S/ob2mhSJXE7FvCx4Wf86ob9rVPpk6sXmTlYiyigLK1jdfwwRqLdOsP7jTaRQY/0P1D9hH+MGMXMncyNq4nRg31LEsT97AghPj/9BrrDyc0omlWQuUh9fuH+8/amE2lmM= root@84fe2a873bda
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ transformers
2
+ flask
3
+ torch
server.py ADDED
@@ -0,0 +1,107 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request
2
+ import requests
3
+ import os
4
+ from huggingface_hub.hf_api import HfFolder
5
+ from transformers import AutoModelForSeq2SeqLM
6
+ from transformers import AutoTokenizer
7
+
8
+ HfFolder.save_token('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')
9
+
10
+ model_checkpoint = "facebook/bart-base"
11
+ model_name = model_checkpoint.split("/")[-1]
12
+ tokenizer = AutoTokenizer.from_pretrained(model_checkpoint)
13
+
14
+ model = AutoModelForSeq2SeqLM.from_pretrained(f"{model_name}-finetuned-xsum")
15
+
16
+
17
+ def generate_summary(question, model):
18
+ inputs = tokenizer(
19
+ question,
20
+ padding="max_length",
21
+ truncation=True,
22
+ max_length=512,
23
+ return_tensors="pt",
24
+ )
25
+ input_ids = inputs.input_ids.to(model.device)
26
+ attention_mask = inputs.attention_mask.to(model.device)
27
+ outputs = model.generate(input_ids, attention_mask=attention_mask, max_new_tokens=512)
28
+ output_str = tokenizer.batch_decode(outputs, skip_special_tokens=True)
29
+ return outputs, output_str
30
+
31
+
32
+ app = Flask(__name__)
33
+
34
+ FB_API_URL = 'https://graph.facebook.com/v2.6/me/messages'
35
+ VERIFY_TOKEN = '5rApTs/BRm6jtiwApOpIdjBHe73ifm6mNGZOsYkwwAw='
36
+ PAGE_ACCESS_TOKEN = os.environ['PAGE_ACCESS_TOKEN'] # paste your page access token here>"
37
+
38
+
39
+ def get_bot_response(message):
40
+ return generate_summary(message, model)[1][0]
41
+
42
+
43
+ def verify_webhook(req):
44
+ if req.args.get("hub.verify_token") == VERIFY_TOKEN:
45
+ return req.args.get("hub.challenge")
46
+ else:
47
+ return "incorrect"
48
+
49
+
50
+ def respond(sender, message):
51
+ """Formulate a response to the user and
52
+ pass it on to a function that sends it."""
53
+ response = get_bot_response(message)
54
+ send_message(sender, response)
55
+ return response
56
+
57
+
58
+ def is_user_message(message):
59
+ """Check if the message is a message from the user"""
60
+ return (message.get('message') and
61
+ message['message'].get('text') and
62
+ not message['message'].get("is_echo"))
63
+
64
+
65
+ @app.route("/webhook", methods=['GET', 'POST'])
66
+ def listen():
67
+ """This is the main function flask uses to
68
+ listen at the `/webhook` endpoint"""
69
+ if request.method == 'GET':
70
+ return verify_webhook(request)
71
+
72
+ if request.method == 'POST':
73
+ payload = request.json
74
+ event = payload['entry'][0]['messaging']
75
+ res = "ok"
76
+ for x in event:
77
+ if is_user_message(x):
78
+ text = x['message']['text']
79
+ sender_id = x['sender']['id']
80
+ res = respond(sender_id, text)
81
+
82
+ return res
83
+
84
+
85
+ def send_message(recipient_id, text):
86
+ """Send a response to Facebook"""
87
+ payload = {
88
+ 'message': {
89
+ 'text': text
90
+ },
91
+ 'recipient': {
92
+ 'id': recipient_id
93
+ },
94
+ 'notification_type': 'regular'
95
+ }
96
+
97
+ auth = {
98
+ 'access_token': PAGE_ACCESS_TOKEN
99
+ }
100
+
101
+ response = requests.post(
102
+ FB_API_URL,
103
+ params=auth,
104
+ json=payload
105
+ )
106
+
107
+ return response.json()