File size: 3,618 Bytes
48fc275
 
0135459
48fc275
 
 
 
 
 
 
 
 
 
 
0135459
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
"""About tab rendering functionality"""

import os
from config import app_config
import data
import requests
import streamlit as st


###
### INTERNAL FUNCTIONS
###
def __paraphrase(text):
    ### Invoke the lambda function via REST api call to paraphrase the text
    url = os.getenv("API_ENDPOINT")
    headers = {"Content-Type": "application/json"}
    payload = {"text": text}
    response = requests.request(method="POST", url=url, headers=headers, json=payload)
    response = response.json()
    paraphrased_text = response["paraphrased_text"]
    return paraphrased_text


def __refresh_access_cnt_stats(curr_count, access_stats):
    access_cnt_stats = f"""
    |Maximum allowed requests|Requests made so far|Requests available|
    |:---: |:---: |:---:|
    |{app_config.max_access_count}|{curr_count}|{app_config.max_access_count - curr_count}
    """
    access_stats.empty()
    access_stats.container().markdown(access_cnt_stats)


###
### MAIN FLOW, entry point
###
def render():
    ### show the current and available number of requests
    st.success(
        "Due to cost constraints the total number of paraphrasing requests are "
        + "restricted, as shown below..."
    )
    access_stats = st.empty()
    access_counter = data.get_access_counter()
    curr_count = access_counter["current-count"]
    __refresh_access_cnt_stats(curr_count, access_stats)
    st.divider()

    ### if maximum number of requests are exhausted then throw and error and abort
    if curr_count >= app_config.max_access_count:
        st.error(
            "SORRY! the maximum number of allowed requests are exhausted, please ping me via "
            + "`Contact Me` section in `About` tab if you see this message."
        )
    else:
        ### render the UI
        output_text = "Paraphrased text will appear here"
        input_text = st.text_area(
            "Input Text:",
            placeholder="Enter the text you want to paraphrase",
            help="Maximum of 100 words are allowed",
            height=250,
        )
        with st.expander("Important Note...", expanded=True):
            st.warning(
                "***Please note that when you try first time, app may return an "
                + "`Internal Error`, this is expected and is due to the AWS hosted hardware"
                + " taking some time to warm-up first time. After couple of trials you "
                + "should be able to see the results.***",
                icon=app_config.icon_important,
            )

        if st.button("Paraphrase ✒️"):
            ## if max word length breached throw and error
            if len(input_text.split(" ")) > 100:
                st.error(
                    "In this demo app, a maximum of 100 words are allowed in input text, "
                    + "please reduce the text length and try again",
                    icon=app_config.icon_stop,
                )
            ## all good, proceed to process now
            else:
                with st.spinner("Processing..."):
                    output_text = __paraphrase(input_text)

                    # update the access-count in DB and refresh the stats display
                    curr_count = data.update_access_count()
                    __refresh_access_cnt_stats(curr_count, access_stats)
                    # show the original and paraphrased text
                    st.divider()
                    st.subheader("Original Text")
                    st.write(input_text)
                    st.subheader("Paraphrased Text")
                    st.write(output_text)
                    st.divider()