awacke1 commited on
Commit
a795c99
1 Parent(s): 3bb2801

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +171 -245
app.py CHANGED
@@ -34,8 +34,8 @@ 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
@@ -92,9 +92,9 @@ def add_Med_Licensing_Exam_Dataset():
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"
@@ -104,7 +104,7 @@ headers = {
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
@@ -128,30 +128,30 @@ def add_witty_humor_buttons():
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='''
@@ -180,7 +180,7 @@ def SpeechSynthesis(result):
180
  </html>
181
  '''
182
 
183
- components.html(documentHTML5, width=1280, height=500)
184
  #return result
185
 
186
 
@@ -191,7 +191,6 @@ def StreamLLMChatResponse(prompt):
191
  endpoint_url = API_URL
192
  hf_token = API_KEY
193
  client = InferenceClient(endpoint_url, token=hf_token)
194
- st.write('Opened HF hub Inference Client for endpoint URL: ' + endpoint_url)
195
  gen_kwargs = dict(
196
  max_new_tokens=512,
197
  top_k=30,
@@ -227,20 +226,11 @@ def StreamLLMChatResponse(prompt):
227
  except:
228
  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).')
229
 
230
- #def query(filename):
231
- # with open(filename, "rb") as f:
232
- # data = f.read
233
- # st.write('Posting request to model ' + API_URL_IE)
234
- # response = requests.post(API_URL_IE, headers=headers, data=data)
235
- # return response.json()
236
-
237
  # 4. Run query with payload
238
  def query(payload):
239
- st.write('Posting request to model ' + API_URL_IE)
240
  response = requests.post(API_URL, headers=headers, json=payload)
241
  st.markdown(response.json())
242
  return response.json()
243
-
244
  def get_output(prompt):
245
  return query({"inputs": prompt})
246
 
@@ -249,8 +239,7 @@ def generate_filename(prompt, file_type):
249
  central = pytz.timezone('US/Central')
250
  safe_date_time = datetime.now(central).strftime("%m%d_%H%M")
251
  replaced_prompt = prompt.replace(" ", "_").replace("\n", "_")
252
- safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:255] # 255 is linux max, 260 is windows max
253
- #safe_prompt = "".join(x for x in replaced_prompt if x.isalnum() or x == "_")[:45]
254
  return f"{safe_date_time}_{safe_prompt}.{file_type}"
255
 
256
  # 6. Speech transcription via OpenAI service
@@ -262,7 +251,6 @@ def transcribe_audio(openai_key, file_path, model):
262
  }
263
  with open(file_path, 'rb') as f:
264
  data = {'file': f}
265
- st.write('Transcribe Audio is Posting request to ' + OPENAI_API_URL)
266
  response = requests.post(OPENAI_API_URL, headers=headers, files=data, data={'model': model})
267
  if response.status_code == 200:
268
  st.write(response.json())
@@ -338,8 +326,6 @@ def get_table_download_link(file_path):
338
  mime_type = 'text/html'
339
  elif ext == '.md':
340
  mime_type = 'text/markdown'
341
- elif ext == '.wav':
342
- mime_type = 'audio/wav'
343
  else:
344
  mime_type = 'application/octet-stream' # general binary data type
345
  href = f'<a href="data:{mime_type};base64,{b64}" target="_blank" download="{file_name}">{file_name}</a>'
@@ -389,7 +375,6 @@ def chat_with_model(prompt, document_section, model_choice='gpt-3.5-turbo'):
389
  res_box = st.empty()
390
  collected_chunks = []
391
  collected_messages = []
392
- st.write('Running prompt with ' + model)
393
  for chunk in openai.ChatCompletion.create(model='gpt-3.5-turbo', messages=conversation, temperature=0.5, stream=True):
394
  collected_chunks.append(chunk)
395
  chunk_message = chunk['choices'][0]['delta']
@@ -463,7 +448,6 @@ def txt2chunks(text):
463
  # Vector Store using FAISS
464
  @st.cache_resource
465
  def vector_store(text_chunks):
466
- st.write('Retrieving OpenAI embeddings')
467
  embeddings = OpenAIEmbeddings(openai_api_key=key)
468
  return FAISS.from_texts(texts=text_chunks, embedding=embeddings)
469
 
@@ -522,22 +506,30 @@ def get_zip_download_link(zip_file):
522
 
523
  # 14. Inference Endpoints for Whisper (best fastest STT) on NVIDIA T4 and Llama (best fastest AGI LLM) on NVIDIA A10
524
  # My Inference Endpoint
525
- # API_URL_IE = f'https://tonpixzfvq3791u9.us-east-1.aws.endpoints.huggingface.cloud'
526
  # Original
527
  #API_URL_IE = "https://api-inference.huggingface.co/models/openai/whisper-small.en"
528
- # Latest small - 1/26
529
- API_URL_IE = "https://api-inference.huggingface.co/models/openai/whisper-small"
530
- #MODEL2 = "openai/whisper-small.en"
531
- #MODEL2_URL = "https://huggingface.co/openai/whisper-small.en"
532
-
533
- HF_KEY = st.secrets['HF_KEY']
 
 
 
 
534
  headers = {
535
  "Authorization": f"Bearer {HF_KEY}",
536
  "Content-Type": "audio/wav"
537
  }
538
 
539
  #@st.cache_resource
540
-
 
 
 
 
541
 
542
  def generate_filename(prompt, file_type):
543
  central = pytz.timezone('US/Central')
@@ -561,28 +553,27 @@ def transcribe_audio(filename):
561
  output = query(filename)
562
  return output
563
 
 
564
  def whisper_main():
565
- #st.title("Speech to Text")
566
- #st.write("Record your speech and get the text.")
567
 
568
  # Audio, transcribe, GPT:
569
  filename = save_and_play_audio(audio_recorder)
570
  if filename is not None:
571
  transcription = transcribe_audio(filename)
572
- try:
573
- transcript = transcription['text']
574
- st.write(transcript)
575
- response = StreamLLMChatResponse(transcript)
576
- filename_txt = generate_filename(transcript, ".txt")
577
- create_file(filename_txt, transcript, response, should_save)
578
- filename_wav = filename_txt.replace('.txt', '.wav')
579
- import shutil
580
- shutil.copyfile(filename, filename_wav)
581
- if os.path.exists(filename):
582
- os.remove(filename)
583
- except:
584
- st.write('Starting Whisper Model on GPU. Please retry in 30 seconds.')
585
 
 
 
 
 
 
 
586
 
587
  import streamlit as st
588
 
@@ -590,156 +581,132 @@ import streamlit as st
590
  def StreamMedChatResponse(topic):
591
  st.write(f"Showing resources or questions related to: {topic}")
592
 
 
 
 
593
 
594
-
595
- def add_medical_exam_buttons():
596
- # Medical exam terminology descriptions
597
- descriptions = {
598
- "White Blood Cells 🌊": "3 Q&A with emojis about types, facts, function, inputs and outputs of white blood cells 🎥",
599
- "CT Imaging🦠": "3 Q&A with emojis on CT Imaging post surgery, how to, what to look for 💊",
600
- "Hematoma 💉": "3 Q&A with emojis about hematoma and infection care and study including bacteria cultures and tests or labs💪",
601
- "Post Surgery Wound Care 🍌": "3 Q&A with emojis on wound care, and good bedside manner 🩸",
602
- "Healing and humor 💊": "3 Q&A with emojis on stories and humor about healing and caregiving 🚑",
603
- "Psychology of bedside manner 🧬": "3 Q&A with emojis on bedside manner and how to make patients feel at ease🛠",
604
- "CT scan 💊": "3 Q&A with analysis on infection using CT scan and packing for skin, cellulitus and fascia 🩺"
605
- }
606
-
607
- # Expander for medical topics
608
- with st.expander("Medical Licensing Exam Topics 📚", expanded=False):
609
- st.markdown("🩺 **Important**: Variety of topics for medical licensing exams.")
610
-
611
- # Create buttons for each description with unique keys
612
- for idx, (label, content) in enumerate(descriptions.items()):
613
- button_key = f"button_{idx}"
614
- if st.button(label, key=button_key):
615
- st.write(f"Running {label}")
616
- input='Create markdown outline for definition of topic ' + label + ' also short quiz with appropriate emojis and definitions for: ' + content
617
- response=StreamLLMChatResponse(input)
618
- filename = generate_filename(response, 'txt')
619
- create_file(filename, input, response, should_save)
620
-
621
- def add_medical_exam_buttons2():
622
- with st.expander("Medical Licensing Exam Topics 📚", expanded=False):
623
- st.markdown("🩺 **Important**: This section provides a variety of medical topics that are often encountered in medical licensing exams.")
624
-
625
- # Define medical exam terminology descriptions
626
  descriptions = {
627
- "White Blood Cells 🌊": "3 Questions and Answers with emojis about white blood cells 🎥",
628
- "CT Imaging🦠": "3 Questions and Answers with emojis about CT Imaging of post surgery abscess, hematoma, and cerosanguiness fluid 💊",
629
- "Hematoma 💉": "3 Questions and Answers with emojis about hematoma and infection and how heat helps white blood cells 💪",
630
- "Post Surgery Wound Care 🍌": "3 Questions and Answers with emojis about wound care and how to help as a caregiver🩸",
631
- "Healing and humor 💊": "3 Questions and Answers with emojis on the use of stories and humor to help patients and family 🚑",
632
- "Psychology of bedside manner 🧬": "3 Questions and Answers with emojis about good bedside manner 🛠",
633
- "CT scan 💊": "3 Questions and Answers with analysis of bacteria and understanding infection using cultures and CT scan 🩺"
 
634
  }
635
 
636
  # Create columns
637
  col1, col2, col3, col4 = st.columns([1, 1, 1, 1], gap="small")
638
 
639
  # Add buttons to columns
640
- if col1.button("Ultrasound with Doppler 🌊"):
641
- StreamLLMChatResponse(descriptions["Ultrasound with Doppler 🌊"])
 
642
 
643
- if col2.button("Oseltamivir 🦠"):
644
- StreamLLMChatResponse(descriptions["Oseltamivir 🦠"])
 
645
 
646
- if col3.button("IM Epinephrine 💉"):
647
- StreamLLMChatResponse(descriptions["IM Epinephrine 💉"])
 
648
 
649
- if col4.button("Hypokalemia 🍌"):
650
- StreamLLMChatResponse(descriptions["Hypokalemia 🍌"])
 
651
 
652
  col5, col6, col7, col8 = st.columns([1, 1, 1, 1], gap="small")
653
 
654
- if col5.button("Succinylcholine 💊"):
655
- StreamLLMChatResponse(descriptions["Succinylcholine 💊"])
 
656
 
657
- if col6.button("Phosphoinositol System 🧬"):
658
- StreamLLMChatResponse(descriptions["Phosphoinositol System 🧬"])
 
 
 
 
 
 
 
 
 
659
 
660
- if col7.button("Ramipril 💊"):
661
- StreamLLMChatResponse(descriptions["Ramipril 💊"])
662
-
663
-
664
 
665
  # 17. Main
666
  def main():
667
 
668
- #st.title("GAIA - Medical License Exam Testing")
669
  prompt = f"Write ten funny jokes that are tweet length stories that make you laugh. Show as markdown outline with emojis for each."
670
 
671
  # Add Wit and Humor buttons
672
  # add_witty_humor_buttons()
673
- add_medical_exam_buttons()
674
-
675
-
676
- with st.expander("Prompts 📚", expanded=False):
677
-
678
- example_input = st.text_input("Enter your prompt text for Llama:", value=prompt, help="Enter text to get a response from DromeLlama.")
679
- if st.button("Run Prompt With Llama model", help="Click to run the prompt."):
680
- try:
681
- response=StreamLLMChatResponse(example_input)
682
- create_file(filename, example_input, response, should_save)
683
- except:
684
- st.write('Llama model is asleep. Starting now on A10 GPU. Please wait one minute then retry. KEDA triggered.')
685
 
686
- openai.api_key = os.getenv('OPENAI_API_KEY')
687
- if openai.api_key == None: openai.api_key = st.secrets['OPENAI_API_KEY']
688
-
689
- menu = ["txt", "htm", "xlsx", "csv", "md", "py"]
690
- choice = st.sidebar.selectbox("Output File Type:", menu)
691
-
692
- model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
693
-
694
- user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
695
- collength, colupload = st.columns([2,3]) # adjust the ratio as needed
696
- with collength:
697
- max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
698
- with colupload:
699
- uploaded_file = st.file_uploader("Add a file for context:", type=["pdf", "xml", "json", "xlsx", "csv", "html", "htm", "md", "txt"])
700
- document_sections = deque()
701
- document_responses = {}
702
- if uploaded_file is not None:
703
- file_content = read_file_content(uploaded_file, max_length)
704
- document_sections.extend(divide_document(file_content, max_length))
705
- if len(document_sections) > 0:
706
- if st.button("👁️ View Upload"):
707
- st.markdown("**Sections of the uploaded file:**")
708
- for i, section in enumerate(list(document_sections)):
709
- st.markdown(f"**Section {i+1}**\n{section}")
710
- st.markdown("**Chat with the model:**")
711
  for i, section in enumerate(list(document_sections)):
712
- if i in document_responses:
713
- st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
714
- else:
715
- if st.button(f"Chat about Section {i+1}"):
716
- st.write('Reasoning with your inputs...')
717
- #response = chat_with_model(user_prompt, section, model_choice)
718
- st.write('Response:')
719
- st.write(response)
720
- document_responses[i] = response
721
- filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
722
- create_file(filename, user_prompt, response, should_save)
723
- st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
724
- if st.button('💬 Chat'):
725
- st.write('Reasoning with your inputs...')
726
- user_prompt_sections = divide_prompt(user_prompt, max_length)
727
- full_response = ''
728
- for prompt_section in user_prompt_sections:
729
- response = chat_with_model(prompt_section, ''.join(list(document_sections)), model_choice)
730
- full_response += response + '\n' # Combine the responses
731
- response = full_response
732
- st.write('Response:')
733
- st.write(response)
734
- filename = generate_filename(user_prompt, choice)
735
- create_file(filename, user_prompt, response, should_save)
736
- #st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
737
-
738
- # Compose a file sidebar of markdown md files:
739
- all_files = glob.glob("*.md")
740
- all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
 
 
 
741
  all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
742
- if st.sidebar.button("🗑 Delete All Text"):
743
  for file in all_files:
744
  os.remove(file)
745
  st.experimental_rerun()
@@ -795,77 +762,36 @@ def main():
795
 
796
  st.experimental_rerun()
797
 
798
-
799
- # Function to encode file to base64
800
- def get_base64_encoded_file(file_path):
801
- with open(file_path, "rb") as file:
802
- return base64.b64encode(file.read()).decode()
803
-
804
- # Function to create a download link
805
- def get_audio_download_link(file_path):
806
- base64_file = get_base64_encoded_file(file_path)
807
- return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">⬇️ Download Audio</a>'
808
-
809
- # Compose a file sidebar of past encounters
810
- all_files = glob.glob("*.wav")
811
- all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
812
- all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
813
-
814
- filekey = 'delall'
815
- if st.sidebar.button("🗑 Delete All Audio", key=filekey):
816
- for file in all_files:
817
- os.remove(file)
818
- st.experimental_rerun()
819
-
820
- for file in all_files:
821
- col1, col2 = st.sidebar.columns([6, 1]) # adjust the ratio as needed
822
- with col1:
823
- st.markdown(file)
824
- if st.button("🎵", key="play_" + file): # play emoji button
825
- audio_file = open(file, 'rb')
826
- audio_bytes = audio_file.read()
827
- st.audio(audio_bytes, format='audio/wav')
828
- #st.markdown(get_audio_download_link(file), unsafe_allow_html=True)
829
- #st.text_input(label="", value=file)
830
- with col2:
831
- if st.button("🗑", key="delete_" + file):
832
- os.remove(file)
833
- st.experimental_rerun()
834
-
835
-
836
-
837
  # Feedback
838
  # Step: Give User a Way to Upvote or Downvote
839
- with st.expander("Give your feedback 👍", expanded=False):
840
-
841
- feedback = st.radio("Step 8: Give your feedback", ("👍 Upvote", "👎 Downvote"))
842
- if feedback == "👍 Upvote":
843
- st.write("You upvoted 👍. Thank you for your feedback!")
844
- else:
845
- st.write("You downvoted 👎. Thank you for your feedback!")
846
-
847
- load_dotenv()
848
- st.write(css, unsafe_allow_html=True)
849
- st.header("Chat with documents :books:")
850
- user_question = st.text_input("Ask a question about your documents:")
851
- if user_question:
852
- process_user_input(user_question)
853
- with st.sidebar:
854
- st.subheader("Your documents")
855
- docs = st.file_uploader("import documents", accept_multiple_files=True)
856
- with st.spinner("Processing"):
857
- raw = pdf2txt(docs)
858
- if len(raw) > 0:
859
- length = str(len(raw))
860
- text_chunks = txt2chunks(raw)
861
- vectorstore = vector_store(text_chunks)
862
- st.session_state.conversation = get_chain(vectorstore)
863
- st.markdown('# AI Search Index of Length:' + length + ' Created.') # add timing
864
- filename = generate_filename(raw, 'txt')
865
- create_file(filename, raw, '', should_save)
866
 
867
  # 18. Run AI Pipeline
868
  if __name__ == "__main__":
869
  whisper_main()
870
  main()
871
- #add_Med_Licensing_Exam_Dataset()
 
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
 
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"
 
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
 
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='''
 
180
  </html>
181
  '''
182
 
183
+ components.html(documentHTML5, width=1280, height=1024)
184
  #return result
185
 
186
 
 
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,
 
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
 
 
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
 
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())
 
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>'
 
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']
 
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
 
 
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')
 
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
 
 
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()
 
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()