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}")