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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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=500)
|
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 |
+
|
587 |
+
|
588 |
+
def add_medical_exam_buttons():
|
589 |
+
# Medical exam terminology descriptions
|
590 |
+
descriptions = {
|
591 |
+
"White Blood Cells π": "3 Q&A with emojis about types, facts, function, inputs and outputs of white blood cells π₯",
|
592 |
+
"CT Imagingπ¦ ": "3 Q&A with emojis on CT Imaging post surgery, how to, what to look for π",
|
593 |
+
"Hematoma π": "3 Q&A with emojis about hematoma and infection care and study including bacteria cultures and tests or labsπͺ",
|
594 |
+
"Post Surgery Wound Care π": "3 Q&A with emojis on wound care, and good bedside manner π©Έ",
|
595 |
+
"Healing and humor π": "3 Q&A with emojis on stories and humor about healing and caregiving π",
|
596 |
+
"Psychology of bedside manner π§¬": "3 Q&A with emojis on bedside manner and how to make patients feel at easeπ ",
|
597 |
+
"CT scan π": "3 Q&A with analysis on infection using CT scan and packing for skin, cellulitus and fascia π©Ί"
|
598 |
+
}
|
599 |
+
|
600 |
+
# Expander for medical topics
|
601 |
+
with st.expander("Medical Licensing Exam Topics π", expanded=False):
|
602 |
+
st.markdown("π©Ί **Important**: Variety of topics for medical licensing exams.")
|
603 |
+
|
604 |
+
# Create buttons for each description with unique keys
|
605 |
+
for idx, (label, content) in enumerate(descriptions.items()):
|
606 |
+
button_key = f"button_{idx}"
|
607 |
+
if st.button(label, key=button_key):
|
608 |
+
st.write(f"Running {label}")
|
609 |
+
input='Create markdown outline for definition of topic ' + label + ' also short quiz with appropriate emojis and definitions for: ' + content
|
610 |
+
response=StreamLLMChatResponse(input)
|
611 |
+
filename = generate_filename(response, 'txt')
|
612 |
+
create_file(filename, input, response, should_save)
|
613 |
+
|
614 |
+
def add_medical_exam_buttons2():
|
615 |
+
with st.expander("Medical Licensing Exam Topics π", expanded=False):
|
616 |
+
st.markdown("π©Ί **Important**: This section provides a variety of medical topics that are often encountered in medical licensing exams.")
|
617 |
+
|
618 |
+
# Define medical exam terminology descriptions
|
619 |
+
descriptions = {
|
620 |
+
"White Blood Cells π": "3 Questions and Answers with emojis about white blood cells π₯",
|
621 |
+
"CT Imagingπ¦ ": "3 Questions and Answers with emojis about CT Imaging of post surgery abscess, hematoma, and cerosanguiness fluid π",
|
622 |
+
"Hematoma π": "3 Questions and Answers with emojis about hematoma and infection and how heat helps white blood cells πͺ",
|
623 |
+
"Post Surgery Wound Care π": "3 Questions and Answers with emojis about wound care and how to help as a caregiverπ©Έ",
|
624 |
+
"Healing and humor π": "3 Questions and Answers with emojis on the use of stories and humor to help patients and family π",
|
625 |
+
"Psychology of bedside manner π§¬": "3 Questions and Answers with emojis about good bedside manner π ",
|
626 |
+
"CT scan π": "3 Questions and Answers with analysis of bacteria and understanding infection using cultures and CT scan π©Ί"
|
627 |
+
}
|
628 |
+
|
629 |
+
# Create columns
|
630 |
+
col1, col2, col3, col4 = st.columns([1, 1, 1, 1], gap="small")
|
631 |
+
|
632 |
+
# Add buttons to columns
|
633 |
+
if col1.button("Ultrasound with Doppler π"):
|
634 |
+
StreamLLMChatResponse(descriptions["Ultrasound with Doppler π"])
|
635 |
+
|
636 |
+
if col2.button("Oseltamivir π¦ "):
|
637 |
+
StreamLLMChatResponse(descriptions["Oseltamivir π¦ "])
|
638 |
+
|
639 |
+
if col3.button("IM Epinephrine π"):
|
640 |
+
StreamLLMChatResponse(descriptions["IM Epinephrine π"])
|
641 |
+
|
642 |
+
if col4.button("Hypokalemia π"):
|
643 |
+
StreamLLMChatResponse(descriptions["Hypokalemia π"])
|
644 |
+
|
645 |
+
col5, col6, col7, col8 = st.columns([1, 1, 1, 1], gap="small")
|
646 |
+
|
647 |
+
if col5.button("Succinylcholine π"):
|
648 |
+
StreamLLMChatResponse(descriptions["Succinylcholine π"])
|
649 |
+
|
650 |
+
if col6.button("Phosphoinositol System π§¬"):
|
651 |
+
StreamLLMChatResponse(descriptions["Phosphoinositol System π§¬"])
|
652 |
+
|
653 |
+
if col7.button("Ramipril π"):
|
654 |
+
StreamLLMChatResponse(descriptions["Ramipril π"])
|
655 |
+
|
656 |
+
|
657 |
+
|
658 |
+
# 17. Main
|
659 |
+
def main():
|
660 |
+
|
661 |
+
#st.title("GAIA - Medical License Exam Testing")
|
662 |
+
prompt = f"Write ten funny jokes that are tweet length stories that make you laugh. Show as markdown outline with emojis for each."
|
663 |
+
|
664 |
+
# Add Wit and Humor buttons
|
665 |
+
# add_witty_humor_buttons()
|
666 |
+
add_medical_exam_buttons()
|
667 |
+
|
668 |
+
|
669 |
+
with st.expander("Prompts π", expanded=False):
|
670 |
+
|
671 |
+
example_input = st.text_input("Enter your prompt text for Llama:", value=prompt, help="Enter text to get a response from DromeLlama.")
|
672 |
+
if st.button("Run Prompt With Llama model", help="Click to run the prompt."):
|
673 |
+
try:
|
674 |
+
response=StreamLLMChatResponse(example_input)
|
675 |
+
create_file(filename, example_input, response, should_save)
|
676 |
+
except:
|
677 |
+
st.write('Llama model is asleep. Starting now on A10 GPU. Please wait one minute then retry. KEDA triggered.')
|
678 |
+
|
679 |
+
openai.api_key = os.getenv('OPENAI_API_KEY')
|
680 |
+
if openai.api_key == None: openai.api_key = st.secrets['OPENAI_API_KEY']
|
681 |
+
|
682 |
+
menu = ["txt", "htm", "xlsx", "csv", "md", "py"]
|
683 |
+
choice = st.sidebar.selectbox("Output File Type:", menu)
|
684 |
+
|
685 |
+
model_choice = st.sidebar.radio("Select Model:", ('gpt-3.5-turbo', 'gpt-3.5-turbo-0301'))
|
686 |
+
|
687 |
+
user_prompt = st.text_area("Enter prompts, instructions & questions:", '', height=100)
|
688 |
+
collength, colupload = st.columns([2,3]) # adjust the ratio as needed
|
689 |
+
with collength:
|
690 |
+
max_length = st.slider("File section length for large files", min_value=1000, max_value=128000, value=12000, step=1000)
|
691 |
+
with colupload:
|
692 |
+
uploaded_file = st.file_uploader("Add a file for context:", type=["pdf", "xml", "json", "xlsx", "csv", "html", "htm", "md", "txt"])
|
693 |
+
document_sections = deque()
|
694 |
+
document_responses = {}
|
695 |
+
if uploaded_file is not None:
|
696 |
+
file_content = read_file_content(uploaded_file, max_length)
|
697 |
+
document_sections.extend(divide_document(file_content, max_length))
|
698 |
+
if len(document_sections) > 0:
|
699 |
+
if st.button("ποΈ View Upload"):
|
700 |
+
st.markdown("**Sections of the uploaded file:**")
|
701 |
+
for i, section in enumerate(list(document_sections)):
|
702 |
+
st.markdown(f"**Section {i+1}**\n{section}")
|
703 |
+
st.markdown("**Chat with the model:**")
|
704 |
+
for i, section in enumerate(list(document_sections)):
|
705 |
+
if i in document_responses:
|
706 |
+
st.markdown(f"**Section {i+1}**\n{document_responses[i]}")
|
707 |
+
else:
|
708 |
+
if st.button(f"Chat about Section {i+1}"):
|
709 |
+
st.write('Reasoning with your inputs...')
|
710 |
+
#response = chat_with_model(user_prompt, section, model_choice)
|
711 |
+
st.write('Response:')
|
712 |
+
st.write(response)
|
713 |
+
document_responses[i] = response
|
714 |
+
filename = generate_filename(f"{user_prompt}_section_{i+1}", choice)
|
715 |
+
create_file(filename, user_prompt, response, should_save)
|
716 |
+
st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
717 |
+
if st.button('π¬ Chat'):
|
718 |
+
st.write('Reasoning with your inputs...')
|
719 |
+
user_prompt_sections = divide_prompt(user_prompt, max_length)
|
720 |
+
full_response = ''
|
721 |
+
for prompt_section in user_prompt_sections:
|
722 |
+
response = chat_with_model(prompt_section, ''.join(list(document_sections)), model_choice)
|
723 |
+
full_response += response + '\n' # Combine the responses
|
724 |
+
response = full_response
|
725 |
+
st.write('Response:')
|
726 |
+
st.write(response)
|
727 |
+
filename = generate_filename(user_prompt, choice)
|
728 |
+
create_file(filename, user_prompt, response, should_save)
|
729 |
+
#st.sidebar.markdown(get_table_download_link(filename), unsafe_allow_html=True)
|
730 |
+
|
731 |
+
# Compose a file sidebar of markdown md files:
|
732 |
+
all_files = glob.glob("*.md")
|
733 |
+
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
|
734 |
+
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
735 |
+
if st.sidebar.button("π Delete All Text"):
|
736 |
+
for file in all_files:
|
737 |
+
os.remove(file)
|
738 |
+
st.experimental_rerun()
|
739 |
+
if st.sidebar.button("β¬οΈ Download All"):
|
740 |
+
zip_file = create_zip_of_files(all_files)
|
741 |
+
st.sidebar.markdown(get_zip_download_link(zip_file), unsafe_allow_html=True)
|
742 |
+
file_contents=''
|
743 |
+
next_action=''
|
744 |
+
for file in all_files:
|
745 |
+
col1, col2, col3, col4, col5 = st.sidebar.columns([1,6,1,1,1]) # adjust the ratio as needed
|
746 |
+
with col1:
|
747 |
+
if st.button("π", key="md_"+file): # md emoji button
|
748 |
+
with open(file, 'r') as f:
|
749 |
+
file_contents = f.read()
|
750 |
+
next_action='md'
|
751 |
+
with col2:
|
752 |
+
st.markdown(get_table_download_link(file), unsafe_allow_html=True)
|
753 |
+
with col3:
|
754 |
+
if st.button("π", key="open_"+file): # open emoji button
|
755 |
+
with open(file, 'r') as f:
|
756 |
+
file_contents = f.read()
|
757 |
+
next_action='open'
|
758 |
+
with col4:
|
759 |
+
if st.button("π", key="read_"+file): # search emoji button
|
760 |
+
with open(file, 'r') as f:
|
761 |
+
file_contents = f.read()
|
762 |
+
next_action='search'
|
763 |
+
with col5:
|
764 |
+
if st.button("π", key="delete_"+file):
|
765 |
+
os.remove(file)
|
766 |
+
st.experimental_rerun()
|
767 |
+
|
768 |
+
|
769 |
+
if len(file_contents) > 0:
|
770 |
+
if next_action=='open':
|
771 |
+
file_content_area = st.text_area("File Contents:", file_contents, height=500)
|
772 |
+
if next_action=='md':
|
773 |
+
st.markdown(file_contents)
|
774 |
+
if next_action=='search':
|
775 |
+
file_content_area = st.text_area("File Contents:", file_contents, height=500)
|
776 |
+
st.write('Reasoning with your inputs...')
|
777 |
+
|
778 |
+
# new - llama
|
779 |
+
response = StreamLLMChatResponse(file_contents)
|
780 |
+
filename = generate_filename(user_prompt, ".md")
|
781 |
+
create_file(filename, file_contents, response, should_save)
|
782 |
+
SpeechSynthesis(response)
|
783 |
+
|
784 |
+
# old - gpt
|
785 |
+
#response = chat_with_model(user_prompt, file_contents, model_choice)
|
786 |
+
#filename = generate_filename(file_contents, choice)
|
787 |
+
#create_file(filename, user_prompt, response, should_save)
|
788 |
+
|
789 |
+
st.experimental_rerun()
|
790 |
+
|
791 |
+
|
792 |
+
# Function to encode file to base64
|
793 |
+
def get_base64_encoded_file(file_path):
|
794 |
+
with open(file_path, "rb") as file:
|
795 |
+
return base64.b64encode(file.read()).decode()
|
796 |
+
|
797 |
+
# Function to create a download link
|
798 |
+
def get_audio_download_link(file_path):
|
799 |
+
base64_file = get_base64_encoded_file(file_path)
|
800 |
+
return f'<a href="data:file/wav;base64,{base64_file}" download="{os.path.basename(file_path)}">β¬οΈ Download Audio</a>'
|
801 |
+
|
802 |
+
# Compose a file sidebar of past encounters
|
803 |
+
all_files = glob.glob("*.wav")
|
804 |
+
all_files = [file for file in all_files if len(os.path.splitext(file)[0]) >= 10] # exclude files with short names
|
805 |
+
all_files.sort(key=lambda x: (os.path.splitext(x)[1], x), reverse=True) # sort by file type and file name in descending order
|
806 |
+
|
807 |
+
filekey = 'delall'
|
808 |
+
if st.sidebar.button("π Delete All Audio", key=filekey):
|
809 |
+
for file in all_files:
|
810 |
+
os.remove(file)
|
811 |
+
st.experimental_rerun()
|
812 |
+
|
813 |
+
for file in all_files:
|
814 |
+
col1, col2 = st.sidebar.columns([6, 1]) # adjust the ratio as needed
|
815 |
+
with col1:
|
816 |
+
st.markdown(file)
|
817 |
+
if st.button("π΅", key="play_" + file): # play emoji button
|
818 |
+
audio_file = open(file, 'rb')
|
819 |
+
audio_bytes = audio_file.read()
|
820 |
+
st.audio(audio_bytes, format='audio/wav')
|
821 |
+
#st.markdown(get_audio_download_link(file), unsafe_allow_html=True)
|
822 |
+
#st.text_input(label="", value=file)
|
823 |
+
with col2:
|
824 |
+
if st.button("π", key="delete_" + file):
|
825 |
+
os.remove(file)
|
826 |
+
st.experimental_rerun()
|
827 |
+
|
828 |
+
|
829 |
+
|
830 |
+
# Feedback
|
831 |
+
# Step: Give User a Way to Upvote or Downvote
|
832 |
+
with st.expander("Give your feedback π", expanded=False):
|
833 |
+
|
834 |
+
feedback = st.radio("Step 8: Give your feedback", ("π Upvote", "π Downvote"))
|
835 |
+
if feedback == "π Upvote":
|
836 |
+
st.write("You upvoted π. Thank you for your feedback!")
|
837 |
+
else:
|
838 |
+
st.write("You downvoted π. Thank you for your feedback!")
|
839 |
+
|
840 |
+
load_dotenv()
|
841 |
+
st.write(css, unsafe_allow_html=True)
|
842 |
+
st.header("Chat with documents :books:")
|
843 |
+
user_question = st.text_input("Ask a question about your documents:")
|
844 |
+
if user_question:
|
845 |
+
process_user_input(user_question)
|
846 |
+
with st.sidebar:
|
847 |
+
st.subheader("Your documents")
|
848 |
+
docs = st.file_uploader("import documents", accept_multiple_files=True)
|
849 |
+
with st.spinner("Processing"):
|
850 |
+
raw = pdf2txt(docs)
|
851 |
+
if len(raw) > 0:
|
852 |
+
length = str(len(raw))
|
853 |
+
text_chunks = txt2chunks(raw)
|
854 |
+
vectorstore = vector_store(text_chunks)
|
855 |
+
st.session_state.conversation = get_chain(vectorstore)
|
856 |
+
st.markdown('# AI Search Index of Length:' + length + ' Created.') # add timing
|
857 |
+
filename = generate_filename(raw, 'txt')
|
858 |
+
create_file(filename, raw, '', should_save)
|
859 |
+
|
860 |
+
# 18. Run AI Pipeline
|
861 |
+
if __name__ == "__main__":
|
862 |
+
whisper_main()
|
863 |
+
main()
|
864 |
+
#add_Med_Licensing_Exam_Dataset()
|