File size: 2,766 Bytes
48fc275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import requests
import spacy
import concurrent.futures

gpt3_url = "https://api.openai.com/v1/completions"
gpt3_api_key = os.environ["OPENAI_KEY"]
gpt3_headers = {
    "Content-Type": "application/json",
    "Authorization": f"Bearer {gpt3_api_key}",
}


def paraphrase(sent):
    gpt3_payload = {
        "model": "text-davinci-003",
        "prompt": "Following is an original sentence followed by paraphrased version "
        + "of it with diverse choice of words:\n\noriginal: Once upon a time a group of "
        + "rats in forest were searching for water\n\nparaphrase: long ago in a jungle "
        + "a pack of rats were thirsty looking for drinking water\n###\noriginal: "
        + "{0}\nparaphrase:".format(sent),
        "max_tokens": 256,
        "temperature": 0.85,
        "top_p": 1,
        "frequency_penalty": 0.72,
        "presence_penalty": 0.72,
        "stop": ["###"],
    }

    response = requests.request(
        method="POST", url=gpt3_url, headers=gpt3_headers, json=gpt3_payload
    )
    response = response.json()
    paraphrase_sent = response["choices"][0]["text"]
    return paraphrase_sent.strip()


def lambda_handler(event, context):
    """Sample pure Lambda function

    Parameters
    ----------
    event: dict, required
        API Gateway Lambda Proxy Input Format

        Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

    context: object, required
        Lambda Context runtime methods and attributes

        Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

    Returns
    ------
    API Gateway Lambda Proxy Output Format: dict

        Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
    """
    raw_text = r"{0}".format(event["body"])
    json_payload = json.loads(raw_text)
    input_text = json_payload["text"]
    ### break text into sentences
    nlp = spacy.load(
        "en_core_web_sm", disable=["ner", "tagger", "lemmatizer", "textcat"]
    )
    sentences = nlp(input_text).sents
    ### get each sentence paraphrased, use concurrency for speed
    with concurrent.futures.ThreadPoolExecutor() as executor:
        futures = [
            executor.submit(paraphrase, sent.text.strip())
            for sent in sentences
            if sent.text.strip() != ""
        ]
    paraphrased_sentences = [future.result() for future in futures]
    output = {"paraphrased_text": " ".join(paraphrased_sentences)}
    ### join the paraphrased sentences and return as full paraphrased text
    return {
        "statusCode": 200,
        "body": json.dumps(output),
    }