Update backupapp.py
Browse files- backupapp.py +197 -63
backupapp.py
CHANGED
@@ -3,7 +3,6 @@ import base64
|
|
3 |
import glob
|
4 |
import json
|
5 |
import math
|
6 |
-
#import mistune
|
7 |
import openai
|
8 |
import os
|
9 |
import pytz
|
@@ -32,23 +31,82 @@ from openai import ChatCompletion
|
|
32 |
from PyPDF2 import PdfReader
|
33 |
from templates import bot_template, css, user_template
|
34 |
from xml.etree import ElementTree as ET
|
|
|
35 |
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
API_KEY = os.getenv('API_KEY')
|
|
|
|
|
|
|
39 |
headers = {
|
40 |
-
"Authorization": f"Bearer {
|
41 |
"Content-Type": "application/json"
|
42 |
}
|
43 |
key = os.getenv('OPENAI_API_KEY')
|
44 |
prompt = f"Write instructions to teach anyone to write a discharge plan. List the entities, features and relationships to CCDA and FHIR objects in boldface."
|
45 |
-
# page config and sidebar declares up front allow all other functions to see global class variables
|
46 |
-
st.set_page_config(page_title="GPT Streamlit Document Reasoner", layout="wide")
|
47 |
-
|
48 |
-
# UI Controls
|
49 |
should_save = st.sidebar.checkbox("💾 Save", value=True, help="Save your session data.")
|
50 |
|
51 |
-
#
|
52 |
def add_witty_humor_buttons():
|
53 |
with st.expander("Wit and Humor 🤣", expanded=True):
|
54 |
# Tip about the Dromedary family
|
@@ -94,10 +152,40 @@ def add_witty_humor_buttons():
|
|
94 |
if col7[0].button("More Funny Rhymes 🎙️"):
|
95 |
StreamLLMChatResponse(descriptions["More Funny Rhymes 🎙️"])
|
96 |
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
def StreamLLMChatResponse(prompt):
|
100 |
-
|
101 |
try:
|
102 |
endpoint_url = API_URL
|
103 |
hf_token = API_KEY
|
@@ -132,27 +220,28 @@ def StreamLLMChatResponse(prompt):
|
|
132 |
|
133 |
except:
|
134 |
st.write('Stream llm issue')
|
|
|
135 |
return result
|
136 |
except:
|
137 |
-
st.write('
|
138 |
-
|
139 |
-
|
140 |
|
|
|
141 |
def query(payload):
|
142 |
response = requests.post(API_URL, headers=headers, json=payload)
|
143 |
st.markdown(response.json())
|
144 |
return response.json()
|
145 |
-
|
146 |
def get_output(prompt):
|
147 |
return query({"inputs": prompt})
|
148 |
|
|
|
149 |
def generate_filename(prompt, file_type):
|
150 |
central = pytz.timezone('US/Central')
|
151 |
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
152 |
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
153 |
-
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:
|
154 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
155 |
|
|
|
156 |
def transcribe_audio(openai_key, file_path, model):
|
157 |
openai.api_key = openai_key
|
158 |
OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
|
@@ -176,6 +265,7 @@ def transcribe_audio(openai_key, file_path, model):
|
|
176 |
st.error("Error in API call.")
|
177 |
return None
|
178 |
|
|
|
179 |
def save_and_play_audio(audio_recorder):
|
180 |
audio_bytes = audio_recorder(key='audio_recorder')
|
181 |
if audio_bytes:
|
@@ -186,34 +276,40 @@ def save_and_play_audio(audio_recorder):
|
|
186 |
return filename
|
187 |
return None
|
188 |
|
|
|
189 |
def create_file(filename, prompt, response, should_save=True):
|
190 |
if not should_save:
|
191 |
return
|
192 |
base_filename, ext = os.path.splitext(filename)
|
193 |
-
has_python_code = bool(re.search(r"```python([\s\S]*?)```", response))
|
194 |
if ext in ['.txt', '.htm', '.md']:
|
195 |
-
with open(f"{base_filename}
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
203 |
|
204 |
def truncate_document(document, length):
|
205 |
return document[:length]
|
206 |
-
|
207 |
def divide_document(document, max_length):
|
208 |
return [document[i:i+max_length] for i in range(0, len(document), max_length)]
|
209 |
|
|
|
|
|
210 |
def get_table_download_link(file_path):
|
211 |
with open(file_path, 'r') as file:
|
212 |
-
|
213 |
-
|
214 |
-
except:
|
215 |
-
st.write('')
|
216 |
-
return file_path
|
217 |
b64 = base64.b64encode(data.encode()).decode()
|
218 |
file_name = os.path.basename(file_path)
|
219 |
ext = os.path.splitext(file_name)[1] # get the file extension
|
@@ -234,13 +330,16 @@ def get_table_download_link(file_path):
|
|
234 |
href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
|
235 |
return href
|
236 |
|
|
|
237 |
def CompressXML(xml_text):
|
238 |
root = ET.fromstring(xml_text)
|
239 |
for elem in list(root.iter()):
|
240 |
if isinstance(elem.tag, str) and 'Comment' in elem.tag:
|
241 |
elem.parent.remove(elem)
|
242 |
return ET.tostring(root, encoding='unicode', method="xml")
|
243 |
-
|
|
|
|
|
244 |
def read_file_content(file,max_length):
|
245 |
if file.type == "application/json":
|
246 |
content = json.load(file)
|
@@ -262,6 +361,8 @@ def read_file_content(file,max_length):
|
|
262 |
else:
|
263 |
return ""
|
264 |
|
|
|
|
|
265 |
def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):
|
266 |
model = model_choice
|
267 |
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
@@ -290,6 +391,8 @@ def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):
|
|
290 |
st.write(time.time() - start_time)
|
291 |
return full_reply_content
|
292 |
|
|
|
|
|
293 |
def chat_with_file_contents(prompt, file_content, model_choice='gpt-3.5-turbo'):
|
294 |
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
295 |
conversation.append({'role': 'user', 'content': prompt})
|
@@ -321,31 +424,34 @@ def extract_file_extension(file):
|
|
321 |
else:
|
322 |
raise ValueError(f"Unable to extract file extension from {file_name}")
|
323 |
|
|
|
|
|
324 |
def pdf2txt(docs):
|
325 |
text = ""
|
326 |
for file in docs:
|
327 |
file_extension = extract_file_extension(file)
|
328 |
st.write(f"File type extension: {file_extension}")
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
text += pdf.pages[page].extract_text() # new PyPDF2 syntax
|
337 |
-
except Exception as e:
|
338 |
-
st.write(f"Error processing file {file.name}: {e}")
|
339 |
return text
|
340 |
|
341 |
def txt2chunks(text):
|
342 |
text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
|
343 |
return text_splitter.split_text(text)
|
344 |
|
|
|
|
|
345 |
def vector_store(text_chunks):
|
346 |
embeddings = OpenAIEmbeddings(openai_api_key=key)
|
347 |
return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
348 |
|
|
|
|
|
349 |
def get_chain(vectorstore):
|
350 |
llm = ChatOpenAI()
|
351 |
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
@@ -378,13 +484,18 @@ def divide_prompt(prompt, max_length):
|
|
378 |
chunks.append(' '.join(current_chunk))
|
379 |
return chunks
|
380 |
|
|
|
|
|
|
|
|
|
381 |
def create_zip_of_files(files):
|
382 |
zip_name = "all_files.zip"
|
383 |
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
384 |
for file in files:
|
385 |
zipf.write(file)
|
386 |
return zip_name
|
387 |
-
|
|
|
388 |
def get_zip_download_link(zip_file):
|
389 |
with open(zip_file, 'rb') as f:
|
390 |
data = f.read()
|
@@ -392,13 +503,24 @@ def get_zip_download_link(zip_file):
|
|
392 |
href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
|
393 |
return href
|
394 |
|
395 |
-
|
|
|
396 |
API_URL_IE = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
397 |
headers = {
|
398 |
-
|
399 |
-
|
400 |
}
|
401 |
|
|
|
402 |
def query(filename):
|
403 |
with open(filename, "rb") as f:
|
404 |
data = f.read()
|
@@ -412,7 +534,7 @@ def generate_filename(prompt, file_type):
|
|
412 |
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
|
413 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
414 |
|
415 |
-
#
|
416 |
def save_and_play_audio(audio_recorder):
|
417 |
audio_bytes = audio_recorder()
|
418 |
if audio_bytes:
|
@@ -422,7 +544,7 @@ def save_and_play_audio(audio_recorder):
|
|
422 |
st.audio(audio_bytes, format="audio/wav")
|
423 |
return filename
|
424 |
|
425 |
-
#
|
426 |
def transcribe_audio(filename):
|
427 |
output = query(filename)
|
428 |
return output
|
@@ -435,7 +557,11 @@ def whisper_main():
|
|
435 |
filename = save_and_play_audio(audio_recorder)
|
436 |
if filename is not None:
|
437 |
transcription = transcribe_audio(filename)
|
438 |
-
|
|
|
|
|
|
|
|
|
439 |
st.write(transcription)
|
440 |
response = StreamLLMChatResponse(transcription)
|
441 |
# st.write(response) - redundant with streaming result?
|
@@ -443,6 +569,8 @@ def whisper_main():
|
|
443 |
create_file(filename, transcription, response, should_save)
|
444 |
#st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
445 |
|
|
|
|
|
446 |
def main():
|
447 |
|
448 |
st.title("AI Drome Llama")
|
@@ -461,14 +589,7 @@ def main():
|
|
461 |
openai.api_key = os.getenv('OPENAI_KEY')
|
462 |
menu = ["txt", "htm", "xlsx", "csv", "md", "py"]
|
463 |
choice = st.sidebar.selectbox("Output File Type:", menu)
|
464 |
-
model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
|
465 |
-
|
466 |
-
#filename = save_and_play_audio(audio_recorder)
|
467 |
-
#if filename is not None:
|
468 |
-
# transcription = transcribe_audio(key, filename, "whisper-1")
|
469 |
-
# st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
470 |
-
# filename = None
|
471 |
-
|
472 |
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
473 |
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
474 |
with collength:
|
@@ -512,6 +633,8 @@ def main():
|
|
512 |
filename = generate_filename(user_prompt, choice)
|
513 |
create_file(filename, user_prompt, response, should_save)
|
514 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
|
|
|
|
515 |
all_files = glob.glob("*.*")
|
516 |
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
|
517 |
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
@@ -547,6 +670,8 @@ def main():
|
|
547 |
if st.button("🗑", key="delete_"+file):
|
548 |
os.remove(file)
|
549 |
st.experimental_rerun()
|
|
|
|
|
550 |
if len(file_contents) > 0:
|
551 |
if next_action=='open':
|
552 |
file_content_area = st.text_area("File Contents:", file_contents, height=500)
|
@@ -555,9 +680,18 @@ def main():
|
|
555 |
if next_action=='search':
|
556 |
file_content_area = st.text_area("File Contents:", file_contents, height=500)
|
557 |
st.write('Reasoning with your inputs...')
|
558 |
-
|
559 |
-
|
560 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
561 |
st.experimental_rerun()
|
562 |
|
563 |
# Feedback
|
@@ -588,7 +722,7 @@ def main():
|
|
588 |
filename = generate_filename(raw, 'txt')
|
589 |
create_file(filename, raw, '', should_save)
|
590 |
|
|
|
591 |
if __name__ == "__main__":
|
592 |
whisper_main()
|
593 |
-
main()
|
594 |
-
|
|
|
3 |
import glob
|
4 |
import json
|
5 |
import math
|
|
|
6 |
import openai
|
7 |
import os
|
8 |
import pytz
|
|
|
31 |
from PyPDF2 import PdfReader
|
32 |
from templates import bot_template, css, user_template
|
33 |
from xml.etree import ElementTree as ET
|
34 |
+
import streamlit.components.v1 as components # Import Streamlit Components for HTML5
|
35 |
|
36 |
+
|
37 |
+
st.set_page_config(page_title="🐪Llama Whisperer🦙 Voice Chat🌟", layout="wide")
|
38 |
+
|
39 |
+
|
40 |
+
def add_Med_Licensing_Exam_Dataset():
|
41 |
+
import streamlit as st
|
42 |
+
from datasets import load_dataset
|
43 |
+
dataset = load_dataset("augtoma/usmle_step_1")['test'] # Using 'test' split
|
44 |
+
st.title("USMLE Step 1 Dataset Viewer")
|
45 |
+
if len(dataset) == 0:
|
46 |
+
st.write("😢 The dataset is empty.")
|
47 |
+
else:
|
48 |
+
st.write("""
|
49 |
+
🔍 Use the search box to filter questions or use the grid to scroll through the dataset.
|
50 |
+
""")
|
51 |
+
|
52 |
+
# 👩🔬 Search Box
|
53 |
+
search_term = st.text_input("Search for a specific question:", "")
|
54 |
+
|
55 |
+
# 🎛 Pagination
|
56 |
+
records_per_page = 100
|
57 |
+
num_records = len(dataset)
|
58 |
+
num_pages = max(int(num_records / records_per_page), 1)
|
59 |
+
|
60 |
+
# Skip generating the slider if num_pages is 1 (i.e., all records fit in one page)
|
61 |
+
if num_pages > 1:
|
62 |
+
page_number = st.select_slider("Select page:", options=list(range(1, num_pages + 1)))
|
63 |
+
else:
|
64 |
+
page_number = 1 # Only one page
|
65 |
+
|
66 |
+
# 📊 Display Data
|
67 |
+
start_idx = (page_number - 1) * records_per_page
|
68 |
+
end_idx = start_idx + records_per_page
|
69 |
+
|
70 |
+
# 🧪 Apply the Search Filter
|
71 |
+
filtered_data = []
|
72 |
+
for record in dataset[start_idx:end_idx]:
|
73 |
+
if isinstance(record, dict) and 'text' in record and 'id' in record:
|
74 |
+
if search_term:
|
75 |
+
if search_term.lower() in record['text'].lower():
|
76 |
+
filtered_data.append(record)
|
77 |
+
else:
|
78 |
+
filtered_data.append(record)
|
79 |
+
|
80 |
+
# 🌐 Render the Grid
|
81 |
+
for record in filtered_data:
|
82 |
+
st.write(f"## Question ID: {record['id']}")
|
83 |
+
st.write(f"### Question:")
|
84 |
+
st.write(f"{record['text']}")
|
85 |
+
st.write(f"### Answer:")
|
86 |
+
st.write(f"{record['answer']}")
|
87 |
+
st.write("---")
|
88 |
+
|
89 |
+
st.write(f"😊 Total Records: {num_records} | 📄 Displaying {start_idx+1} to {min(end_idx, num_records)}")
|
90 |
+
|
91 |
+
# 1. Constants and Top Level UI Variables
|
92 |
+
|
93 |
+
# My Inference API Copy
|
94 |
+
# API_URL = 'https://qe55p8afio98s0u3.us-east-1.aws.endpoints.huggingface.cloud' # Dr Llama
|
95 |
+
# Original:
|
96 |
+
API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
|
97 |
API_KEY = os.getenv('API_KEY')
|
98 |
+
MODEL1="meta-llama/Llama-2-7b-chat-hf"
|
99 |
+
MODEL1URL="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf"
|
100 |
+
HF_KEY = os.getenv('HF_KEY')
|
101 |
headers = {
|
102 |
+
"Authorization": f"Bearer {HF_KEY}",
|
103 |
"Content-Type": "application/json"
|
104 |
}
|
105 |
key = os.getenv('OPENAI_API_KEY')
|
106 |
prompt = f"Write instructions to teach anyone to write a discharge plan. List the entities, features and relationships to CCDA and FHIR objects in boldface."
|
|
|
|
|
|
|
|
|
107 |
should_save = st.sidebar.checkbox("💾 Save", value=True, help="Save your session data.")
|
108 |
|
109 |
+
# 2. Prompt label button demo for LLM
|
110 |
def add_witty_humor_buttons():
|
111 |
with st.expander("Wit and Humor 🤣", expanded=True):
|
112 |
# Tip about the Dromedary family
|
|
|
152 |
if col7[0].button("More Funny Rhymes 🎙️"):
|
153 |
StreamLLMChatResponse(descriptions["More Funny Rhymes 🎙️"])
|
154 |
|
155 |
+
def SpeechSynthesis(result):
|
156 |
+
documentHTML5='''
|
157 |
+
<!DOCTYPE html>
|
158 |
+
<html>
|
159 |
+
<head>
|
160 |
+
<title>Read It Aloud</title>
|
161 |
+
<script type="text/javascript">
|
162 |
+
function readAloud() {
|
163 |
+
const text = document.getElementById("textArea").value;
|
164 |
+
const speech = new SpeechSynthesisUtterance(text);
|
165 |
+
window.speechSynthesis.speak(speech);
|
166 |
+
}
|
167 |
+
</script>
|
168 |
+
</head>
|
169 |
+
<body>
|
170 |
+
<h1>🔊 Read It Aloud</h1>
|
171 |
+
<textarea id="textArea" rows="10" cols="80">
|
172 |
+
'''
|
173 |
+
documentHTML5 = documentHTML5 + result
|
174 |
+
documentHTML5 = documentHTML5 + '''
|
175 |
+
</textarea>
|
176 |
+
<br>
|
177 |
+
<button onclick="readAloud()">🔊 Read Aloud</button>
|
178 |
+
</body>
|
179 |
+
</html>
|
180 |
+
'''
|
181 |
+
|
182 |
+
components.html(documentHTML5, width=1280, height=1024)
|
183 |
+
#return result
|
184 |
+
|
185 |
+
|
186 |
+
# 3. Stream Llama Response
|
187 |
+
# @st.cache_resource
|
188 |
def StreamLLMChatResponse(prompt):
|
|
|
189 |
try:
|
190 |
endpoint_url = API_URL
|
191 |
hf_token = API_KEY
|
|
|
220 |
|
221 |
except:
|
222 |
st.write('Stream llm issue')
|
223 |
+
SpeechSynthesis(result)
|
224 |
return result
|
225 |
except:
|
226 |
+
st.write('Llama model is asleep. Starting up now on A10 - please give 5 minutes then retry as KEDA scales up from zero to activate running container(s).')
|
|
|
|
|
227 |
|
228 |
+
# 4. Run query with payload
|
229 |
def query(payload):
|
230 |
response = requests.post(API_URL, headers=headers, json=payload)
|
231 |
st.markdown(response.json())
|
232 |
return response.json()
|
|
|
233 |
def get_output(prompt):
|
234 |
return query({"inputs": prompt})
|
235 |
|
236 |
+
# 5. Auto name generated output files from time and content
|
237 |
def generate_filename(prompt, file_type):
|
238 |
central = pytz.timezone('US/Central')
|
239 |
safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
|
240 |
replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
|
241 |
+
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:45]
|
242 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
243 |
|
244 |
+
# 6. Speech transcription via OpenAI service
|
245 |
def transcribe_audio(openai_key, file_path, model):
|
246 |
openai.api_key = openai_key
|
247 |
OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
|
|
|
265 |
st.error("Error in API call.")
|
266 |
return None
|
267 |
|
268 |
+
# 7. Auto stop on silence audio control for recording WAV files
|
269 |
def save_and_play_audio(audio_recorder):
|
270 |
audio_bytes = audio_recorder(key='audio_recorder')
|
271 |
if audio_bytes:
|
|
|
276 |
return filename
|
277 |
return None
|
278 |
|
279 |
+
# 8. File creator that interprets type and creates output file for text, markdown and code
|
280 |
def create_file(filename, prompt, response, should_save=True):
|
281 |
if not should_save:
|
282 |
return
|
283 |
base_filename, ext = os.path.splitext(filename)
|
|
|
284 |
if ext in ['.txt', '.htm', '.md']:
|
285 |
+
with open(f"{base_filename}.md", 'w') as file:
|
286 |
+
try:
|
287 |
+
content = prompt.strip() + '\r\n' + response
|
288 |
+
file.write(content)
|
289 |
+
except:
|
290 |
+
st.write('.')
|
291 |
+
|
292 |
+
#has_python_code = re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response)
|
293 |
+
#has_python_code = bool(re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response))
|
294 |
+
#if has_python_code:
|
295 |
+
# python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
|
296 |
+
# with open(f"{base_filename}-Code.py", 'w') as file:
|
297 |
+
# file.write(python_code)
|
298 |
+
# with open(f"{base_filename}.md", 'w') as file:
|
299 |
+
# content = prompt.strip() + '\r\n' + response
|
300 |
+
# file.write(content)
|
301 |
|
302 |
def truncate_document(document, length):
|
303 |
return document[:length]
|
|
|
304 |
def divide_document(document, max_length):
|
305 |
return [document[i:i+max_length] for i in range(0, len(document), max_length)]
|
306 |
|
307 |
+
# 9. Sidebar with UI controls to review and re-run prompts and continue responses
|
308 |
+
@st.cache_resource
|
309 |
def get_table_download_link(file_path):
|
310 |
with open(file_path, 'r') as file:
|
311 |
+
data = file.read()
|
312 |
+
|
|
|
|
|
|
|
313 |
b64 = base64.b64encode(data.encode()).decode()
|
314 |
file_name = os.path.basename(file_path)
|
315 |
ext = os.path.splitext(file_name)[1] # get the file extension
|
|
|
330 |
href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
|
331 |
return href
|
332 |
|
333 |
+
|
334 |
def CompressXML(xml_text):
|
335 |
root = ET.fromstring(xml_text)
|
336 |
for elem in list(root.iter()):
|
337 |
if isinstance(elem.tag, str) and 'Comment' in elem.tag:
|
338 |
elem.parent.remove(elem)
|
339 |
return ET.tostring(root, encoding='unicode', method="xml")
|
340 |
+
|
341 |
+
# 10. Read in and provide UI for past files
|
342 |
+
@st.cache_resource
|
343 |
def read_file_content(file,max_length):
|
344 |
if file.type == "application/json":
|
345 |
content = json.load(file)
|
|
|
361 |
else:
|
362 |
return ""
|
363 |
|
364 |
+
# 11. Chat with GPT - Caution on quota - now favoring fastest AI pipeline STT Whisper->LLM Llama->TTS
|
365 |
+
@st.cache_resource
|
366 |
def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):
|
367 |
model = model_choice
|
368 |
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
|
|
391 |
st.write(time.time() - start_time)
|
392 |
return full_reply_content
|
393 |
|
394 |
+
# 12. Embedding VectorDB for LLM query of documents to text to compress inputs and prompt together as Chat memory using Langchain
|
395 |
+
@st.cache_resource
|
396 |
def chat_with_file_contents(prompt, file_content, model_choice='gpt-3.5-turbo'):
|
397 |
conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
|
398 |
conversation.append({'role': 'user', 'content': prompt})
|
|
|
424 |
else:
|
425 |
raise ValueError(f"Unable to extract file extension from {file_name}")
|
426 |
|
427 |
+
# Normalize input as text from PDF and other formats
|
428 |
+
@st.cache_resource
|
429 |
def pdf2txt(docs):
|
430 |
text = ""
|
431 |
for file in docs:
|
432 |
file_extension = extract_file_extension(file)
|
433 |
st.write(f"File type extension: {file_extension}")
|
434 |
+
if file_extension.lower() in ['py', 'txt', 'html', 'htm', 'xml', 'json']:
|
435 |
+
text += file.getvalue().decode('utf-8')
|
436 |
+
elif file_extension.lower() == 'pdf':
|
437 |
+
from PyPDF2 import PdfReader
|
438 |
+
pdf = PdfReader(BytesIO(file.getvalue()))
|
439 |
+
for page in range(len(pdf.pages)):
|
440 |
+
text += pdf.pages[page].extract_text() # new PyPDF2 syntax
|
|
|
|
|
|
|
441 |
return text
|
442 |
|
443 |
def txt2chunks(text):
|
444 |
text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
|
445 |
return text_splitter.split_text(text)
|
446 |
|
447 |
+
# Vector Store using FAISS
|
448 |
+
@st.cache_resource
|
449 |
def vector_store(text_chunks):
|
450 |
embeddings = OpenAIEmbeddings(openai_api_key=key)
|
451 |
return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
|
452 |
|
453 |
+
# Memory and Retrieval chains
|
454 |
+
@st.cache_resource
|
455 |
def get_chain(vectorstore):
|
456 |
llm = ChatOpenAI()
|
457 |
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
|
|
|
484 |
chunks.append(' '.join(current_chunk))
|
485 |
return chunks
|
486 |
|
487 |
+
|
488 |
+
# 13. Provide way of saving all and deleting all to give way of reviewing output and saving locally before clearing it
|
489 |
+
|
490 |
+
@st.cache_resource
|
491 |
def create_zip_of_files(files):
|
492 |
zip_name = "all_files.zip"
|
493 |
with zipfile.ZipFile(zip_name, 'w') as zipf:
|
494 |
for file in files:
|
495 |
zipf.write(file)
|
496 |
return zip_name
|
497 |
+
|
498 |
+
@st.cache_resource
|
499 |
def get_zip_download_link(zip_file):
|
500 |
with open(zip_file, 'rb') as f:
|
501 |
data = f.read()
|
|
|
503 |
href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
|
504 |
return href
|
505 |
|
506 |
+
# 14. Inference Endpoints for Whisper (best fastest STT) on NVIDIA T4 and Llama (best fastest AGI LLM) on NVIDIA A10
|
507 |
+
# My Inference Endpoint
|
508 |
API_URL_IE = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
|
509 |
+
# Original
|
510 |
+
API_URL_IE = "https://api-inference.huggingface.co/models/openai/whisper-small.en"
|
511 |
+
MODEL2 = "openai/whisper-small.en"
|
512 |
+
MODEL2_URL = "https://huggingface.co/openai/whisper-small.en"
|
513 |
+
#headers = {
|
514 |
+
# "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
|
515 |
+
# "Content-Type": "audio/wav"
|
516 |
+
#}
|
517 |
+
HF_KEY = os.getenv('HF_KEY')
|
518 |
headers = {
|
519 |
+
"Authorization": f"Bearer {HF_KEY}",
|
520 |
+
"Content-Type": "audio/wav"
|
521 |
}
|
522 |
|
523 |
+
#@st.cache_resource
|
524 |
def query(filename):
|
525 |
with open(filename, "rb") as f:
|
526 |
data = f.read()
|
|
|
534 |
safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
|
535 |
return f"{safe_date_time}_{safe_prompt}.{file_type}"
|
536 |
|
537 |
+
# 15. Audio recorder to Wav file
|
538 |
def save_and_play_audio(audio_recorder):
|
539 |
audio_bytes = audio_recorder()
|
540 |
if audio_bytes:
|
|
|
544 |
st.audio(audio_bytes, format="audio/wav")
|
545 |
return filename
|
546 |
|
547 |
+
# 16. Speech transcription to file output
|
548 |
def transcribe_audio(filename):
|
549 |
output = query(filename)
|
550 |
return output
|
|
|
557 |
filename = save_and_play_audio(audio_recorder)
|
558 |
if filename is not None:
|
559 |
transcription = transcribe_audio(filename)
|
560 |
+
try:
|
561 |
+
transcription = transcription['text']
|
562 |
+
except:
|
563 |
+
st.write('Whisper model is asleep. Starting up now on T4 GPU - please give 5 minutes then retry as it scales up from zero to activate running container(s).')
|
564 |
+
|
565 |
st.write(transcription)
|
566 |
response = StreamLLMChatResponse(transcription)
|
567 |
# st.write(response) - redundant with streaming result?
|
|
|
569 |
create_file(filename, transcription, response, should_save)
|
570 |
#st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
571 |
|
572 |
+
|
573 |
+
# 17. Main
|
574 |
def main():
|
575 |
|
576 |
st.title("AI Drome Llama")
|
|
|
589 |
openai.api_key = os.getenv('OPENAI_KEY')
|
590 |
menu = ["txt", "htm", "xlsx", "csv", "md", "py"]
|
591 |
choice = st.sidebar.selectbox("Output File Type:", menu)
|
592 |
+
model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
593 |
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
594 |
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
595 |
with collength:
|
|
|
633 |
filename = generate_filename(user_prompt, choice)
|
634 |
create_file(filename, user_prompt, response, should_save)
|
635 |
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
636 |
+
|
637 |
+
# Compose a file sidebar of past encounters
|
638 |
all_files = glob.glob("*.*")
|
639 |
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
|
640 |
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
|
|
670 |
if st.button("🗑", key="delete_"+file):
|
671 |
os.remove(file)
|
672 |
st.experimental_rerun()
|
673 |
+
|
674 |
+
|
675 |
if len(file_contents) > 0:
|
676 |
if next_action=='open':
|
677 |
file_content_area = st.text_area("File Contents:", file_contents, height=500)
|
|
|
680 |
if next_action=='search':
|
681 |
file_content_area = st.text_area("File Contents:", file_contents, height=500)
|
682 |
st.write('Reasoning with your inputs...')
|
683 |
+
|
684 |
+
# new - llama
|
685 |
+
response = StreamLLMChatResponse(file_contents)
|
686 |
+
filename = generate_filename(user_prompt, ".md")
|
687 |
+
create_file(filename, file_contents, response, should_save)
|
688 |
+
SpeechSynthesis(response)
|
689 |
+
|
690 |
+
# old - gpt
|
691 |
+
#response = chat_with_model(user_prompt, file_contents, model_choice)
|
692 |
+
#filename = generate_filename(file_contents, choice)
|
693 |
+
#create_file(filename, user_prompt, response, should_save)
|
694 |
+
|
695 |
st.experimental_rerun()
|
696 |
|
697 |
# Feedback
|
|
|
722 |
filename = generate_filename(raw, 'txt')
|
723 |
create_file(filename, raw, '', should_save)
|
724 |
|
725 |
+
# 18. Run AI Pipeline
|
726 |
if __name__ == "__main__":
|
727 |
whisper_main()
|
728 |
+
main()
|
|