awacke1 commited on
Commit
04b4b52
β€’
1 Parent(s): 42b3a5d

Create backup2.app.py

Browse files
Files changed (1) hide show
  1. backup2.app.py +836 -0
backup2.app.py ADDED
@@ -0,0 +1,836 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Imports
2
+ import base64
3
+ import glob
4
+ import json
5
+ import math
6
+ import openai
7
+ import os
8
+ import pytz
9
+ import re
10
+ import requests
11
+ import streamlit as st
12
+ import textract
13
+ import time
14
+ import zipfile
15
+ import huggingface_hub
16
+ import dotenv
17
+ from audio_recorder_streamlit import audio_recorder
18
+ from bs4 import BeautifulSoup
19
+ from collections import deque
20
+ from datetime import datetime
21
+ from dotenv import load_dotenv
22
+ from huggingface_hub import InferenceClient
23
+ from io import BytesIO
24
+ from langchain.chat_models import ChatOpenAI
25
+ from langchain.chains import ConversationalRetrievalChain
26
+ from langchain.embeddings import OpenAIEmbeddings
27
+ from langchain.memory import ConversationBufferMemory
28
+ from langchain.text_splitter import CharacterTextSplitter
29
+ from langchain.vectorstores import FAISS
30
+ from openai import ChatCompletion
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
+ st.markdown(record)
77
+ filtered_data.append(record)
78
+ else:
79
+ filtered_data.append(record)
80
+
81
+ # 🌐 Render the Grid
82
+ for record in filtered_data:
83
+ st.write(f"## Question ID: {record['id']}")
84
+ st.write(f"### Question:")
85
+ st.write(f"{record['text']}")
86
+ st.write(f"### Answer:")
87
+ st.write(f"{record['answer']}")
88
+ st.write("---")
89
+
90
+ st.write(f"😊 Total Records: {num_records} | πŸ“„ Displaying {start_idx+1} to {min(end_idx, num_records)}")
91
+
92
+ # 1. Constants and Top Level UI Variables
93
+
94
+ # My Inference API Copy
95
+ # API_URL = 'https://qe55p8afio98s0u3.us-east-1.aws.endpoints.huggingface.cloud' # Dr Llama
96
+ # Original:
97
+ API_URL = "https://api-inference.huggingface.co/models/meta-llama/Llama-2-7b-chat-hf"
98
+ API_KEY = os.getenv('API_KEY')
99
+ MODEL1="meta-llama/Llama-2-7b-chat-hf"
100
+ MODEL1URL="https://huggingface.co/meta-llama/Llama-2-7b-chat-hf"
101
+ HF_KEY = os.getenv('HF_KEY')
102
+ headers = {
103
+ "Authorization": f"Bearer {HF_KEY}",
104
+ "Content-Type": "application/json"
105
+ }
106
+ key = os.getenv('OPENAI_API_KEY')
107
+ prompt = f"Write instructions to teach discharge planning along with guidelines and patient education. List entities, features and relationships to CCDA and FHIR objects in boldface."
108
+ should_save = st.sidebar.checkbox("πŸ’Ύ Save", value=True, help="Save your session data.")
109
+
110
+ # 2. Prompt label button demo for LLM
111
+ def add_witty_humor_buttons():
112
+ with st.expander("Wit and Humor 🀣", expanded=True):
113
+ # Tip about the Dromedary family
114
+ st.markdown("πŸ”¬ **Fun Fact**: Dromedaries, part of the camel family, have a single hump and are adapted to arid environments. Their 'superpowers' include the ability to survive without water for up to 7 days, thanks to their specialized blood cells and water storage in their hump.")
115
+
116
+ # Define button descriptions
117
+ descriptions = {
118
+ "Generate Limericks πŸ˜‚": "Write ten random adult limericks based on quotes that are tweet length and make you laugh 🎭",
119
+ "Wise Quotes πŸ§™": "Generate ten wise quotes that are tweet length πŸ¦‰",
120
+ "Funny Rhymes 🎀": "Create ten funny rhymes that are tweet length 🎢",
121
+ "Medical Jokes πŸ’‰": "Create ten medical jokes that are tweet length πŸ₯",
122
+ "Minnesota Humor ❄️": "Create ten jokes about Minnesota that are tweet length 🌨️",
123
+ "Top Funny Stories πŸ“–": "Create ten funny stories that are tweet length πŸ“š",
124
+ "More Funny Rhymes πŸŽ™οΈ": "Create ten more funny rhymes that are tweet length 🎡"
125
+ }
126
+
127
+ # Create columns
128
+ col1, col2, col3 = st.columns([1, 1, 1], gap="small")
129
+
130
+ # Add buttons to columns
131
+ if col1.button("Wise Limericks πŸ˜‚"):
132
+ StreamLLMChatResponse(descriptions["Generate Limericks πŸ˜‚"])
133
+
134
+ if col2.button("Wise Quotes πŸ§™"):
135
+ StreamLLMChatResponse(descriptions["Wise Quotes πŸ§™"])
136
+
137
+ #if col3.button("Funny Rhymes 🎀"):
138
+ # StreamLLMChatResponse(descriptions["Funny Rhymes 🎀"])
139
+
140
+ col4, col5, col6 = st.columns([1, 1, 1], gap="small")
141
+
142
+ if col4.button("Top Ten Funniest Clean Jokes πŸ’‰"):
143
+ StreamLLMChatResponse(descriptions["Top Ten Funniest Clean Jokes πŸ’‰"])
144
+
145
+ if col5.button("Minnesota Humor ❄️"):
146
+ StreamLLMChatResponse(descriptions["Minnesota Humor ❄️"])
147
+
148
+ if col6.button("Origins of Medical Science True Stories"):
149
+ StreamLLMChatResponse(descriptions["Origins of Medical Science True Stories"])
150
+
151
+ col7 = st.columns(1, gap="small")
152
+
153
+ if col7[0].button("Top Ten Best Write a streamlit python program prompts to build AI programs. πŸŽ™οΈ"):
154
+ StreamLLMChatResponse(descriptions["Top Ten Best Write a streamlit python program prompts to build AI programs. πŸŽ™οΈ"])
155
+
156
+ def SpeechSynthesis(result):
157
+ documentHTML5='''
158
+ <!DOCTYPE html>
159
+ <html>
160
+ <head>
161
+ <title>Read It Aloud</title>
162
+ <script type="text/javascript">
163
+ function readAloud() {
164
+ const text = document.getElementById("textArea").value;
165
+ const speech = new SpeechSynthesisUtterance(text);
166
+ window.speechSynthesis.speak(speech);
167
+ }
168
+ </script>
169
+ </head>
170
+ <body>
171
+ <h1>πŸ”Š Read It Aloud</h1>
172
+ <textarea id="textArea" rows="10" cols="80">
173
+ '''
174
+ documentHTML5 = documentHTML5 + result
175
+ documentHTML5 = documentHTML5 + '''
176
+ </textarea>
177
+ <br>
178
+ <button onclick="readAloud()">πŸ”Š Read Aloud</button>
179
+ </body>
180
+ </html>
181
+ '''
182
+
183
+ components.html(documentHTML5, width=1280, height=1024)
184
+ #return result
185
+
186
+
187
+ # 3. Stream Llama Response
188
+ # @st.cache_resource
189
+ def StreamLLMChatResponse(prompt):
190
+ try:
191
+ endpoint_url = API_URL
192
+ hf_token = API_KEY
193
+ client = InferenceClient(endpoint_url, token=hf_token)
194
+ gen_kwargs = dict(
195
+ max_new_tokens=512,
196
+ top_k=30,
197
+ top_p=0.9,
198
+ temperature=0.2,
199
+ repetition_penalty=1.02,
200
+ stop_sequences=["\nUser:", "<|endoftext|>", "</s>"],
201
+ )
202
+ stream = client.text_generation(prompt, stream=True, details=True, **gen_kwargs)
203
+ report=[]
204
+ res_box = st.empty()
205
+ collected_chunks=[]
206
+ collected_messages=[]
207
+ allresults=''
208
+ for r in stream:
209
+ if r.token.special:
210
+ continue
211
+ if r.token.text in gen_kwargs["stop_sequences"]:
212
+ break
213
+ collected_chunks.append(r.token.text)
214
+ chunk_message = r.token.text
215
+ collected_messages.append(chunk_message)
216
+ try:
217
+ report.append(r.token.text)
218
+ if len(r.token.text) > 0:
219
+ result="".join(report).strip()
220
+ res_box.markdown(f'*{result}*')
221
+
222
+ except:
223
+ st.write('Stream llm issue')
224
+ SpeechSynthesis(result)
225
+ return result
226
+ except:
227
+ 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).')
228
+
229
+ # 4. Run query with payload
230
+ def query(payload):
231
+ response = requests.post(API_URL, headers=headers, json=payload)
232
+ st.markdown(response.json())
233
+ return response.json()
234
+ def get_output(prompt):
235
+ return query({"inputs": prompt})
236
+
237
+ # 5. Auto name generated output files from time and content
238
+ def generate_filename(prompt, file_type):
239
+ central = pytz.timezone('US/Central')
240
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
241
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
242
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:255] # 255 is linux max, 260 is windows max
243
+ #safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:45]
244
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
245
+
246
+ # 6. Speech transcription via OpenAI service
247
+ def transcribe_audio(openai_key, file_path, model):
248
+ openai.api_key = openai_key
249
+ OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
250
+ headers = {
251
+ "Authorization": f"Bearer {openai_key}",
252
+ }
253
+ with open(file_path, 'rb') as f:
254
+ data = {'file': f}
255
+ response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
256
+ if response.status_code == 200:
257
+ st.write(response.json())
258
+ chatResponse = chat_with_model(response.json().get('text'), '') # *************************************
259
+ transcript = response.json().get('text')
260
+ filename = generate_filename(transcript, 'txt')
261
+ response = chatResponse
262
+ user_prompt = transcript
263
+ create_file(filename, user_prompt, response, should_save)
264
+ return transcript
265
+ else:
266
+ st.write(response.json())
267
+ st.error("Error in API call.")
268
+ return None
269
+
270
+ # 7. Auto stop on silence audio control for recording WAV files
271
+ def save_and_play_audio(audio_recorder):
272
+ audio_bytes = audio_recorder(key='audio_recorder')
273
+ if audio_bytes:
274
+ filename = generate_filename("Recording", "wav")
275
+ with open(filename, 'wb') as f:
276
+ f.write(audio_bytes)
277
+ st.audio(audio_bytes, format="audio/wav")
278
+ return filename
279
+ return None
280
+
281
+ # 8. File creator that interprets type and creates output file for text, markdown and code
282
+ def create_file(filename, prompt, response, should_save=True):
283
+ if not should_save:
284
+ return
285
+ base_filename, ext = os.path.splitext(filename)
286
+ if ext in ['.txt', '.htm', '.md']:
287
+ with open(f"{base_filename}.md", 'w') as file:
288
+ try:
289
+ content = prompt.strip() + '\r\n' + response
290
+ file.write(content)
291
+ except:
292
+ st.write('.')
293
+
294
+ #has_python_code = re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response)
295
+ #has_python_code = bool(re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response))
296
+ #if has_python_code:
297
+ # python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
298
+ # with open(f"{base_filename}-Code.py", 'w') as file:
299
+ # file.write(python_code)
300
+ # with open(f"{base_filename}.md", 'w') as file:
301
+ # content = prompt.strip() + '\r\n' + response
302
+ # file.write(content)
303
+
304
+ def truncate_document(document, length):
305
+ return document[:length]
306
+ def divide_document(document, max_length):
307
+ return [document[i:i+max_length] for i in range(0, len(document), max_length)]
308
+
309
+ # 9. Sidebar with UI controls to review and re-run prompts and continue responses
310
+ @st.cache_resource
311
+ def get_table_download_link(file_path):
312
+ with open(file_path, 'r') as file:
313
+ data = file.read()
314
+
315
+ b64 = base64.b64encode(data.encode()).decode()
316
+ file_name = os.path.basename(file_path)
317
+ ext = os.path.splitext(file_name)[1] # get the file extension
318
+ if ext == '.txt':
319
+ mime_type = 'text/plain'
320
+ elif ext == '.py':
321
+ mime_type = 'text/plain'
322
+ elif ext == '.xlsx':
323
+ mime_type = 'text/plain'
324
+ elif ext == '.csv':
325
+ mime_type = 'text/plain'
326
+ elif ext == '.htm':
327
+ mime_type = 'text/html'
328
+ elif ext == '.md':
329
+ mime_type = 'text/markdown'
330
+ elif ext == '.wav':
331
+ mime_type = 'audio/wav'
332
+ else:
333
+ mime_type = 'application/octet-stream' # general binary data type
334
+ href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
335
+ return href
336
+
337
+
338
+ def CompressXML(xml_text):
339
+ root = ET.fromstring(xml_text)
340
+ for elem in list(root.iter()):
341
+ if isinstance(elem.tag, str) and 'Comment' in elem.tag:
342
+ elem.parent.remove(elem)
343
+ return ET.tostring(root, encoding='unicode', method="xml")
344
+
345
+ # 10. Read in and provide UI for past files
346
+ @st.cache_resource
347
+ def read_file_content(file,max_length):
348
+ if file.type == "application/json":
349
+ content = json.load(file)
350
+ return str(content)
351
+ elif file.type == "text/html" or file.type == "text/htm":
352
+ content = BeautifulSoup(file, "html.parser")
353
+ return content.text
354
+ elif file.type == "application/xml" or file.type == "text/xml":
355
+ tree = ET.parse(file)
356
+ root = tree.getroot()
357
+ xml = CompressXML(ET.tostring(root, encoding='unicode'))
358
+ return xml
359
+ elif file.type == "text/markdown" or file.type == "text/md":
360
+ md = mistune.create_markdown()
361
+ content = md(file.read().decode())
362
+ return content
363
+ elif file.type == "text/plain":
364
+ return file.getvalue().decode()
365
+ else:
366
+ return ""
367
+
368
+ # 11. Chat with GPT - Caution on quota - now favoring fastest AI pipeline STT Whisper->LLM Llama->TTS
369
+ @st.cache_resource
370
+ def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):
371
+ model = model_choice
372
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
373
+ conversation.append({'role': 'user', 'content': prompt})
374
+ if len(document_section)>0:
375
+ conversation.append({'role': 'assistant', 'content': document_section})
376
+ start_time = time.time()
377
+ report = []
378
+ res_box = st.empty()
379
+ collected_chunks = []
380
+ collected_messages = []
381
+ for chunk in openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=conversation, temperature=0.5, stream=True):
382
+ collected_chunks.append(chunk)
383
+ chunk_message = chunk['choices'][0]['delta']
384
+ collected_messages.append(chunk_message)
385
+ content=chunk["choices"][0].get("delta",{}).get("content")
386
+ try:
387
+ report.append(content)
388
+ if len(content) > 0:
389
+ result = "".join(report).strip()
390
+ res_box.markdown(f'*{result}*')
391
+ except:
392
+ st.write(' ')
393
+ full_reply_content = ''.join([m.get('content', '') for m in collected_messages])
394
+ st.write("Elapsed time:")
395
+ st.write(time.time() - start_time)
396
+ return full_reply_content
397
+
398
+ # 12. Embedding VectorDB for LLM query of documents to text to compress inputs and prompt together as Chat memory using Langchain
399
+ @st.cache_resource
400
+ def chat_with_file_contents(prompt, file_content, model_choice='gpt-3.5-turbo'):
401
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
402
+ conversation.append({'role': 'user', 'content': prompt})
403
+ if len(file_content)>0:
404
+ conversation.append({'role': 'assistant', 'content': file_content})
405
+ response = openai.ChatCompletion.create(model=model_choice, messages=conversation)
406
+ return response['choices'][0]['message']['content']
407
+
408
+ def extract_mime_type(file):
409
+ if isinstance(file, str):
410
+ pattern = r"type='(.*?)'"
411
+ match = re.search(pattern, file)
412
+ if match:
413
+ return match.group(1)
414
+ else:
415
+ raise ValueError(f"Unable to extract MIME type from {file}")
416
+ elif isinstance(file, streamlit.UploadedFile):
417
+ return file.type
418
+ else:
419
+ raise TypeError("Input should be a string or a streamlit.UploadedFile object")
420
+
421
+ def extract_file_extension(file):
422
+ # get the file name directly from the UploadedFile object
423
+ file_name = file.name
424
+ pattern = r".*?\.(.*?)$"
425
+ match = re.search(pattern, file_name)
426
+ if match:
427
+ return match.group(1)
428
+ else:
429
+ raise ValueError(f"Unable to extract file extension from {file_name}")
430
+
431
+ # Normalize input as text from PDF and other formats
432
+ @st.cache_resource
433
+ def pdf2txt(docs):
434
+ text = ""
435
+ for file in docs:
436
+ file_extension = extract_file_extension(file)
437
+ st.write(f"File type extension: {file_extension}")
438
+ if file_extension.lower() in ['py', 'txt', 'html', 'htm', 'xml', 'json']:
439
+ text += file.getvalue().decode('utf-8')
440
+ elif file_extension.lower() == 'pdf':
441
+ from PyPDF2 import PdfReader
442
+ pdf = PdfReader(BytesIO(file.getvalue()))
443
+ for page in range(len(pdf.pages)):
444
+ text += pdf.pages[page].extract_text() # new PyPDF2 syntax
445
+ return text
446
+
447
+ def txt2chunks(text):
448
+ text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
449
+ return text_splitter.split_text(text)
450
+
451
+ # Vector Store using FAISS
452
+ @st.cache_resource
453
+ def vector_store(text_chunks):
454
+ embeddings = OpenAIEmbeddings(openai_api_key=key)
455
+ return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
456
+
457
+ # Memory and Retrieval chains
458
+ @st.cache_resource
459
+ def get_chain(vectorstore):
460
+ llm = ChatOpenAI()
461
+ memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
462
+ return ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectorstore.as_retriever(), memory=memory)
463
+
464
+ def process_user_input(user_question):
465
+ response = st.session_state.conversation({'question': user_question})
466
+ st.session_state.chat_history = response['chat_history']
467
+ for i, message in enumerate(st.session_state.chat_history):
468
+ template = user_template if i % 2 == 0 else bot_template
469
+ st.write(template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
470
+ filename = generate_filename(user_question, 'txt')
471
+ response = message.content
472
+ user_prompt = user_question
473
+ create_file(filename, user_prompt, response, should_save)
474
+
475
+ def divide_prompt(prompt, max_length):
476
+ words = prompt.split()
477
+ chunks = []
478
+ current_chunk = []
479
+ current_length = 0
480
+ for word in words:
481
+ if len(word) + current_length <= max_length:
482
+ current_length += len(word) + 1
483
+ current_chunk.append(word)
484
+ else:
485
+ chunks.append(' '.join(current_chunk))
486
+ current_chunk = [word]
487
+ current_length = len(word)
488
+ chunks.append(' '.join(current_chunk))
489
+ return chunks
490
+
491
+
492
+ # 13. Provide way of saving all and deleting all to give way of reviewing output and saving locally before clearing it
493
+
494
+ @st.cache_resource
495
+ def create_zip_of_files(files):
496
+ zip_name = "all_files.zip"
497
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
498
+ for file in files:
499
+ zipf.write(file)
500
+ return zip_name
501
+
502
+ @st.cache_resource
503
+ def get_zip_download_link(zip_file):
504
+ with open(zip_file, 'rb') as f:
505
+ data = f.read()
506
+ b64 = base64.b64encode(data).decode()
507
+ href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
508
+ return href
509
+
510
+ # 14. Inference Endpoints for Whisper (best fastest STT) on NVIDIA T4 and Llama (best fastest AGI LLM) on NVIDIA A10
511
+ # My Inference Endpoint
512
+ API_URL_IE = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
513
+ # Original
514
+ API_URL_IE = "https://api-inference.huggingface.co/models/openai/whisper-small.en"
515
+ MODEL2 = "openai/whisper-small.en"
516
+ MODEL2_URL = "https://huggingface.co/openai/whisper-small.en"
517
+ #headers = {
518
+ # "Authorization": "Bearer XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
519
+ # "Content-Type": "audio/wav"
520
+ #}
521
+ # HF_KEY = os.getenv('HF_KEY')
522
+ HF_KEY = st.secrets['HF_KEY']
523
+ headers = {
524
+ "Authorization": f"Bearer {HF_KEY}",
525
+ "Content-Type": "audio/wav"
526
+ }
527
+
528
+ #@st.cache_resource
529
+ def query(filename):
530
+ with open(filename, "rb") as f:
531
+ data = f.read()
532
+ response = requests.post(API_URL_IE, headers=headers, data=data)
533
+ return response.json()
534
+
535
+ def generate_filename(prompt, file_type):
536
+ central = pytz.timezone('US/Central')
537
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
538
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
539
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
540
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
541
+
542
+ # 15. Audio recorder to Wav file
543
+ def save_and_play_audio(audio_recorder):
544
+ audio_bytes = audio_recorder()
545
+ if audio_bytes:
546
+ filename = generate_filename("Recording", "wav")
547
+ with open(filename, 'wb') as f:
548
+ f.write(audio_bytes)
549
+ st.audio(audio_bytes, format="audio/wav")
550
+ return filename
551
+
552
+ # 16. Speech transcription to file output
553
+ def transcribe_audio(filename):
554
+ output = query(filename)
555
+ return output
556
+
557
+ def whisper_main():
558
+ #st.title("Speech to Text")
559
+ #st.write("Record your speech and get the text.")
560
+
561
+ # Audio, transcribe, GPT:
562
+ filename = save_and_play_audio(audio_recorder)
563
+ if filename is not None:
564
+ transcription = transcribe_audio(filename)
565
+ try:
566
+ transcript = transcription['text']
567
+ st.write(transcript)
568
+ response = StreamLLMChatResponse(transcript)
569
+ filename_txt = generate_filename(transcript, ".txt")
570
+ create_file(filename_txt, transcript, response, should_save)
571
+ filename_wav = filename_txt.replace('.txt', '.wav')
572
+ import shutil
573
+ shutil.copyfile(filename, filename_wav)
574
+ if os.path.exists(filename):
575
+ os.remove(filename)
576
+ except:
577
+ st.write('Starting Whisper Model on GPU. Please retry in 30 seconds.')
578
+
579
+
580
+ import streamlit as st
581
+
582
+ # Sample function to demonstrate a response, replace with your own logic
583
+ def StreamMedChatResponse(topic):
584
+ st.write(f"Showing resources or questions related to: {topic}")
585
+
586
+ def add_medical_exam_buttons():
587
+ with st.expander("Medical Licensing Exam Topics πŸ“š", expanded=False):
588
+ st.markdown("🩺 **Important**: This section provides a variety of medical topics that are often encountered in medical licensing exams.")
589
+
590
+ # Define medical exam terminology descriptions
591
+ descriptions = {
592
+ "Ultrasound with Doppler 🌊": "3 Questions and Answers with emojis about Doppler Ultrasound imaging techniques πŸŽ₯",
593
+ "Oseltamivir 🦠": "3 Questions and Answers with emojis about the antiviral medication Oseltamivir πŸ’Š",
594
+ "IM Epinephrine πŸ’‰": "3 Questions and Answers with emojis about intramuscular administration of epinephrine πŸ’ͺ",
595
+ "Hypokalemia 🍌": "3 Questions and Answers with emojis about low potassium levels in blood 🩸",
596
+ "Succinylcholine πŸ’Š": "3 Questions and Answers with emojis on the use and side-effects of Succinylcholine πŸš‘",
597
+ "Phosphoinositol System 🧬": "3 Questions and Answers with emojis about the Phosphoinositol signalling system πŸ› ",
598
+ "Ramipril πŸ’Š": "3 Questions and Answers with emojis related to the ACE inhibitor Ramipril 🩺"
599
+ }
600
+
601
+ # Create columns
602
+ col1, col2, col3, col4 = st.columns([1, 1, 1, 1], gap="small")
603
+
604
+ # Add buttons to columns
605
+ if col1.button("Ultrasound with Doppler 🌊"):
606
+ StreamLLMChatResponse(descriptions["Ultrasound with Doppler 🌊"])
607
+
608
+ if col2.button("Oseltamivir 🦠"):
609
+ StreamLLMChatResponse(descriptions["Oseltamivir 🦠"])
610
+
611
+ if col3.button("IM Epinephrine πŸ’‰"):
612
+ StreamLLMChatResponse(descriptions["IM Epinephrine πŸ’‰"])
613
+
614
+ if col4.button("Hypokalemia 🍌"):
615
+ StreamLLMChatResponse(descriptions["Hypokalemia 🍌"])
616
+
617
+ col5, col6, col7, col8 = st.columns([1, 1, 1, 1], gap="small")
618
+
619
+ if col5.button("Succinylcholine πŸ’Š"):
620
+ StreamLLMChatResponse(descriptions["Succinylcholine πŸ’Š"])
621
+
622
+ if col6.button("Phosphoinositol System 🧬"):
623
+ StreamLLMChatResponse(descriptions["Phosphoinositol System 🧬"])
624
+
625
+ if col7.button("Ramipril πŸ’Š"):
626
+ StreamLLMChatResponse(descriptions["Ramipril πŸ’Š"])
627
+
628
+
629
+
630
+ # 17. Main
631
+ def main():
632
+
633
+ #st.title("GAIA - Medical License Exam Testing")
634
+ prompt = f"Write ten funny jokes that are tweet length stories that make you laugh. Show as markdown outline with emojis for each."
635
+
636
+ # Add Wit and Humor buttons
637
+ # add_witty_humor_buttons()
638
+ add_medical_exam_buttons()
639
+
640
+
641
+ with st.expander("Prompts πŸ“š", expanded=False):
642
+
643
+ example_input = st.text_input("Enter your prompt text for Llama:", value=prompt, help="Enter text to get a response from DromeLlama.")
644
+ if st.button("Run Prompt With Llama model", help="Click to run the prompt."):
645
+ try:
646
+ response=StreamLLMChatResponse(example_input)
647
+ create_file(filename, example_input, response, should_save)
648
+ except:
649
+ st.write('Llama model is asleep. Starting now on A10 GPU. Please wait one minute then retry. KEDA triggered.')
650
+
651
+ openai.api_key = os.getenv('OPENAI_API_KEY')
652
+ if openai.api_key == None: openai.api_key = st.secrets['OPENAI_API_KEY']
653
+
654
+ menu = ["txt", "htm", "xlsx", "csv", "md", "py"]
655
+ choice = st.sidebar.selectbox("Output File Type:", menu)
656
+
657
+ model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
658
+
659
+ user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
660
+ collength, colupload = st.columns([2,3]) # adjust the ratio as needed
661
+ with collength:
662
+ max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
663
+ with colupload:
664
+ uploaded_file = st.file_uploader("Add a file for context:", type=["pdf", "xml", "json", "xlsx", "csv", "html", "htm", "md", "txt"])
665
+ document_sections = deque()
666
+ document_responses = {}
667
+ if uploaded_file is not None:
668
+ file_content = read_file_content(uploaded_file, max_length)
669
+ document_sections.extend(divide_document(file_content, max_length))
670
+ if len(document_sections) > 0:
671
+ if st.button("πŸ‘οΈ View Upload"):
672
+ st.markdown("**Sections of the uploaded file:**")
673
+ for i, section in enumerate(list(document_sections)):
674
+ st.markdown(f"**Section {i+1}**\n{section}")
675
+ st.markdown("**Chat with the model:**")
676
+ for i, section in enumerate(list(document_sections)):
677
+ if i in document_responses:
678
+ st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
679
+ else:
680
+ if st.button(f"Chat about Section {i+1}"):
681
+ st.write('Reasoning with your inputs...')
682
+ #response = chat_with_model(user_prompt, section, model_choice)
683
+ st.write('Response:')
684
+ st.write(response)
685
+ document_responses[i] = response
686
+ filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
687
+ create_file(filename, user_prompt, response, should_save)
688
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
689
+ if st.button('πŸ’¬ Chat'):
690
+ st.write('Reasoning with your inputs...')
691
+ user_prompt_sections = divide_prompt(user_prompt, max_length)
692
+ full_response = ''
693
+ for prompt_section in user_prompt_sections:
694
+ response = chat_with_model(prompt_section, ''.join(list(document_sections)), model_choice)
695
+ full_response += response + '\n' # Combine the responses
696
+ response = full_response
697
+ st.write('Response:')
698
+ st.write(response)
699
+ filename = generate_filename(user_prompt, choice)
700
+ create_file(filename, user_prompt, response, should_save)
701
+ #st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
702
+
703
+ # Compose a file sidebar of markdown md files:
704
+ all_files = glob.glob("*.md")
705
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
706
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
707
+ if st.sidebar.button("πŸ—‘ Delete All Text"):
708
+ for file in all_files:
709
+ os.remove(file)
710
+ st.experimental_rerun()
711
+ if st.sidebar.button("⬇️ Download All"):
712
+ zip_file = create_zip_of_files(all_files)
713
+ st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
714
+ file_contents=''
715
+ next_action=''
716
+ for file in all_files:
717
+ col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
718
+ with col1:
719
+ if st.button("🌐", key="md_"+file): # md emoji button
720
+ with open(file, 'r') as f:
721
+ file_contents = f.read()
722
+ next_action='md'
723
+ with col2:
724
+ st.markdown(get_table_download_link(file), unsafe_allow_html=True)
725
+ with col3:
726
+ if st.button("πŸ“‚", key="open_"+file): # open emoji button
727
+ with open(file, 'r') as f:
728
+ file_contents = f.read()
729
+ next_action='open'
730
+ with col4:
731
+ if st.button("πŸ”", key="read_"+file): # search emoji button
732
+ with open(file, 'r') as f:
733
+ file_contents = f.read()
734
+ next_action='search'
735
+ with col5:
736
+ if st.button("πŸ—‘", key="delete_"+file):
737
+ os.remove(file)
738
+ st.experimental_rerun()
739
+
740
+
741
+ if len(file_contents) > 0:
742
+ if next_action=='open':
743
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
744
+ if next_action=='md':
745
+ st.markdown(file_contents)
746
+ if next_action=='search':
747
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
748
+ st.write('Reasoning with your inputs...')
749
+
750
+ # new - llama
751
+ response = StreamLLMChatResponse(file_contents)
752
+ filename = generate_filename(user_prompt, ".md")
753
+ create_file(filename, file_contents, response, should_save)
754
+ SpeechSynthesis(response)
755
+
756
+ # old - gpt
757
+ #response = chat_with_model(user_prompt, file_contents, model_choice)
758
+ #filename = generate_filename(file_contents, choice)
759
+ #create_file(filename, user_prompt, response, should_save)
760
+
761
+ st.experimental_rerun()
762
+
763
+
764
+ # Function to encode file to base64
765
+ def get_base64_encoded_file(file_path):
766
+ with open(file_path, "rb") as file:
767
+ return base64.b64encode(file.read()).decode()
768
+
769
+ # Function to create a download link
770
+ def get_audio_download_link(file_path):
771
+ base64_file = get_base64_encoded_file(file_path)
772
+ return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">⬇️ Download Audio</a>'
773
+
774
+ # Compose a file sidebar of past encounters
775
+ all_files = glob.glob("*.wav")
776
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
777
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
778
+
779
+ filekey = 'delall'
780
+ if st.sidebar.button("πŸ—‘ Delete All Audio", key=filekey):
781
+ for file in all_files:
782
+ os.remove(file)
783
+ st.experimental_rerun()
784
+
785
+ for file in all_files:
786
+ col1, col2 = st.sidebar.columns([6, 1]) # adjust the ratio as needed
787
+ with col1:
788
+ st.markdown(file)
789
+ if st.button("🎡", key="play_" + file): # play emoji button
790
+ audio_file = open(file, 'rb')
791
+ audio_bytes = audio_file.read()
792
+ st.audio(audio_bytes, format='audio/wav')
793
+ #st.markdown(get_audio_download_link(file), unsafe_allow_html=True)
794
+ #st.text_input(label="", value=file)
795
+ with col2:
796
+ if st.button("πŸ—‘", key="delete_" + file):
797
+ os.remove(file)
798
+ st.experimental_rerun()
799
+
800
+
801
+
802
+ # Feedback
803
+ # Step: Give User a Way to Upvote or Downvote
804
+ with st.expander("Give your feedback πŸ‘", expanded=False):
805
+
806
+ feedback = st.radio("Step 8: Give your feedback", ("πŸ‘ Upvote", "πŸ‘Ž Downvote"))
807
+ if feedback == "πŸ‘ Upvote":
808
+ st.write("You upvoted πŸ‘. Thank you for your feedback!")
809
+ else:
810
+ st.write("You downvoted πŸ‘Ž. Thank you for your feedback!")
811
+
812
+ load_dotenv()
813
+ st.write(css, unsafe_allow_html=True)
814
+ st.header("Chat with documents :books:")
815
+ user_question = st.text_input("Ask a question about your documents:")
816
+ if user_question:
817
+ process_user_input(user_question)
818
+ with st.sidebar:
819
+ st.subheader("Your documents")
820
+ docs = st.file_uploader("import documents", accept_multiple_files=True)
821
+ with st.spinner("Processing"):
822
+ raw = pdf2txt(docs)
823
+ if len(raw) > 0:
824
+ length = str(len(raw))
825
+ text_chunks = txt2chunks(raw)
826
+ vectorstore = vector_store(text_chunks)
827
+ st.session_state.conversation = get_chain(vectorstore)
828
+ st.markdown('# AI Search Index of Length:' + length + ' Created.') # add timing
829
+ filename = generate_filename(raw, 'txt')
830
+ create_file(filename, raw, '', should_save)
831
+
832
+ # 18. Run AI Pipeline
833
+ if __name__ == "__main__":
834
+ whisper_main()
835
+ main()
836
+ #add_Med_Licensing_Exam_Dataset()