awacke1 commited on
Commit
1b24bfb
โ€ข
1 Parent(s): 55151e6

Create backup-010924.app.py

Browse files
Files changed (1) hide show
  1. backup-010924.app.py +797 -0
backup-010924.app.py ADDED
@@ -0,0 +1,797 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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", layout="wide")
38
+ st.markdown('(Inference Endpoints)[https://ui.endpoints.huggingface.co/awacke1/endpoints]')
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 anyone to write a discharge plan. List the 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("Generate 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("Medical Jokes ๐Ÿ’‰"):
143
+ StreamLLMChatResponse(descriptions["Medical Jokes ๐Ÿ’‰"])
144
+
145
+ if col5.button("Minnesota Humor โ„๏ธ"):
146
+ StreamLLMChatResponse(descriptions["Minnesota Humor โ„๏ธ"])
147
+
148
+ if col6.button("Top Funny Stories ๐Ÿ“–"):
149
+ StreamLLMChatResponse(descriptions["Top Funny Stories ๐Ÿ“–"])
150
+
151
+ col7 = st.columns(1, gap="small")
152
+
153
+ if col7[0].button("More Funny Rhymes ๐ŸŽ™๏ธ"):
154
+ StreamLLMChatResponse(descriptions["More Funny Rhymes ๐ŸŽ™๏ธ"])
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 == "_")[:45]
243
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
244
+
245
+ # 6. Speech transcription via OpenAI service
246
+ def transcribe_audio(openai_key, file_path, model):
247
+ openai.api_key = openai_key
248
+ OPENAI_API_URL = "https://api.openai.com/v1/audio/transcriptions"
249
+ headers = {
250
+ "Authorization": f"Bearer {openai_key}",
251
+ }
252
+ with open(file_path, 'rb') as f:
253
+ data = {'file': f}
254
+ response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
255
+ if response.status_code == 200:
256
+ st.write(response.json())
257
+ chatResponse = chat_with_model(response.json().get('text'), '') # *************************************
258
+ transcript = response.json().get('text')
259
+ filename = generate_filename(transcript, 'txt')
260
+ response = chatResponse
261
+ user_prompt = transcript
262
+ create_file(filename, user_prompt, response, should_save)
263
+ return transcript
264
+ else:
265
+ st.write(response.json())
266
+ st.error("Error in API call.")
267
+ return None
268
+
269
+ # 7. Auto stop on silence audio control for recording WAV files
270
+ def save_and_play_audio(audio_recorder):
271
+ audio_bytes = audio_recorder(key='audio_recorder')
272
+ if audio_bytes:
273
+ filename = generate_filename("Recording", "wav")
274
+ with open(filename, 'wb') as f:
275
+ f.write(audio_bytes)
276
+ st.audio(audio_bytes, format="audio/wav")
277
+ return filename
278
+ return None
279
+
280
+ # 8. File creator that interprets type and creates output file for text, markdown and code
281
+ def create_file(filename, prompt, response, should_save=True):
282
+ if not should_save:
283
+ return
284
+ base_filename, ext = os.path.splitext(filename)
285
+ if ext in ['.txt', '.htm', '.md']:
286
+ with open(f"{base_filename}.md", 'w') as file:
287
+ try:
288
+ content = prompt.strip() + '\r\n' + response
289
+ file.write(content)
290
+ except:
291
+ st.write('.')
292
+
293
+ #has_python_code = re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response)
294
+ #has_python_code = bool(re.search(r"```python([\s\S]*?)```", prompt.strip() + '\r\n' + response))
295
+ #if has_python_code:
296
+ # python_code = re.findall(r"```python([\s\S]*?)```", response)[0].strip()
297
+ # with open(f"{base_filename}-Code.py", 'w') as file:
298
+ # file.write(python_code)
299
+ # with open(f"{base_filename}.md", 'w') as file:
300
+ # content = prompt.strip() + '\r\n' + response
301
+ # file.write(content)
302
+
303
+ def truncate_document(document, length):
304
+ return document[:length]
305
+ def divide_document(document, max_length):
306
+ return [document[i:i+max_length] for i in range(0, len(document), max_length)]
307
+
308
+ # 9. Sidebar with UI controls to review and re-run prompts and continue responses
309
+ @st.cache_resource
310
+ def get_table_download_link(file_path):
311
+ with open(file_path, 'r') as file:
312
+ data = file.read()
313
+
314
+ b64 = base64.b64encode(data.encode()).decode()
315
+ file_name = os.path.basename(file_path)
316
+ ext = os.path.splitext(file_name)[1] # get the file extension
317
+ if ext == '.txt':
318
+ mime_type = 'text/plain'
319
+ elif ext == '.py':
320
+ mime_type = 'text/plain'
321
+ elif ext == '.xlsx':
322
+ mime_type = 'text/plain'
323
+ elif ext == '.csv':
324
+ mime_type = 'text/plain'
325
+ elif ext == '.htm':
326
+ mime_type = 'text/html'
327
+ elif ext == '.md':
328
+ mime_type = 'text/markdown'
329
+ else:
330
+ mime_type = 'application/octet-stream' # general binary data type
331
+ href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
332
+ return href
333
+
334
+
335
+ def CompressXML(xml_text):
336
+ root = ET.fromstring(xml_text)
337
+ for elem in list(root.iter()):
338
+ if isinstance(elem.tag, str) and 'Comment' in elem.tag:
339
+ elem.parent.remove(elem)
340
+ return ET.tostring(root, encoding='unicode', method="xml")
341
+
342
+ # 10. Read in and provide UI for past files
343
+ @st.cache_resource
344
+ def read_file_content(file,max_length):
345
+ if file.type == "application/json":
346
+ content = json.load(file)
347
+ return str(content)
348
+ elif file.type == "text/html" or file.type == "text/htm":
349
+ content = BeautifulSoup(file, "html.parser")
350
+ return content.text
351
+ elif file.type == "application/xml" or file.type == "text/xml":
352
+ tree = ET.parse(file)
353
+ root = tree.getroot()
354
+ xml = CompressXML(ET.tostring(root, encoding='unicode'))
355
+ return xml
356
+ elif file.type == "text/markdown" or file.type == "text/md":
357
+ md = mistune.create_markdown()
358
+ content = md(file.read().decode())
359
+ return content
360
+ elif file.type == "text/plain":
361
+ return file.getvalue().decode()
362
+ else:
363
+ return ""
364
+
365
+ # 11. Chat with GPT - Caution on quota - now favoring fastest AI pipeline STT Whisper->LLM Llama->TTS
366
+ @st.cache_resource
367
+ def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):
368
+ model = model_choice
369
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
370
+ conversation.append({'role': 'user', 'content': prompt})
371
+ if len(document_section)>0:
372
+ conversation.append({'role': 'assistant', 'content': document_section})
373
+ start_time = time.time()
374
+ report = []
375
+ res_box = st.empty()
376
+ collected_chunks = []
377
+ collected_messages = []
378
+ for chunk in openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=conversation, temperature=0.5, stream=True):
379
+ collected_chunks.append(chunk)
380
+ chunk_message = chunk['choices'][0]['delta']
381
+ collected_messages.append(chunk_message)
382
+ content=chunk["choices"][0].get("delta",{}).get("content")
383
+ try:
384
+ report.append(content)
385
+ if len(content) > 0:
386
+ result = "".join(report).strip()
387
+ res_box.markdown(f'*{result}*')
388
+ except:
389
+ st.write(' ')
390
+ full_reply_content = ''.join([m.get('content', '') for m in collected_messages])
391
+ st.write("Elapsed time:")
392
+ st.write(time.time() - start_time)
393
+ return full_reply_content
394
+
395
+ # 12. Embedding VectorDB for LLM query of documents to text to compress inputs and prompt together as Chat memory using Langchain
396
+ @st.cache_resource
397
+ def chat_with_file_contents(prompt, file_content, model_choice='gpt-3.5-turbo'):
398
+ conversation = [{'role': 'system', 'content': 'You are a helpful assistant.'}]
399
+ conversation.append({'role': 'user', 'content': prompt})
400
+ if len(file_content)>0:
401
+ conversation.append({'role': 'assistant', 'content': file_content})
402
+ response = openai.ChatCompletion.create(model=model_choice, messages=conversation)
403
+ return response['choices'][0]['message']['content']
404
+
405
+ def extract_mime_type(file):
406
+ if isinstance(file, str):
407
+ pattern = r"type='(.*?)'"
408
+ match = re.search(pattern, file)
409
+ if match:
410
+ return match.group(1)
411
+ else:
412
+ raise ValueError(f"Unable to extract MIME type from {file}")
413
+ elif isinstance(file, streamlit.UploadedFile):
414
+ return file.type
415
+ else:
416
+ raise TypeError("Input should be a string or a streamlit.UploadedFile object")
417
+
418
+ def extract_file_extension(file):
419
+ # get the file name directly from the UploadedFile object
420
+ file_name = file.name
421
+ pattern = r".*?\.(.*?)$"
422
+ match = re.search(pattern, file_name)
423
+ if match:
424
+ return match.group(1)
425
+ else:
426
+ raise ValueError(f"Unable to extract file extension from {file_name}")
427
+
428
+ # Normalize input as text from PDF and other formats
429
+ @st.cache_resource
430
+ def pdf2txt(docs):
431
+ text = ""
432
+ for file in docs:
433
+ file_extension = extract_file_extension(file)
434
+ st.write(f"File type extension: {file_extension}")
435
+ if file_extension.lower() in ['py', 'txt', 'html', 'htm', 'xml', 'json']:
436
+ text += file.getvalue().decode('utf-8')
437
+ elif file_extension.lower() == 'pdf':
438
+ from PyPDF2 import PdfReader
439
+ pdf = PdfReader(BytesIO(file.getvalue()))
440
+ for page in range(len(pdf.pages)):
441
+ text += pdf.pages[page].extract_text() # new PyPDF2 syntax
442
+ return text
443
+
444
+ def txt2chunks(text):
445
+ text_splitter = CharacterTextSplitter(separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len)
446
+ return text_splitter.split_text(text)
447
+
448
+ # Vector Store using FAISS
449
+ @st.cache_resource
450
+ def vector_store(text_chunks):
451
+ embeddings = OpenAIEmbeddings(openai_api_key=key)
452
+ return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
453
+
454
+ # Memory and Retrieval chains
455
+ @st.cache_resource
456
+ def get_chain(vectorstore):
457
+ llm = ChatOpenAI()
458
+ memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True)
459
+ return ConversationalRetrievalChain.from_llm(llm=llm, retriever=vectorstore.as_retriever(), memory=memory)
460
+
461
+ def process_user_input(user_question):
462
+ response = st.session_state.conversation({'question': user_question})
463
+ st.session_state.chat_history = response['chat_history']
464
+ for i, message in enumerate(st.session_state.chat_history):
465
+ template = user_template if i % 2 == 0 else bot_template
466
+ st.write(template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
467
+ filename = generate_filename(user_question, 'txt')
468
+ response = message.content
469
+ user_prompt = user_question
470
+ create_file(filename, user_prompt, response, should_save)
471
+
472
+ def divide_prompt(prompt, max_length):
473
+ words = prompt.split()
474
+ chunks = []
475
+ current_chunk = []
476
+ current_length = 0
477
+ for word in words:
478
+ if len(word) + current_length <= max_length:
479
+ current_length += len(word) + 1
480
+ current_chunk.append(word)
481
+ else:
482
+ chunks.append(' '.join(current_chunk))
483
+ current_chunk = [word]
484
+ current_length = len(word)
485
+ chunks.append(' '.join(current_chunk))
486
+ return chunks
487
+
488
+
489
+ # 13. Provide way of saving all and deleting all to give way of reviewing output and saving locally before clearing it
490
+
491
+ @st.cache_resource
492
+ def create_zip_of_files(files):
493
+ zip_name = "all_files.zip"
494
+ with zipfile.ZipFile(zip_name, 'w') as zipf:
495
+ for file in files:
496
+ zipf.write(file)
497
+ return zip_name
498
+
499
+ @st.cache_resource
500
+ def get_zip_download_link(zip_file):
501
+ with open(zip_file, 'rb') as f:
502
+ data = f.read()
503
+ b64 = base64.b64encode(data).decode()
504
+ href = f'<a href="data:application/zip;base64,{b64}" download="{zip_file}">Download All</a>'
505
+ return href
506
+
507
+ # 14. Inference Endpoints for Whisper (best fastest STT) on NVIDIA T4 and Llama (best fastest AGI LLM) on NVIDIA A10
508
+ # My Inference Endpoint
509
+ #API_URL_IE = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
510
+ # Original
511
+ #API_URL_IE = "https://api-inference.huggingface.co/models/openai/whisper-small.en"
512
+ # A10 Inference Endpoint for whisper large tests
513
+ API_URL_IE = "https://hifdvffh2em0wn50.us-east-1.aws.endpoints.huggingface.cloud"
514
+
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
+ headers = {
523
+ "Authorization": f"Bearer {HF_KEY}",
524
+ "Content-Type": "audio/wav"
525
+ }
526
+
527
+ #@st.cache_resource
528
+ def query(filename):
529
+ with open(filename, "rb") as f:
530
+ data = f.read()
531
+ response = requests.post(API_URL_IE, headers=headers, data=data)
532
+ return response.json()
533
+
534
+ def generate_filename(prompt, file_type):
535
+ central = pytz.timezone('US/Central')
536
+ safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
537
+ replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
538
+ safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:90]
539
+ return f"{safe_date_time}_{safe_prompt}.{file_type}"
540
+
541
+ # 15. Audio recorder to Wav file
542
+ def save_and_play_audio(audio_recorder):
543
+ audio_bytes = audio_recorder()
544
+ if audio_bytes:
545
+ filename = generate_filename("Recording", "wav")
546
+ with open(filename, 'wb') as f:
547
+ f.write(audio_bytes)
548
+ st.audio(audio_bytes, format="audio/wav")
549
+ return filename
550
+
551
+ # 16. Speech transcription to file output
552
+ def transcribe_audio(filename):
553
+ output = query(filename)
554
+ return output
555
+
556
+
557
+ def whisper_main():
558
+ st.title("1๐ŸชLlama๐Ÿฆ™Whisperer")
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
+
567
+ transcript = transcription['text']
568
+ #except:
569
+ #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).')
570
+
571
+ st.write(transcript)
572
+ response = StreamLLMChatResponse(transcript)
573
+ # st.write(response) - redundant with streaming result?
574
+ filename = generate_filename(transcript, ".txt")
575
+ create_file(filename, transcript, response, should_save)
576
+ #st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
577
+
578
+ import streamlit as st
579
+
580
+ # Sample function to demonstrate a response, replace with your own logic
581
+ def StreamMedChatResponse(topic):
582
+ st.write(f"Showing resources or questions related to: {topic}")
583
+
584
+ def add_multi_system_agent_topics():
585
+ with st.expander("Multi-System Agent AI Topics ๐Ÿค–", expanded=True):
586
+ st.markdown("๐Ÿค– **Explore Multi-System Agent AI Topics**: This section provides a variety of topics related to multi-system agent AI systems.")
587
+
588
+ # Define multi-system agent AI topics and descriptions
589
+ descriptions = {
590
+ "Reinforcement Learning ๐ŸŽฎ": "Questions related to reinforcement learning algorithms and applications ๐Ÿ•น๏ธ",
591
+ "Natural Language Processing ๐Ÿ—ฃ๏ธ": "Questions about natural language processing techniques and chatbot development ๐Ÿ—จ๏ธ",
592
+ "Multi-Agent Systems ๐Ÿค": "Questions pertaining to multi-agent systems and cooperative AI interactions ๐Ÿค–",
593
+ "Conversational AI ๐Ÿ—จ๏ธ": "Questions on building conversational AI agents and chatbots for various platforms ๐Ÿ’ฌ",
594
+ "Distributed AI Systems ๐ŸŒ": "Questions about distributed AI systems and their implementation in networked environments ๐ŸŒ",
595
+ "AI Ethics and Bias ๐Ÿค”": "Questions related to ethics and bias considerations in AI systems and decision-making ๐Ÿง ",
596
+ "AI in Healthcare ๐Ÿฅ": "Questions about the application of AI in healthcare and medical diagnosis ๐Ÿฉบ",
597
+ "AI in Autonomous Vehicles ๐Ÿš—": "Questions on the use of AI in autonomous vehicles and self-driving technology ๐Ÿš—"
598
+ }
599
+
600
+ # Create columns
601
+ col1, col2, col3, col4 = st.columns([1, 1, 1, 1], gap="small")
602
+
603
+ # Add buttons to columns
604
+ if col1.button("Reinforcement Learning ๐ŸŽฎ"):
605
+ st.write(descriptions["Reinforcement Learning ๐ŸŽฎ"])
606
+ StreamLLMChatResponse(descriptions["Reinforcement Learning ๐ŸŽฎ"])
607
+
608
+ if col2.button("Natural Language Processing ๐Ÿ—ฃ๏ธ"):
609
+ st.write(descriptions["Natural Language Processing ๐Ÿ—ฃ๏ธ"])
610
+ StreamLLMChatResponse(descriptions["Natural Language Processing ๐Ÿ—ฃ๏ธ"])
611
+
612
+ if col3.button("Multi-Agent Systems ๐Ÿค"):
613
+ st.write(descriptions["Multi-Agent Systems ๐Ÿค"])
614
+ StreamLLMChatResponse(descriptions["Multi-Agent Systems ๐Ÿค"])
615
+
616
+ if col4.button("Conversational AI ๐Ÿ—จ๏ธ"):
617
+ st.write(descriptions["Conversational AI ๐Ÿ—จ๏ธ"])
618
+ StreamLLMChatResponse(descriptions["Conversational AI ๐Ÿ—จ๏ธ"])
619
+
620
+ col5, col6, col7, col8 = st.columns([1, 1, 1, 1], gap="small")
621
+
622
+ if col5.button("Distributed AI Systems ๐ŸŒ"):
623
+ st.write(descriptions["Distributed AI Systems ๐ŸŒ"])
624
+ StreamLLMChatResponse(descriptions["Distributed AI Systems ๐ŸŒ"])
625
+
626
+ if col6.button("AI Ethics and Bias ๐Ÿค”"):
627
+ st.write(descriptions["AI Ethics and Bias ๐Ÿค”"])
628
+ StreamLLMChatResponse(descriptions["AI Ethics and Bias ๐Ÿค”"])
629
+
630
+ if col7.button("AI in Healthcare ๐Ÿฅ"):
631
+ st.write(descriptions["AI in Healthcare ๐Ÿฅ"])
632
+ StreamLLMChatResponse(descriptions["AI in Healthcare ๐Ÿฅ"])
633
+
634
+ if col8.button("AI in Autonomous Vehicles ๐Ÿš—"):
635
+ st.write(descriptions["AI in Autonomous Vehicles ๐Ÿš—"])
636
+ StreamLLMChatResponse(descriptions["AI in Autonomous Vehicles ๐Ÿš—"])
637
+
638
+
639
+ # 17. Main
640
+ def main():
641
+
642
+ st.title("Try Some Topics:")
643
+ prompt = f"Write ten funny jokes that are tweet length stories that make you laugh. Show as markdown outline with emojis for each."
644
+
645
+ # Add Wit and Humor buttons
646
+ # add_witty_humor_buttons()
647
+ # Calling the function to add the multi-system agent AI topics buttons
648
+ add_multi_system_agent_topics()
649
+
650
+ example_input = st.text_input("Enter your example text:", value=prompt, help="Enter text to get a response from DromeLlama.")
651
+ if st.button("Run Prompt With DromeLlama", help="Click to run the prompt."):
652
+ try:
653
+ StreamLLMChatResponse(example_input)
654
+ except:
655
+ st.write('DromeLlama is asleep. Starting up now on A10 - please give 5 minutes then retry as KEDA scales up from zero to activate running container(s).')
656
+
657
+ openai.api_key = os.getenv('OPENAI_KEY')
658
+ menu = ["txt", "htm", "xlsx", "csv", "md", "py"]
659
+ choice = st.sidebar.selectbox("Output File Type:", menu)
660
+ model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
661
+ user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
662
+ collength, colupload = st.columns([2,3]) # adjust the ratio as needed
663
+ with collength:
664
+ max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
665
+ with colupload:
666
+ uploaded_file = st.file_uploader("Add a file for context:", type=["pdf", "xml", "json", "xlsx", "csv", "html", "htm", "md", "txt"])
667
+ document_sections = deque()
668
+ document_responses = {}
669
+ if uploaded_file is not None:
670
+ file_content = read_file_content(uploaded_file, max_length)
671
+ document_sections.extend(divide_document(file_content, max_length))
672
+ if len(document_sections) > 0:
673
+ if st.button("๐Ÿ‘๏ธ View Upload"):
674
+ st.markdown("**Sections of the uploaded file:**")
675
+ for i, section in enumerate(list(document_sections)):
676
+ st.markdown(f"**Section {i+1}**\n{section}")
677
+ st.markdown("**Chat with the model:**")
678
+ for i, section in enumerate(list(document_sections)):
679
+ if i in document_responses:
680
+ st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
681
+ else:
682
+ if st.button(f"Chat about Section {i+1}"):
683
+ st.write('Reasoning with your inputs...')
684
+ response = chat_with_model(user_prompt, section, model_choice)
685
+ st.write('Response:')
686
+ st.write(response)
687
+ document_responses[i] = response
688
+ filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
689
+ create_file(filename, user_prompt, response, should_save)
690
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
691
+ if st.button('๐Ÿ’ฌ Chat'):
692
+ st.write('Reasoning with your inputs...')
693
+ user_prompt_sections = divide_prompt(user_prompt, max_length)
694
+ full_response = ''
695
+ for prompt_section in user_prompt_sections:
696
+ response = chat_with_model(prompt_section, ''.join(list(document_sections)), model_choice)
697
+ full_response += response + '\n' # Combine the responses
698
+ response = full_response
699
+ st.write('Response:')
700
+ st.write(response)
701
+ filename = generate_filename(user_prompt, choice)
702
+ create_file(filename, user_prompt, response, should_save)
703
+ st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
704
+
705
+ # Compose a file sidebar of past encounters
706
+ all_files = glob.glob("*.*")
707
+ all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 20] # exclude files with short names
708
+ all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
709
+ if st.sidebar.button("๐Ÿ—‘ Delete All"):
710
+ for file in all_files:
711
+ os.remove(file)
712
+ st.experimental_rerun()
713
+ if st.sidebar.button("โฌ‡๏ธ Download All"):
714
+ zip_file = create_zip_of_files(all_files)
715
+ st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
716
+ file_contents=''
717
+ next_action=''
718
+ for file in all_files:
719
+ col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
720
+ with col1:
721
+ if st.button("๐ŸŒ", key="md_"+file): # md emoji button
722
+ with open(file, 'r') as f:
723
+ file_contents = f.read()
724
+ next_action='md'
725
+ with col2:
726
+ st.markdown(get_table_download_link(file), unsafe_allow_html=True)
727
+ with col3:
728
+ if st.button("๐Ÿ“‚", key="open_"+file): # open emoji button
729
+ with open(file, 'r') as f:
730
+ file_contents = f.read()
731
+ next_action='open'
732
+ with col4:
733
+ if st.button("๐Ÿ”", key="read_"+file): # search emoji button
734
+ with open(file, 'r') as f:
735
+ file_contents = f.read()
736
+ next_action='search'
737
+ with col5:
738
+ if st.button("๐Ÿ—‘", key="delete_"+file):
739
+ os.remove(file)
740
+ st.experimental_rerun()
741
+
742
+
743
+ if len(file_contents) > 0:
744
+ if next_action=='open':
745
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
746
+ if next_action=='md':
747
+ st.markdown(file_contents)
748
+ if next_action=='search':
749
+ file_content_area = st.text_area("File Contents:", file_contents, height=500)
750
+ st.write('Reasoning with your inputs...')
751
+
752
+ # new - llama
753
+ response = StreamLLMChatResponse(file_contents)
754
+ filename = generate_filename(user_prompt, ".md")
755
+ create_file(filename, file_contents, response, should_save)
756
+ SpeechSynthesis(response)
757
+
758
+ # old - gpt
759
+ #response = chat_with_model(user_prompt, file_contents, model_choice)
760
+ #filename = generate_filename(file_contents, choice)
761
+ #create_file(filename, user_prompt, response, should_save)
762
+
763
+ st.experimental_rerun()
764
+
765
+ # Feedback
766
+ # Step: Give User a Way to Upvote or Downvote
767
+ feedback = st.radio("Step 8: Give your feedback", ("๐Ÿ‘ Upvote", "๐Ÿ‘Ž Downvote"))
768
+ if feedback == "๐Ÿ‘ Upvote":
769
+ st.write("You upvoted ๐Ÿ‘. Thank you for your feedback!")
770
+ else:
771
+ st.write("You downvoted ๐Ÿ‘Ž. Thank you for your feedback!")
772
+
773
+ load_dotenv()
774
+ st.write(css, unsafe_allow_html=True)
775
+ st.header("Chat with documents :books:")
776
+ user_question = st.text_input("Ask a question about your documents:")
777
+ if user_question:
778
+ process_user_input(user_question)
779
+ with st.sidebar:
780
+ st.subheader("Your documents")
781
+ docs = st.file_uploader("import documents", accept_multiple_files=True)
782
+ with st.spinner("Processing"):
783
+ raw = pdf2txt(docs)
784
+ if len(raw) > 0:
785
+ length = str(len(raw))
786
+ text_chunks = txt2chunks(raw)
787
+ vectorstore = vector_store(text_chunks)
788
+ st.session_state.conversation = get_chain(vectorstore)
789
+ st.markdown('# AI Search Index of Length:' + length + ' Created.') # add timing
790
+ filename = generate_filename(raw, 'txt')
791
+ create_file(filename, raw, '', should_save)
792
+
793
+ # 18. Run AI Pipeline
794
+ if __name__ == "__main__":
795
+ whisper_main()
796
+ main()
797
+ add_Med_Licensing_Exam_Dataset()