Dark-O-Ether commited on
Commit
aa1c0b3
·
1 Parent(s): da6615b

testing (final versions) v-0.1.1

Browse files
Files changed (1) hide show
  1. app.py +95 -22
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import subprocess
2
  import json
3
  import streamlit as st
 
4
  import os
5
  import stat
6
  import socket
@@ -83,10 +84,6 @@ else:
83
  # st.write("An error occurred:")
84
  # st.write(e.stderr)
85
 
86
- end_time = time.time()
87
- elapsed = end_time - start_time
88
- print(f"Elapsed time till setup: {elapsed:.2f} seconds")
89
-
90
  prompt_format = \
91
  '''Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
92
 
@@ -107,27 +104,103 @@ Just as entities like dates, currencies, and scientific units have been normaliz
107
  ## Response:
108
  {}'''
109
 
110
- prompt = "हा अहवाल 30 pages लांब आणि 10 MB आकाराचा आहे."
 
 
 
 
 
 
 
 
111
 
112
- prompt = prompt_format.format (
113
- prompt, # input
114
- "", # output - leave this blank for generation!
115
- )
 
116
 
117
- prompt = prompt.replace('\n','\\n')
 
118
 
119
- command = \
120
- '''curl --request POST \
121
- --url http://localhost:8081/completion \
122
- --header "Content-Type: application/json" \
123
- --data '{"prompt": "'''+prompt+'''", "n_predict": 256}\''''
124
 
125
- # print(command)
126
- # Display the variable on the page
127
- st.write("executing command ... \n")
128
- result = subprocess.run(command, shell=True, capture_output=True, text=True)
129
- output = json.loads(result.stdout)['content']
130
- print(output)
 
131
  # output = "hello me tasmay!"
132
  # time.sleep(5)
133
- st.write(f"Output:\n{output}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import subprocess
2
  import json
3
  import streamlit as st
4
+ import streamlit.components.v1 as components
5
  import os
6
  import stat
7
  import socket
 
84
  # st.write("An error occurred:")
85
  # st.write(e.stderr)
86
 
 
 
 
 
87
  prompt_format = \
88
  '''Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
89
 
 
104
  ## Response:
105
  {}'''
106
 
107
+ def infer(prompt):
108
+ print(f"Prompt:\n{prompt}\n")
109
+ # st.write(command)
110
+ prompt = prompt_format.format (
111
+ prompt, # input
112
+ "", # output - leave this blank for generation!
113
+ )
114
+
115
+ prompt = prompt.replace('\n','\\n')
116
 
117
+ command = \
118
+ '''curl --request POST \
119
+ --url http://localhost:8081/completion \
120
+ --header "Content-Type: application/json" \
121
+ --data '{"prompt": "'''+prompt+'''", "n_predict": 256}\''''
122
 
123
+ print("executing llm run command ... \n")
124
+ # st.write("executing llm run command ... \n")
125
 
126
+ print(f"Command:\n{command}\n")
127
+ # st.write(f"Command: {command}")
 
 
 
128
 
129
+ # Display the variable on the page
130
+ result = subprocess.run(command, shell=True, capture_output=True, text=True)
131
+ output = json.loads(result.stdout)['content']
132
+ print(f"LLM run command Output:\n{output}\n")
133
+ return output
134
+
135
+ _ = infer("हा अहवाल 30 pages लांब आणि 10 MB आकाराचा आहे.")
136
  # output = "hello me tasmay!"
137
  # time.sleep(5)
138
+ # st.write(f"Output:\n{output}")
139
+ print("All execution is completed.")
140
+
141
+ end_time = time.time()
142
+ elapsed = end_time - start_time
143
+ print(f"Elapsed time till complete setup: {elapsed:.2f} seconds")
144
+
145
+ def main():
146
+ st.title("Sarvam AI - Entity Normalisation App")
147
+
148
+ # Use a form so that pressing Enter in the text input triggers submission.
149
+ with st.form(key="llm_form"):
150
+ user_input = st.text_input("Enter your text:")
151
+ submit = st.form_submit_button("Submit")
152
+
153
+ if submit:
154
+ # Display a loading spinner for 5 seconds to simulate processing delay.
155
+ with st.spinner('Loading output...'):
156
+ output_text = infer(user_input)
157
+
158
+ st.subheader("Output:")
159
+ # Show uneditable output text area.
160
+ st.text_area("Model generated response", output_text, height=150, key="output_area", disabled=True)
161
+
162
+ # Safely dump the output text as a JSON string for JS.
163
+ output_json = json.dumps(output_text)
164
+
165
+ # HTML/JavaScript code for the copy button.
166
+ html_code = f"""
167
+ <!DOCTYPE html>
168
+ <html>
169
+ <head>
170
+ <meta charset="UTF-8">
171
+ <script>
172
+ function copyText() {{
173
+ var text = {output_json};
174
+ navigator.clipboard.writeText(text).then(function() {{
175
+ var btn = document.getElementById('copy_btn');
176
+ btn.innerText = 'Copied!';
177
+ setTimeout(function() {{
178
+ btn.innerText = 'Copy Output';
179
+ }}, 2000);
180
+ }}, function(err) {{
181
+ console.error('Failed to copy text: ', err);
182
+ }});
183
+ }}
184
+ </script>
185
+ </head>
186
+ <body>
187
+ <button id="copy_btn" onclick="copyText()" style="
188
+ padding: 0.5em 1em;
189
+ font-size: 1em;
190
+ margin-top: 0.5em;
191
+ border: none;
192
+ border-radius: 4px;
193
+ background-color: #4CAF50;
194
+ color: white;
195
+ cursor: pointer;
196
+ ">
197
+ Copy Output
198
+ </button>
199
+ </body>
200
+ </html>
201
+ """
202
+ # Embed the HTML. Adjust the height as needed.
203
+ components.html(html_code, height=120)
204
+
205
+ if __name__ == "__main__":
206
+ main()