File size: 8,257 Bytes
8a2c233
 
 
aa1c0b3
a7c09cd
 
2e7b88d
9539210
8a2c233
8e6c672
 
 
2e7b88d
7cb2c40
94ce1a7
7cb2c40
94ce1a7
 
7cb2c40
94ce1a7
 
7cb2c40
94ce1a7
 
7cb2c40
94ce1a7
7cb2c40
94ce1a7
 
7cb2c40
94ce1a7
 
7cb2c40
2e7b88d
 
 
 
 
 
 
 
 
 
 
 
aa1c0b3
 
 
 
 
 
 
 
 
8a2c233
aa1c0b3
 
 
 
 
8a2c233
aa1c0b3
 
8a2c233
aa1c0b3
 
8a2c233
aa1c0b3
 
 
 
 
 
8e6c672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa1c0b3
8e6c672
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa1c0b3
 
8e6c672
029808c
8e6c672
7cb2c40
8e6c672
 
 
029808c
8e6c672
 
 
 
 
aa1c0b3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import subprocess
import json
import streamlit as st
import streamlit.components.v1 as components
import os
import stat
import socket
import time

env_var = "ran_script_once"
host = "127.0.0.1"
port = 8081

prompt_format = \
'''Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.

## Instruction:
Normalize entities in a given sentence, including dates (various formats), currencies (multiple symbols and notations), and scientific units (single and compound). Convert them into their full, standardized textual representations in the same language.

### Example Input:
15/03/1990 को, वैज्ञानिक ने $120 में 500mg यौगिक का एक नमूना खरीदा।

### Example Response:
पंद्रह मार्च उन्नीस सौ नब्बे को, वैज्ञानिक ने एक सौ बीस अमेरिकी डॉलर में पाँच सौ मिलीग्राम यौगिक का एक नमूना खरीदा।

Just as entities like dates, currencies, and scientific units have been normalized into simple terms, you must do the same. Do not leave any entity un-normalised.

## Input:
{}

## Response:
{}'''

def is_port_in_use(host: str, port: int) -> bool:
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
        sock.settimeout(1)  # Set a timeout in case nothing is listening
        try:
            sock.connect((host, port))
            return True
        except (ConnectionRefusedError, socket.timeout):
            return False
        except Exception as e:
            print(f"Unexpected error: {e}")
            return False

def infer(prompt):
    print(f"Prompt:\n{prompt}\n")
    # st.write(command)
    prompt = prompt_format.format (
        prompt, # input
            "", # output - leave this blank for generation!
    )

    prompt = prompt.replace('\n','\\n')

    command = \
    '''curl --request POST \
        --url http://localhost:8081/completion \
        --header "Content-Type: application/json" \
        --data '{"prompt": "'''+prompt+'''", "n_predict": 256}\''''

    print("executing llm run command ... \n")
    # st.write("executing llm run command ... \n")

    print(f"Command:\n{command}\n")
    # st.write(f"Command: {command}")

    # Display the variable on the page
    result = subprocess.run(command, shell=True, capture_output=True, text=True)
    output = json.loads(result.stdout)['content']
    print(f"LLM run command Output:\n{output}\n")
    return output

def setup():
    # Get current permissions and add the execute bit for the owner
    current_permissions = os.stat("init.sh").st_mode
    os.chmod("init.sh", current_permissions | stat.S_IXUSR)
    os.chmod("init2.sh", current_permissions | stat.S_IXUSR)

    # Check if the environment variable exists
    if env_var not in os.environ:
        # Execute the init.sh script
        print(f"{env_var} does not exist")
        # st.write(f"{env_var} does not exist")
        try:
            # result = subprocess.run(["bash", "init.sh"], check=True, capture_output=True, text=True)
            # print("Script output:")
            # print(result.stdout)
            subprocess.run(["bash", "init.sh"], check=True, text=True)
            # st.write("Script output:")
            # st.write(result.stdout)
            while not is_port_in_use(host, port):
                print(f"Not listening on port: {host}:{port}")
                print("Waiting 10 seconds before retrying...")
                # st.write(f"Not listening on port: {host}:{port}")
                # st.write("Waiting 10 seconds before retrying...")
                time.sleep(10)
            print(f"Listening on port: {host}:{port}")
            # st.write(f"Listening on port: {host}:{port}")
        except subprocess.CalledProcessError as e:
            print("An error occurred:")
            print(e.stderr)
            # st.write("An error occurred:")
            # st.write(e.stderr)
        os.environ[env_var] = "1"
        print(f"{env_var} is set to 1.")
        # st.write(f"{env_var} is set to 1.")

    else:
        print(f"{env_var} exists with value: {os.environ[env_var]}")
        # st.write(f"{env_var} exists with value: {os.environ[env_var]}")
        if is_port_in_use(host, port):
            print(f"Something is listening on {host}:{port}")
            print("No need to execute anything")
            # st.write(f"Something is listening on {host}:{port}")
            # st.write("No need to execute anything")
        else:
            print(f"Nothing is listening on {host}:{port}")
            print("Executing init2.sh")
            # st.write(f"Nothing is listening on {host}:{port}")
            # st.write("Executing init2.sh")
            try:
                # result = subprocess.run(["bash", "init2.sh"], check=True, capture_output=True, text=True)
                # print("Script output:")
                # print(result.stdout)
                subprocess.run(["bash", "init2.sh"], check=True, text=True)
                # st.write("Script output:")
                # st.write(result.stdout)
            except subprocess.CalledProcessError as e:
                print("An error occurred:")
                print(e.stderr)
                # st.write("An error occurred:")
                # st.write(e.stderr)

    _ = infer("हा अहवाल 30 pages लांब आणि 10 MB आकाराचा आहे.")
    # output = "hello me tasmay!"
    # time.sleep(5)
    # st.write(f"Output:\n{output}")
    print("All setup execution is completed.")

def main():
    start_time = time.time()
    # Show a spinner while the app is setting up.
    if "setup_done" not in st.session_state:
        with st.spinner("Setting up the app, please wait. It may take around 6-7 minutes to setup."):
            setup()
        st.session_state["setup_done"] = True
    else:
        pass
    
    end_time = time.time()
    elapsed = end_time - start_time
    print(f"Elapsed time till complete setup: {elapsed:.2f} seconds")

    st.title("Sarvam AI - Entity Normalisation App")

    # Use a form so that pressing Enter in the text input triggers submission.
    with st.form(key="llm_form"):
        user_input = st.text_input("Enter your text:")
        submit = st.form_submit_button("Submit")

    if submit:
        # Display a loading spinner for 5 seconds to simulate processing delay.
        with st.spinner('Loading output...'):
            output_text = infer(user_input)

        st.subheader("Output:")
        # Show uneditable output text area.
        st.text_area("Model generated response", output_text, height=150, key="output_area", disabled=True)

        # Safely dump the output text as a JSON string for JS.
        output_json = json.dumps(output_text)

        # HTML/JavaScript code for the copy button.
        html_code = f"""
        <!DOCTYPE html>
        <html>
        <head>
          <meta charset="UTF-8">
          <script>
            function copyText() {{
                var text = {output_json};
                navigator.clipboard.writeText(text).then(function() {{
                    var btn = document.getElementById('copy_btn');
                    btn.innerText = 'Copied!';
                    setTimeout(function() {{
                        btn.innerText = 'Copy Output';
                    }}, 2000);
                }}, function(err) {{
                    console.error('Failed to copy text: ', err);
                }});
            }}
          </script>
        </head>
        <body>
          <button id="copy_btn" onclick="copyText()" style="
            padding: 0.5em 1em;
            font-size: 1em;
            margin-top: 0.5em;
            border: none;
            border-radius: 4px;
            background-color: #4CAF50;
            color: white;
            cursor: pointer;
          ">
            Copy Output
          </button>
        </body>
        </html>
        """
        # Embed the HTML. Adjust the height as needed.
        components.html(html_code, height=120)

if __name__ == "__main__":
    main()