AbeerTrial commited on
Commit
0abab6d
·
1 Parent(s): 59e6084

Upload 13 files

Browse files
app.py ADDED
@@ -0,0 +1,907 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import os
3
+ import openai
4
+
5
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
6
+ os.environ["OPENAI_API_KEY"]
7
+
8
+ def save_file(input_file):
9
+ import shutil
10
+ import os
11
+
12
+ destination_dir = "/home/user/app/file/"
13
+ os.makedirs(destination_dir, exist_ok=True)
14
+
15
+ output_dir="/home/user/app/file/"
16
+
17
+ for file in input_file:
18
+ shutil.copy(file.name, output_dir)
19
+
20
+ return "File(s) saved successfully!"
21
+
22
+ def process_file():
23
+ from langchain.document_loaders import PyPDFLoader
24
+ from langchain.document_loaders import DirectoryLoader
25
+ from langchain.document_loaders import TextLoader
26
+ from langchain.document_loaders import Docx2txtLoader
27
+ from langchain.vectorstores import FAISS
28
+ from langchain.embeddings.openai import OpenAIEmbeddings
29
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
30
+ import openai
31
+
32
+ loader1 = DirectoryLoader('/home/user/app/file/', glob="./*.pdf", loader_cls=PyPDFLoader)
33
+ document1 = loader1.load()
34
+
35
+ loader2 = DirectoryLoader('/home/user/app/file/', glob="./*.txt", loader_cls=TextLoader)
36
+ document2 = loader2.load()
37
+
38
+ loader3 = DirectoryLoader('/home/user/app/file/', glob="./*.docx", loader_cls=Docx2txtLoader)
39
+ document3 = loader3.load()
40
+
41
+ document1.extend(document2)
42
+ document1.extend(document3)
43
+
44
+ text_splitter = RecursiveCharacterTextSplitter(
45
+ chunk_size=1000,
46
+ chunk_overlap=200,
47
+ length_function=len)
48
+
49
+ docs = text_splitter.split_documents(document1)
50
+ embeddings = OpenAIEmbeddings()
51
+
52
+ file_db = FAISS.from_documents(docs, embeddings)
53
+ file_db.save_local("/home/user/app/file_db/")
54
+
55
+ return "File(s) processed successfully!"
56
+
57
+ def process_local():
58
+ from langchain.document_loaders import PyPDFLoader
59
+ from langchain.document_loaders import DirectoryLoader
60
+ from langchain.document_loaders import TextLoader
61
+ from langchain.document_loaders import Docx2txtLoader
62
+ from langchain.vectorstores import FAISS
63
+ from langchain.embeddings.openai import OpenAIEmbeddings
64
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
65
+ import openai
66
+ import os
67
+
68
+ destination_dir = "/home/user/app/local_docs/"
69
+ os.makedirs(destination_dir, exist_ok=True)
70
+
71
+ directory_path = '/home/user/app/local_db1/'
72
+ if os.path.exists(directory_path):
73
+ os.rmdir(directory_path)
74
+
75
+ loader1 = DirectoryLoader('/home/user/app/local_docs/', glob="./*.pdf", loader_cls=PyPDFLoader)
76
+ document1 = loader1.load()
77
+
78
+ loader2 = DirectoryLoader('/home/user/app/local_docs/', glob="./*.txt", loader_cls=TextLoader)
79
+ document2 = loader2.load()
80
+
81
+ loader3 = DirectoryLoader('/home/user/app/local_docs/', glob="./*.docx", loader_cls=Docx2txtLoader)
82
+ document3 = loader3.load()
83
+
84
+ document1.extend(document2)
85
+ document1.extend(document3)
86
+
87
+ text_splitter = RecursiveCharacterTextSplitter(
88
+ chunk_size=1000,
89
+ chunk_overlap=200,
90
+ length_function=len)
91
+
92
+ docs = text_splitter.split_documents(document1)
93
+ embeddings = OpenAIEmbeddings()
94
+
95
+ file_db = FAISS.from_documents(docs, embeddings)
96
+ file_db.save_local("/home/user/app/local_db1/")
97
+
98
+ return "File(s) processed successfully!"
99
+
100
+ def formatted_response(docs, response):
101
+ formatted_output = response + "\n\nSources"
102
+
103
+ for i, doc in enumerate(docs):
104
+ source_info = doc.metadata.get('source', 'Unknown source')
105
+ page_info = doc.metadata.get('page', None)
106
+
107
+ file_name = source_info.split('/')[-1].strip()
108
+
109
+ if page_info is not None:
110
+ formatted_output += f"\n{file_name}\tpage no {page_info}"
111
+ else:
112
+ formatted_output += f"\n{file_name}"
113
+
114
+ return formatted_output
115
+
116
+ def search_file(question):
117
+ from langchain.embeddings.openai import OpenAIEmbeddings
118
+ from langchain.vectorstores import FAISS
119
+ from langchain.chains.question_answering import load_qa_chain
120
+ from langchain.callbacks import get_openai_callback
121
+ from langchain.llms import OpenAI
122
+ import openai
123
+ from langchain.chat_models import ChatOpenAI
124
+
125
+ embeddings = OpenAIEmbeddings()
126
+ file_db = FAISS.load_local("/home/user/app/file_db/", embeddings)
127
+ docs = file_db.similarity_search(question)
128
+
129
+ llm = ChatOpenAI(model_name='gpt-3.5-turbo')
130
+ chain = load_qa_chain(llm, chain_type="stuff")
131
+
132
+ with get_openai_callback() as cb:
133
+ response = chain.run(input_documents=docs, question=question)
134
+ print(cb)
135
+
136
+ return formatted_response(docs, response)
137
+
138
+ def local_search(question):
139
+ from langchain.embeddings.openai import OpenAIEmbeddings
140
+ from langchain.vectorstores import FAISS
141
+ from langchain.chains.question_answering import load_qa_chain
142
+ from langchain.callbacks import get_openai_callback
143
+ from langchain.llms import OpenAI
144
+ import openai
145
+ from langchain.chat_models import ChatOpenAI
146
+
147
+ embeddings = OpenAIEmbeddings()
148
+ file_db = FAISS.load_local("/home/user/app/local_db1/", embeddings)
149
+ docs = file_db.similarity_search(question)
150
+
151
+ llm = ChatOpenAI(model_name='gpt-3.5-turbo')
152
+ chain = load_qa_chain(llm, chain_type="stuff")
153
+
154
+ with get_openai_callback() as cb:
155
+ response = chain.run(input_documents=docs, question=question)
156
+ print(cb)
157
+
158
+ return formatted_response(docs, response)
159
+
160
+ def delete_file():
161
+
162
+ import shutil
163
+
164
+ path1 = "/home/user/app/file/"
165
+ path2 = "/home/user/app/file_db/"
166
+
167
+ try:
168
+ shutil.rmtree(path1)
169
+ shutil.rmtree(path2)
170
+ return "Deleted Successfully"
171
+
172
+ except:
173
+ return "Already Deleted"
174
+
175
+ global soap_file_list
176
+ global sbar_file_list
177
+
178
+ def soap_refresh():
179
+ import os
180
+ import gradio as gr
181
+ global soap_file_list
182
+
183
+ destination_folder = "/home/user/app/soap_docs/"
184
+ if not os.path.exists(destination_folder):
185
+ os.makedirs(destination_folder)
186
+
187
+ directory = '/home/user/app/soap_docs/'
188
+ soap_file_list = []
189
+
190
+ for root, dirs, files in os.walk(directory):
191
+ for file in files:
192
+ soap_file_list.append(file)
193
+ return gr.CheckboxGroup.update(choices=soap_file_list)
194
+
195
+ def sbar_refresh():
196
+ import os
197
+ import gradio as gr
198
+ global sbar_file_list
199
+
200
+ destination_folder = "/home/user/app/sbar_docs/"
201
+ if not os.path.exists(destination_folder):
202
+ os.makedirs(destination_folder)
203
+
204
+ directory = '/home/user/app/sbar_docs/'
205
+ sbar_file_list = []
206
+
207
+ for root, dirs, files in os.walk(directory):
208
+ for file in files:
209
+ sbar_file_list.append(file)
210
+ return gr.CheckboxGroup.update(choices=sbar_file_list)
211
+
212
+ def ask_soap(doc_names, question):
213
+ from langchain.llms import OpenAI
214
+ from langchain import PromptTemplate, LLMChain
215
+ from langchain.chat_models import ChatOpenAI
216
+ import openai
217
+ import docx
218
+ import os
219
+
220
+ destination_folder = "/home/user/app/soap_docs/"
221
+ if not os.path.exists(destination_folder):
222
+ os.makedirs(destination_folder)
223
+
224
+ extracted_text = "Extracted text:\n\n\n"
225
+
226
+ for doc_name in doc_names:
227
+ docx_path = "/home/user/app/soap_docs/" + doc_name
228
+ doc = docx.Document(docx_path)
229
+
230
+ for paragraph in doc.paragraphs:
231
+ extracted_text += paragraph.text + "\n"
232
+
233
+ extracted_text += "\nExtracted text:\n\n\n"
234
+
235
+ question = (
236
+ "\n\nUse the suitable 'Extracted text' to answer the following question:\n" + question
237
+ )
238
+ extracted_text += question
239
+
240
+ if extracted_text:
241
+ print(extracted_text)
242
+ else:
243
+ print("failed")
244
+
245
+ template = """Question: {question}
246
+
247
+ Answer: Let's think step by step."""
248
+
249
+ prompt = PromptTemplate(template=template, input_variables=["question"])
250
+
251
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
252
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
253
+ response = llm_chain.run(extracted_text)
254
+
255
+ return response
256
+
257
+ def ask_sbar(doc_names, question):
258
+ from langchain.llms import OpenAI
259
+ from langchain import PromptTemplate, LLMChain
260
+ from langchain.chat_models import ChatOpenAI
261
+ import openai
262
+ import docx
263
+ import os
264
+
265
+ destination_folder = "/home/user/app/sbar_docs/"
266
+ if not os.path.exists(destination_folder):
267
+ os.makedirs(destination_folder)
268
+
269
+ extracted_text = "Extracted text:\n\n\n"
270
+
271
+ for doc_name in doc_names:
272
+ docx_path = "/home/user/app/sbar_docs/" + doc_name
273
+ doc = docx.Document(docx_path)
274
+
275
+ for paragraph in doc.paragraphs:
276
+ extracted_text += paragraph.text + "\n"
277
+
278
+ extracted_text += "\nExtracted text:\n\n\n"
279
+
280
+ question = (
281
+ "\n\nUse the suitable 'Extracted text' to answer the following question:\n" + question
282
+ )
283
+ extracted_text += question
284
+
285
+ if extracted_text:
286
+ print(extracted_text)
287
+ else:
288
+ print("failed")
289
+
290
+ template = """Question: {question}
291
+
292
+ Answer: Let's think step by step."""
293
+
294
+ prompt = PromptTemplate(template=template, input_variables=["question"])
295
+
296
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
297
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
298
+ response = llm_chain.run(extracted_text)
299
+
300
+ return response
301
+
302
+ soap_refresh()
303
+
304
+ def ask_all_soap(question):
305
+ from langchain.llms import OpenAI
306
+ from langchain import PromptTemplate, LLMChain
307
+ from langchain.chat_models import ChatOpenAI
308
+ import openai
309
+ import docx
310
+ import os
311
+ global soap_file_list
312
+ soap_file_list = soap_file_list
313
+
314
+ destination_folder = "/home/user/app/soap_docs/"
315
+ if not os.path.exists(destination_folder):
316
+ os.makedirs(destination_folder)
317
+
318
+ extracted_text = "Extracted text:\n\n\n"
319
+
320
+ for file in soap_file_list:
321
+ docx_path = "/home/user/app/soap_docs/" + file
322
+ doc = docx.Document(docx_path)
323
+
324
+ for paragraph in doc.paragraphs:
325
+ extracted_text += paragraph.text + "\n"
326
+
327
+ extracted_text += "\nExtracted text:\n\n\n"
328
+
329
+ question = (
330
+ "\n\nUse the suitable 'Extracted text' to answer the following question:\n" + question
331
+ )
332
+ extracted_text += question
333
+
334
+ if extracted_text:
335
+ print(extracted_text)
336
+ else:
337
+ print("failed")
338
+
339
+ template = """Question: {question}
340
+
341
+ Answer: Let's think step by step."""
342
+
343
+ prompt = PromptTemplate(template=template, input_variables=["question"])
344
+
345
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
346
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
347
+ response = llm_chain.run(extracted_text)
348
+
349
+ return response
350
+
351
+ sbar_refresh()
352
+
353
+ def ask_all_sbar(question):
354
+ from langchain.llms import OpenAI
355
+ from langchain import PromptTemplate, LLMChain
356
+ from langchain.chat_models import ChatOpenAI
357
+ import openai
358
+ import docx
359
+ import os
360
+ global sbar_file_list
361
+ sbar_file_list = sbar_file_list
362
+
363
+ destination_folder = "/home/user/app/sbar_docs/"
364
+ if not os.path.exists(destination_folder):
365
+ os.makedirs(destination_folder)
366
+
367
+ extracted_text = "Extracted text:\n\n\n"
368
+
369
+ for file in sbar_file_list:
370
+ docx_path = "/home/user/app/sbar_docs/" + file
371
+ doc = docx.Document(docx_path)
372
+
373
+ for paragraph in doc.paragraphs:
374
+ extracted_text += paragraph.text + "\n"
375
+
376
+ extracted_text += "\nExtracted text:\n\n\n"
377
+
378
+ question = (
379
+ "\n\nUse the suitable 'Extracted text' to answer the following question:\n" + question
380
+ )
381
+ extracted_text += question
382
+
383
+ if extracted_text:
384
+ print(extracted_text)
385
+ else:
386
+ print("failed")
387
+
388
+ template = """Question: {question}
389
+
390
+ Answer: Let's think step by step."""
391
+
392
+ prompt = PromptTemplate(template=template, input_variables=["question"])
393
+
394
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
395
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
396
+ response = llm_chain.run(extracted_text)
397
+
398
+ return response
399
+
400
+ def search_gpt(question):
401
+ from langchain.llms import OpenAI
402
+ from langchain import PromptTemplate, LLMChain
403
+ from langchain.chat_models import ChatOpenAI
404
+
405
+ template = """Question: {question}
406
+
407
+ Answer: Let's think step by step."""
408
+
409
+ prompt = PromptTemplate(template=template, input_variables=["question"])
410
+
411
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
412
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
413
+ response = llm_chain.run(question)
414
+
415
+ return response
416
+
417
+ def local_gpt(question):
418
+ from langchain.llms import OpenAI
419
+ from langchain import PromptTemplate, LLMChain
420
+ from langchain.chat_models import ChatOpenAI
421
+
422
+ template = """Question: {question}
423
+
424
+ Answer: Let's think step by step."""
425
+
426
+ prompt = PromptTemplate(template=template, input_variables=["question"])
427
+
428
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
429
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
430
+ response = llm_chain.run(question)
431
+
432
+ return response
433
+
434
+ global output
435
+
436
+ def audio_text(filepath):
437
+ import openai
438
+ global output
439
+
440
+ audio = open(filepath, "rb")
441
+ transcript = openai.Audio.transcribe("whisper-1", audio)
442
+ output = transcript["text"]
443
+
444
+ return output
445
+
446
+ global soap_response
447
+ global sbar_response
448
+
449
+ def transcript_soap(text):
450
+ from langchain.llms import OpenAI
451
+ from langchain import PromptTemplate, LLMChain
452
+ from langchain.chat_models import ChatOpenAI
453
+
454
+ global soap_response
455
+
456
+ question = (
457
+ "Use the following context given below to generate a detailed SOAP Report:\n\n"
458
+ )
459
+ question += text
460
+ print(question)
461
+
462
+ template = """Question: {question}
463
+
464
+ Answer: Let's think step by step."""
465
+
466
+ word_count = len(text.split())
467
+ prompt = PromptTemplate(template=template, input_variables=["question"])
468
+
469
+ if word_count < 2000:
470
+ llm = ChatOpenAI(model="gpt-3.5-turbo")
471
+ elif word_count < 5000:
472
+ llm = ChatOpenAI(model="gpt-4")
473
+ else:
474
+ llm = ChatOpenAI(model="gpt-4-32k")
475
+
476
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
477
+ soap_response = llm_chain.run(question)
478
+
479
+ return soap_response
480
+
481
+ def transcript_sbar(text):
482
+ from langchain.llms import OpenAI
483
+ from langchain import PromptTemplate, LLMChain
484
+ from langchain.chat_models import ChatOpenAI
485
+
486
+ global sbar_response
487
+
488
+ question = (
489
+ "Use the following context given below to generate a detailed SBAR Report:\n\n"
490
+ )
491
+ question += text
492
+ print(question)
493
+
494
+ template = """Question: {question}
495
+
496
+ Answer: Let's think step by step."""
497
+
498
+ word_count = len(text.split())
499
+ prompt = PromptTemplate(template=template, input_variables=["question"])
500
+
501
+ if word_count < 2000:
502
+ llm = ChatOpenAI(model="gpt-3.5-turbo")
503
+ elif word_count < 5000:
504
+ llm = ChatOpenAI(model="gpt-4")
505
+ else:
506
+ llm = ChatOpenAI(model="gpt-4-32k")
507
+
508
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
509
+ sbar_response = llm_chain.run(question)
510
+
511
+ return sbar_response
512
+
513
+ def text_soap():
514
+ from langchain.llms import OpenAI
515
+ from langchain import PromptTemplate, LLMChain
516
+ from langchain.chat_models import ChatOpenAI
517
+
518
+ global output
519
+ global soap_response
520
+ output = output
521
+
522
+ question = (
523
+ "Use the following context given below to generate a detailed SOAP Report:\n\n"
524
+ )
525
+ question += output
526
+ print(question)
527
+
528
+ template = """Question: {question}
529
+
530
+ Answer: Let's think step by step."""
531
+
532
+ word_count = len(output.split())
533
+ prompt = PromptTemplate(template=template, input_variables=["question"])
534
+
535
+ if word_count < 2000:
536
+ llm = ChatOpenAI(model="gpt-3.5-turbo")
537
+ elif word_count < 5000:
538
+ llm = ChatOpenAI(model="gpt-4")
539
+ else:
540
+ llm = ChatOpenAI(model="gpt-4-32k")
541
+
542
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
543
+ soap_response = llm_chain.run(question)
544
+
545
+ return soap_response
546
+
547
+ def text_sbar():
548
+ from langchain.llms import OpenAI
549
+ from langchain import PromptTemplate, LLMChain
550
+ from langchain.chat_models import ChatOpenAI
551
+
552
+ global output
553
+ global sbar_response
554
+ output = output
555
+
556
+ question = (
557
+ "Use the following context given below to generate a detailed SBAR Report:\n\n"
558
+ )
559
+ question += output
560
+ print(question)
561
+
562
+ template = """Question: {question}
563
+
564
+ Answer: Let's think step by step."""
565
+
566
+ word_count = len(output.split())
567
+ prompt = PromptTemplate(template=template, input_variables=["question"])
568
+
569
+ if word_count < 2000:
570
+ llm = ChatOpenAI(model="gpt-3.5-turbo")
571
+ elif word_count < 5000:
572
+ llm = ChatOpenAI(model="gpt-4")
573
+ else:
574
+ llm = ChatOpenAI(model="gpt-4-32k")
575
+
576
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
577
+ sbar_response = llm_chain.run(question)
578
+
579
+ return sbar_response
580
+
581
+ global soap_path
582
+ global sbar_path
583
+
584
+ def soap_docx(name):
585
+ global soap_response
586
+ soap_response = soap_response
587
+ import docx
588
+ import os
589
+ global soap_path
590
+
591
+ destination_folder = "/home/user/app/soap_docs/"
592
+ if not os.path.exists(destination_folder):
593
+ os.makedirs(destination_folder)
594
+
595
+ soap_path = f"/home/user/app/soap_docs/SOAP_{name}.docx"
596
+
597
+ doc = docx.Document()
598
+ doc.add_paragraph(soap_response)
599
+ doc.save(soap_path)
600
+
601
+ return "Successfully saved SOAP .docx File"
602
+
603
+ def sbar_docx(name):
604
+ global sbar_response
605
+ sbar_response = sbar_response
606
+ import docx
607
+ import os
608
+ global sbar_path
609
+
610
+ destination_folder = "/home/user/app/sbar_docs/"
611
+ if not os.path.exists(destination_folder):
612
+ os.makedirs(destination_folder)
613
+
614
+ sbar_path = f"/home/user/app/sbar_docs/SBAR_{name}.docx"
615
+
616
+ doc = docx.Document()
617
+ doc.add_paragraph(sbar_response)
618
+ doc.save(sbar_path)
619
+
620
+ return "Successfully saved SBAR .docx File"
621
+
622
+ def download_soap():
623
+ global soap_path
624
+ soap_path = soap_path
625
+
626
+ return soap_path
627
+
628
+ def download_sbar():
629
+ global sbar_path
630
+ sbar_path = sbar_path
631
+
632
+ return sbar_path
633
+
634
+ import gradio as gr
635
+
636
+ css = """
637
+ .col{
638
+ max-width: 50%;
639
+ margin: 0 auto;
640
+ display: flex;
641
+ flex-direction: column;
642
+ justify-content: center;
643
+ align-items: center;
644
+ }
645
+ """
646
+
647
+ theme = gr.Theme.from_hub("shivi/calm_seafoam")
648
+
649
+ with gr.Blocks(theme=theme, css=css) as demo:
650
+ gr.Markdown("## <center>Medical App</center>")
651
+
652
+ with gr.Tab("Create SOAP and SBAR Reports"):
653
+ with gr.Column(elem_classes="col"):
654
+
655
+ with gr.Tab("From Recorded Audio"):
656
+ with gr.Column():
657
+
658
+ mic_audio_input = gr.Audio(source="microphone", type="filepath", label="Speak to the Microphone")
659
+ mic_audio_button = gr.Button("Generate Transcript")
660
+ mic_audio_output = gr.Textbox(label="Transcription")
661
+
662
+ gr.ClearButton([mic_audio_input, mic_audio_output])
663
+
664
+ with gr.Tab("SOAP Report"):
665
+ with gr.Column():
666
+
667
+ mic_text_soap_button = gr.Button("Generate SOAP Report")
668
+ mic_text_soap_output = gr.Textbox(label="SOAP Report")
669
+
670
+ mic_soap_docx_input = gr.Textbox(label="Enter the name of SOAP .docx File")
671
+ mic_soap_docx_button = gr.Button("Save SOAP Document")
672
+ mic_soap_docx_output = gr.Textbox(label="File save status")
673
+
674
+ mic_soap_download_button = gr.Button("Download SOAP .docx File")
675
+ mic_soap_download_output = gr.Files(label="Download Link")
676
+
677
+ gr.ClearButton([mic_text_soap_output, mic_soap_docx_input, mic_soap_docx_output, mic_soap_download_output])
678
+
679
+ with gr.Tab("SBAR Report"):
680
+ with gr.Column():
681
+
682
+ mic_text_sbar_button = gr.Button("Generate SBAR Report")
683
+ mic_text_sbar_output = gr.Textbox(label="SBAR Report")
684
+
685
+ mic_sbar_docx_input = gr.Textbox(label="Enter the name of SBAR .docx File")
686
+ mic_sbar_docx_button = gr.Button("Save SBAR Document")
687
+ mic_sbar_docx_output = gr.Textbox(label="File save status")
688
+
689
+ mic_sbar_download_button = gr.Button("Download SBAR .docx File")
690
+ mic_sbar_download_output = gr.Files(label="Download Link")
691
+
692
+ gr.ClearButton([mic_text_sbar_output, mic_sbar_docx_input, mic_sbar_docx_output, mic_sbar_download_output])
693
+
694
+ with gr.Tab("From Uploaded Audio"):
695
+ with gr.Column():
696
+
697
+ upload_audio_input = gr.Audio(source="upload", type="filepath", label="Upload Audio File here")
698
+ upload_audio_button = gr.Button("Generate Transcript")
699
+ upload_audio_output = gr.Textbox(label="Output")
700
+
701
+ gr.ClearButton([upload_audio_input, upload_audio_output])
702
+
703
+ with gr.Tab("SOAP Report"):
704
+ with gr.Column():
705
+
706
+ upload_text_soap_button = gr.Button("Generate SOAP Report")
707
+ upload_text_soap_output = gr.Textbox(label="SOAP Report")
708
+
709
+ upload_soap_docx_input = gr.Textbox(label="Enter the name of SOAP .docx File")
710
+ upload_soap_docx_button = gr.Button("Save SOAP Document")
711
+ upload_soap_docx_output = gr.Textbox(label="File save status")
712
+
713
+ upload_soap_download_button = gr.Button("Download SOAP .docx File")
714
+ upload_soap_download_output = gr.Files(label="Download Link")
715
+
716
+ gr.ClearButton([upload_text_soap_output, upload_soap_docx_input, upload_soap_docx_output, upload_soap_download_output])
717
+
718
+ with gr.Tab("SBAR Report"):
719
+ with gr.Column():
720
+
721
+ upload_text_sbar_button = gr.Button("Generate SBAR Report")
722
+ upload_text_sbar_output = gr.Textbox(label="SBAR Report")
723
+
724
+ upload_sbar_docx_input = gr.Textbox(label="Enter the name of SBAR .docx File")
725
+ upload_sbar_docx_button = gr.Button("Save SBAR Document")
726
+ upload_sbar_docx_output = gr.Textbox(label="File save status")
727
+
728
+ upload_sbar_download_button = gr.Button("Download SBAR .docx File")
729
+ upload_sbar_download_output = gr.Files(label="Download Link")
730
+
731
+ gr.ClearButton([upload_text_sbar_output, upload_sbar_docx_input, upload_sbar_docx_output, upload_sbar_download_output])
732
+
733
+ with gr.Tab("From Text Transcript"):
734
+ with gr.Column():
735
+
736
+ text_transcript_input = gr.Textbox(label="Enter your Transcript here")
737
+
738
+ gr.ClearButton([text_transcript_input])
739
+
740
+ with gr.Tab("SOAP Report"):
741
+ with gr.Column():
742
+
743
+ text_text_soap_button = gr.Button("Generate SOAP Report")
744
+ text_text_soap_output = gr.Textbox(label="SOAP Report")
745
+
746
+ text_soap_docx_input = gr.Textbox(label="Enter the name of SOAP .docx File")
747
+ text_soap_docx_button = gr.Button("Save SOAP Document")
748
+ text_soap_docx_output = gr.Textbox(label="File save status")
749
+
750
+ text_soap_download_button = gr.Button("Download SOAP .docx File")
751
+ text_soap_download_output = gr.Files(label="Download Link")
752
+
753
+ gr.ClearButton([text_text_soap_output, text_soap_docx_input, text_soap_docx_output, text_soap_download_output])
754
+
755
+ with gr.Tab("SBAR Report"):
756
+ with gr.Column():
757
+
758
+ text_text_sbar_button = gr.Button("Generate SBAR Report")
759
+ text_text_sbar_output = gr.Textbox(label="SBAR Report")
760
+
761
+ text_sbar_docx_input = gr.Textbox(label="Enter the name of SBAR .docx File")
762
+ text_sbar_docx_button = gr.Button("Save SBAR Document")
763
+ text_sbar_docx_output = gr.Textbox(label="File save status")
764
+
765
+ text_sbar_download_button = gr.Button("Download SBAR .docx File")
766
+ text_sbar_download_output = gr.Files(label="Download Link")
767
+
768
+ gr.ClearButton([text_text_sbar_output, text_sbar_docx_input, text_sbar_docx_output, text_sbar_download_output])
769
+
770
+ with gr.Tab("Query SOAP and SBAR Reports"):
771
+ with gr.Column(elem_classes="col"):
772
+
773
+ with gr.Tab("Query SOAP Reports"):
774
+ with gr.Column():
775
+
776
+ soap_refresh_button = gr.Button("Refresh")
777
+ ask_soap_input = gr.CheckboxGroup(label="Choose File(s)")
778
+
779
+ ask_soap_question = gr.Textbox(label="Enter Question here")
780
+ ask_soap_button = gr.Button("Submit")
781
+ ask_soap_output = gr.Textbox(label="Output")
782
+
783
+ ask_all_soap_button = gr.Button("Ask all SOAP Reports")
784
+ ask_all_soap_output = gr.Textbox(label="Output")
785
+
786
+ gr.ClearButton([ask_soap_input, ask_soap_question, ask_soap_output, ask_all_soap_output])
787
+
788
+ with gr.Tab("Query SBAR Reports"):
789
+ with gr.Column():
790
+
791
+ sbar_refresh_button = gr.Button("Refresh")
792
+ ask_sbar_input = gr.CheckboxGroup(label="Choose File(s)")
793
+
794
+ ask_sbar_question = gr.Textbox(label="Enter Question here")
795
+ ask_sbar_button = gr.Button("Submit")
796
+ ask_sbar_output = gr.Textbox(label="Output")
797
+
798
+ ask_all_sbar_button = gr.Button("Ask all SBAR Reports")
799
+ ask_all_sbar_output = gr.Textbox(label="Output")
800
+
801
+ gr.ClearButton([ask_sbar_input, ask_sbar_question, ask_sbar_output, ask_all_sbar_output])
802
+
803
+ with gr.Tab("Query your Documents"):
804
+ with gr.Column(elem_classes="col"):
805
+
806
+ with gr.Tab("Upload and Process Documents"):
807
+ with gr.Column():
808
+
809
+ file_upload_input = gr.Files(label="Upload File(s) here")
810
+ file_upload_button = gr.Button("Upload")
811
+ file_upload_output = gr.Textbox(label="Output")
812
+
813
+ file_process_button = gr.Button("Process")
814
+ file_process_output = gr.Textbox(label="Output")
815
+
816
+ gr.ClearButton([file_upload_input, file_upload_output, file_process_output])
817
+
818
+ with gr.Tab("Query Documents"):
819
+ with gr.Column():
820
+
821
+ file_search_input = gr.Textbox(label="Enter Question here")
822
+ file_search_button = gr.Button("Search")
823
+ file_search_output = gr.Textbox(label="Output")
824
+
825
+ search_gpt_button = gr.Button("Ask ChatGPT")
826
+ search_gpt_output = gr.Textbox(label="Output")
827
+
828
+ file_delete_button = gr.Button("Delete")
829
+ file_delete_output = gr.Textbox(label="Output")
830
+
831
+ gr.ClearButton([file_search_input, file_search_output, search_gpt_output, file_delete_output])
832
+
833
+ with gr.Tab("Query all Local Documents"):
834
+ with gr.Column(elem_classes="col"):
835
+
836
+ local_process_button = gr.Button("Process")
837
+ local_process_output = gr.Textbox(label="Output")
838
+
839
+ local_search_input = gr.Textbox(label="Enter Question here")
840
+ local_search_button = gr.Button("Search")
841
+ local_search_output = gr.Textbox(label="Output")
842
+
843
+ local_gpt_button = gr.Button("Ask ChatGPT")
844
+ local_gpt_output = gr.Textbox(label="Output")
845
+
846
+ gr.ClearButton([local_process_output, local_search_input, local_search_output, local_gpt_output])
847
+
848
+ #########################################################################################################
849
+ file_upload_button.click(save_file, inputs=file_upload_input, outputs=file_upload_output)
850
+ file_process_button.click(process_file, inputs=None, outputs=file_process_output)
851
+
852
+ file_search_button.click(search_file, inputs=file_search_input, outputs=file_search_output)
853
+ search_gpt_button.click(search_gpt, inputs=file_search_input, outputs=search_gpt_output)
854
+
855
+ file_delete_button.click(delete_file, inputs=None, outputs=file_delete_output)
856
+
857
+ #########################################################################################################
858
+ local_process_button.click(process_local, inputs=None, outputs=local_process_output)
859
+ local_search_button.click(local_search, inputs=local_search_input, outputs=local_search_output)
860
+ local_gpt_button.click(local_gpt, inputs=local_search_input, outputs=local_gpt_output)
861
+
862
+ ########################################################################################################
863
+ soap_refresh_button.click(soap_refresh, inputs=None, outputs=ask_soap_input)
864
+ ask_soap_button.click(ask_soap, inputs=[ask_soap_input, ask_soap_question], outputs=ask_soap_output)
865
+
866
+ sbar_refresh_button.click(sbar_refresh, inputs=None, outputs=ask_sbar_input)
867
+ ask_sbar_button.click(ask_sbar, inputs=[ask_sbar_input, ask_sbar_question], outputs=ask_sbar_output)
868
+
869
+ ask_all_soap_button.click(ask_all_soap, inputs=ask_soap_question, outputs=ask_all_soap_output)
870
+ ask_all_sbar_button.click(ask_all_sbar, inputs=ask_sbar_question, outputs=ask_all_sbar_output)
871
+
872
+ ########################################################################################################
873
+ mic_audio_button.click(audio_text, inputs=mic_audio_input, outputs=mic_audio_output)
874
+
875
+ mic_text_soap_button.click(text_soap, inputs=None, outputs=mic_text_soap_output)
876
+ mic_text_sbar_button.click(text_sbar, inputs=None, outputs=mic_text_sbar_output)
877
+
878
+ mic_soap_docx_button.click(soap_docx, inputs=mic_soap_docx_input, outputs=mic_soap_docx_output)
879
+ mic_sbar_docx_button.click(sbar_docx, inputs=mic_sbar_docx_input, outputs=mic_sbar_docx_output)
880
+
881
+ mic_soap_download_button.click(download_soap, inputs=None, outputs=mic_soap_download_output)
882
+ mic_sbar_download_button.click(download_sbar, inputs=None, outputs=mic_sbar_download_output)
883
+ ##########################################################################################################
884
+ upload_audio_button.click(audio_text, inputs=upload_audio_input, outputs=upload_audio_output)
885
+
886
+ upload_text_soap_button.click(text_soap, inputs=None, outputs=upload_text_soap_output)
887
+ upload_text_sbar_button.click(text_sbar, inputs=None, outputs=upload_text_sbar_output)
888
+
889
+ upload_soap_docx_button.click(soap_docx, inputs=upload_soap_docx_input, outputs=upload_soap_docx_output)
890
+ upload_sbar_docx_button.click(sbar_docx, inputs=upload_sbar_docx_input, outputs=upload_sbar_docx_output)
891
+
892
+ upload_soap_download_button.click(download_soap, inputs=None, outputs=upload_soap_download_output)
893
+ upload_sbar_download_button.click(download_sbar, inputs=None, outputs=upload_sbar_download_output)
894
+ ############################################################################################################
895
+ text_text_soap_button.click(transcript_soap, inputs=text_transcript_input, outputs=text_text_soap_output)
896
+ text_text_sbar_button.click(transcript_sbar, inputs=text_transcript_input, outputs=text_text_sbar_output)
897
+
898
+ text_soap_docx_button.click(soap_docx, inputs=text_soap_docx_input, outputs=text_soap_docx_output)
899
+ text_sbar_docx_button.click(sbar_docx, inputs=text_sbar_docx_input, outputs=text_sbar_docx_output)
900
+
901
+ text_soap_download_button.click(download_soap, inputs=None, outputs=text_soap_download_output)
902
+ text_sbar_download_button.click(download_sbar, inputs=None, outputs=text_sbar_download_output)
903
+ #############################################################################################################
904
+
905
+
906
+ demo.queue()
907
+ demo.launch()
local_db/test01.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ test file
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ numpy==1.22.0
2
+ langchain
3
+ pypdf
4
+ PyPDF2
5
+ streamlit
6
+ docx2txt
7
+ gradio
8
+ faiss-gpu
9
+ openai
10
+ tiktoken
11
+ python-docx
12
+ git+https://github.com/openai/whisper.git
13
+ sounddevice
sbar_docs/Benjamin Martinez.docx ADDED
Binary file (27.5 kB). View file
 
sbar_docs/David Moore.docx ADDED
Binary file (27.9 kB). View file
 
sbar_docs/Isabella Brown.docx ADDED
Binary file (28.3 kB). View file
 
sbar_docs/Jerry Tylor.docx ADDED
Binary file (27.8 kB). View file
 
sbar_docs/Sophia Johnson.docx ADDED
Binary file (27.9 kB). View file
 
soap_docs/Jackson Lee.docx ADDED
Binary file (27.1 kB). View file
 
soap_docs/Mason Jones.docx ADDED
Binary file (28 kB). View file
 
soap_docs/Olivia Thomas.docx ADDED
Binary file (27 kB). View file
 
soap_docs/Samual Harris.docx ADDED
Binary file (27.5 kB). View file
 
soap_docs/William Anderson.docx ADDED
Binary file (27.6 kB). View file