|
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) |
|
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") |
|
|
|
prompt = prompt_format.format ( |
|
prompt, |
|
"", |
|
) |
|
|
|
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") |
|
|
|
|
|
print(f"Command:\n{command}\n") |
|
|
|
|
|
|
|
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(): |
|
|
|
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) |
|
|
|
|
|
if env_var not in os.environ: |
|
|
|
print(f"{env_var} does not exist") |
|
|
|
try: |
|
|
|
|
|
|
|
subprocess.run(["bash", "init.sh"], check=True, text=True) |
|
|
|
|
|
while not is_port_in_use(host, port): |
|
print(f"Not listening on port: {host}:{port}") |
|
print("Waiting 10 seconds before retrying...") |
|
|
|
|
|
time.sleep(10) |
|
print(f"Listening on port: {host}:{port}") |
|
|
|
except subprocess.CalledProcessError as e: |
|
print("An error occurred:") |
|
print(e.stderr) |
|
|
|
|
|
os.environ[env_var] = "1" |
|
print(f"{env_var} is set to 1.") |
|
|
|
|
|
else: |
|
print(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") |
|
|
|
|
|
else: |
|
print(f"Nothing is listening on {host}:{port}") |
|
print("Executing init2.sh") |
|
|
|
|
|
try: |
|
|
|
|
|
|
|
subprocess.run(["bash", "init2.sh"], check=True, text=True) |
|
|
|
|
|
except subprocess.CalledProcessError as e: |
|
print("An error occurred:") |
|
print(e.stderr) |
|
|
|
|
|
|
|
_ = infer("हा अहवाल 30 pages लांब आणि 10 MB आकाराचा आहे.") |
|
|
|
|
|
|
|
print("All setup execution is completed.") |
|
|
|
def main(): |
|
start_time = time.time() |
|
|
|
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") |
|
|
|
|
|
with st.form(key="llm_form"): |
|
user_input = st.text_input("Enter your text:") |
|
submit = st.form_submit_button("Submit") |
|
|
|
if submit: |
|
|
|
with st.spinner('Loading output...'): |
|
output_text = infer(user_input) |
|
|
|
st.subheader("Output:") |
|
|
|
st.text_area("Model generated response", output_text, height=150, key="output_area", disabled=True) |
|
|
|
|
|
output_json = json.dumps(output_text) |
|
|
|
|
|
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> |
|
""" |
|
|
|
components.html(html_code, height=120) |
|
|
|
if __name__ == "__main__": |
|
main() |