File size: 4,921 Bytes
8a2c233
 
 
a7c09cd
 
2e7b88d
9539210
8a2c233
a7c09cd
 
 
2e7b88d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9539210
 
 
7b17e5f
 
9539210
 
7b17e5f
2e7b88d
 
 
 
 
 
 
 
 
 
 
7b17e5f
2e7b88d
 
 
 
 
 
 
 
7b17e5f
 
2e7b88d
 
 
 
 
 
 
 
 
 
 
a7c09cd
 
8a2c233
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import subprocess
import json
import streamlit as st
import os
import stat
import socket
import time

# 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)

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

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

# 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)
        # 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)
            # 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)


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:
{}'''

prompt = "हा अहवाल 30 pages लांब आणि 10 MB आकाराचा आहे."

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(command)
# Display the variable on the page
st.write("executing command ... \n")
result = subprocess.run(command, shell=True, capture_output=True, text=True)
output = json.loads(result.stdout)['content']
print(output)
# output = "hello me tasmay!"
# time.sleep(5)
st.write(f"Output:\n{output}")