|
| 1 | +import flask |
| 2 | +import gzip |
| 3 | +import json |
| 4 | +import jwt |
| 5 | +import logging |
| 6 | +import os |
| 7 | +import queue |
| 8 | +import re |
| 9 | +import requests |
| 10 | +import threading |
| 11 | +import time |
| 12 | + |
| 13 | +WD_API_URL = os.getenv('WD_API_URL') |
| 14 | +WD_API_KEY = os.getenv('WD_API_KEY') |
| 15 | +WEBHOOK_SECRET = os.getenv('WEBHOOK_SECRET') |
| 16 | +IBM_CLOUD_API_KEY = os.getenv('IBM_CLOUD_API_KEY') |
| 17 | +WML_ENDPOINT_URL = os.getenv('WML_ENDPOINT_URL', 'https://us-south.ml.cloud.ibm.com') |
| 18 | +WML_INSTANCE_CRN = os.getenv('WML_INSTANCE_CRN') |
| 19 | + |
| 20 | +# Enrichment task queue |
| 21 | +q = queue.Queue() |
| 22 | + |
| 23 | +app = flask.Flask(__name__) |
| 24 | +app.logger.setLevel(logging.INFO) |
| 25 | +app.logger.handlers[0].setFormatter(logging.Formatter('[%(asctime)s] %(levelname)s in %(module)s: %(message)s (%(filename)s:%(lineno)d)')) |
| 26 | + |
| 27 | +def get_iam_token(): |
| 28 | + data = {'grant_type': 'urn:ibm:params:oauth:grant-type:apikey', 'apikey': IBM_CLOUD_API_KEY} |
| 29 | + response = requests.post('https://iam.cloud.ibm.com/identity/token', data=data) |
| 30 | + if response.status_code == 200: |
| 31 | + return response.json()['access_token'] |
| 32 | + else: |
| 33 | + raise Exception('Failed to get IAM token.') |
| 34 | + |
| 35 | +IAM_TOKEN = None |
| 36 | + |
| 37 | +def extract_entities(text): |
| 38 | + global IAM_TOKEN |
| 39 | + if IAM_TOKEN is None: |
| 40 | + IAM_TOKEN = get_iam_token() |
| 41 | + # Prompt |
| 42 | + payload = { |
| 43 | + 'model_id': 'ibm/granite-13b-instruct-v1', |
| 44 | + 'input': f'''Act as a webmaster who must extract structured information from emails. Read the below email and extract and categorize each entity. If no entity is found, output "None". |
| 45 | +
|
| 46 | +Input: |
| 47 | +"Golden Bank is a competitor of Silver Bank in the US" said John Doe. |
| 48 | +
|
| 49 | +Named Entities: |
| 50 | +Golden Bank: company, Silver Bank: company, US: country, John Doe: person |
| 51 | +
|
| 52 | +Input: |
| 53 | +{text} |
| 54 | +
|
| 55 | +Named Entities: |
| 56 | +''', |
| 57 | + 'parameters': { |
| 58 | + 'decoding_method': 'greedy', |
| 59 | + 'max_new_tokens': 50, |
| 60 | + 'min_new_tokens': 1, |
| 61 | + 'stop_sequences': [], |
| 62 | + 'repetition_penalty': 1 |
| 63 | + }, |
| 64 | + 'wml_instance_crn': WML_INSTANCE_CRN |
| 65 | + } |
| 66 | + params = {'version': '2023-05-29'} |
| 67 | + headers = {'Authorization': f'Bearer {IAM_TOKEN}'} |
| 68 | + response = requests.post(f'{WML_ENDPOINT_URL}/ml/v1-beta/generation/text', json=payload, params=params, headers=headers) |
| 69 | + if response.status_code == 200: |
| 70 | + result = response.json()['results'][0]['generated_text'] |
| 71 | + app.logger.info('LLM result: %s', result) |
| 72 | + entities = [] |
| 73 | + if result == 'None': |
| 74 | + # No entity found |
| 75 | + return entities |
| 76 | + for pair in re.split(r',\s*', result): |
| 77 | + text_type = re.split(r':\s*', pair) |
| 78 | + entities.append({'text': text_type[0], 'type': text_type[1]}) |
| 79 | + return entities |
| 80 | + elif response.status_code == 401: |
| 81 | + # Token expired. Re-generate it. |
| 82 | + IAM_TOKEN = get_iam_token() |
| 83 | + return extract_entities(text) |
| 84 | + else: |
| 85 | + raise Exception(f'Failed to generate: {response.text}') |
| 86 | + |
| 87 | +def enrich(doc): |
| 88 | + app.logger.info('doc: %s', doc) |
| 89 | + features_to_send = [] |
| 90 | + for feature in doc['features']: |
| 91 | + # Target 'text' field |
| 92 | + if feature['properties']['field_name'] != 'text': |
| 93 | + continue |
| 94 | + location = feature['location'] |
| 95 | + begin = location['begin'] |
| 96 | + end = location['end'] |
| 97 | + text = doc['artifact'][begin:end] |
| 98 | + try: |
| 99 | + # Entity extraction example |
| 100 | + results = extract_entities(text) |
| 101 | + app.logger.info('entities: %s', results) |
| 102 | + for entity in results: |
| 103 | + entity_text = entity['text'] |
| 104 | + entity_type = entity['type'] |
| 105 | + for matched in re.finditer(re.escape(entity_text), text): |
| 106 | + features_to_send.append( |
| 107 | + { |
| 108 | + 'type': 'annotation', |
| 109 | + 'location': { |
| 110 | + 'begin': matched.start() + begin, |
| 111 | + 'end': matched.end() + begin, |
| 112 | + }, |
| 113 | + 'properties': { |
| 114 | + 'type': 'entities', |
| 115 | + 'confidence': 1.0, |
| 116 | + 'entity_type': entity_type, |
| 117 | + 'entity_text': matched.group(0), |
| 118 | + }, |
| 119 | + } |
| 120 | + ) |
| 121 | + except Exception as e: |
| 122 | + # Notice example |
| 123 | + features_to_send.append( |
| 124 | + { |
| 125 | + 'type': 'notice', |
| 126 | + 'properties': { |
| 127 | + 'description': str(e), |
| 128 | + 'created': round(time.time() * 1000), |
| 129 | + }, |
| 130 | + } |
| 131 | + ) |
| 132 | + app.logger.info('features_to_send: %s', features_to_send) |
| 133 | + return {'document_id': doc['document_id'], 'features': features_to_send} |
| 134 | + |
| 135 | +def enrichment_worker(): |
| 136 | + while True: |
| 137 | + item = q.get() |
| 138 | + version = item['version'] |
| 139 | + data = item['data'] |
| 140 | + project_id = data['project_id'] |
| 141 | + collection_id = data['collection_id'] |
| 142 | + batch_id = data['batch_id'] |
| 143 | + batch_api = f'{WD_API_URL}/v2/projects/{project_id}/collections/{collection_id}/batches/{batch_id}' |
| 144 | + params = {'version': version} |
| 145 | + auth = ('apikey', WD_API_KEY) |
| 146 | + headers = {'Accept-Encoding': 'gzip'} |
| 147 | + try: |
| 148 | + # Get documents from WD |
| 149 | + response = requests.get(batch_api, params=params, auth=auth, headers=headers, stream=True) |
| 150 | + status_code = response.status_code |
| 151 | + app.logger.info('Pulled a batch: %s, status: %d', batch_id, status_code) |
| 152 | + if status_code == 200: |
| 153 | + # Annotate documents |
| 154 | + enriched_docs = [enrich(json.loads(line)) for line in response.iter_lines()] |
| 155 | + files = { |
| 156 | + 'file': ( |
| 157 | + 'data.ndjson.gz', |
| 158 | + gzip.compress( |
| 159 | + '\n'.join( |
| 160 | + [json.dumps(enriched_doc) for enriched_doc in enriched_docs] |
| 161 | + ).encode('utf-8') |
| 162 | + ), |
| 163 | + 'application/x-ndjson' |
| 164 | + ) |
| 165 | + } |
| 166 | + # Upload annotated documents |
| 167 | + response = requests.post(batch_api, params=params, files=files, auth=auth) |
| 168 | + status_code = response.status_code |
| 169 | + app.logger.info('Pushed a batch: %s, status: %d', batch_id, status_code) |
| 170 | + except Exception as e: |
| 171 | + app.logger.error('An error occurred: %s', e, exc_info=True) |
| 172 | + # Retry |
| 173 | + q.put(item) |
| 174 | + |
| 175 | +# Turn on the enrichment worker thread |
| 176 | +threading.Thread(target=enrichment_worker, daemon=True).start() |
| 177 | + |
| 178 | +# Webhook endpoint |
| 179 | +@app.route('/webhook', methods=['POST']) |
| 180 | +def webhook(): |
| 181 | + # Verify JWT token |
| 182 | + header = flask.request.headers.get('Authorization') |
| 183 | + _, token = header.split() |
| 184 | + try: |
| 185 | + jwt.decode(token, WEBHOOK_SECRET, algorithms=['HS256']) |
| 186 | + except jwt.PyJWTError as e: |
| 187 | + app.logger.error('Invalid token: %s', e) |
| 188 | + return {'status': 'unauthorized'}, 401 |
| 189 | + # Process webhook event |
| 190 | + data = flask.json.loads(flask.request.data) |
| 191 | + app.logger.info('Received event: %s', data) |
| 192 | + event = data['event'] |
| 193 | + if event == 'ping': |
| 194 | + # Receive this event when a webhook enrichment is created |
| 195 | + code = 200 |
| 196 | + status = 'ok' |
| 197 | + elif event == 'enrichment.batch.created': |
| 198 | + # Receive this event when a batch of the documents gets ready |
| 199 | + code = 202 |
| 200 | + status = 'accepted' |
| 201 | + # Put an enrichment request into the queue |
| 202 | + q.put(data) |
| 203 | + else: |
| 204 | + # Unknown event type |
| 205 | + code = 400 |
| 206 | + status = 'bad request' |
| 207 | + return {'status': status}, code |
| 208 | + |
| 209 | +PORT = os.getenv('PORT', '8080') |
| 210 | +if __name__ == '__main__': |
| 211 | + app.run(host='0.0.0.0', port=int(PORT)) |
0 commit comments