AbeerTrial commited on
Commit
0a3a31f
·
1 Parent(s): ca6b840

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +127 -104
app.py CHANGED
@@ -74,15 +74,8 @@
74
 
75
  # copy_files(source_folder, destination_folder)
76
 
77
- import os
78
- import openai
79
-
80
- os.environ["TOKENIZERS_PARALLELISM"] = "false"
81
- os.environ["OPENAI_API_KEY"] = "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"
82
- openai.api_key = "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"
83
 
84
  def api_key(key):
85
-
86
  import os
87
  import openai
88
 
@@ -92,6 +85,7 @@ def api_key(key):
92
 
93
  return "Successful!"
94
 
 
95
  def save_file(input_file):
96
  import shutil
97
  import os
@@ -99,13 +93,14 @@ def save_file(input_file):
99
  destination_dir = "/home/user/app/file/"
100
  os.makedirs(destination_dir, exist_ok=True)
101
 
102
- output_dir="/home/user/app/file/"
103
 
104
  for file in input_file:
105
- shutil.copy(file.name, output_dir)
106
 
107
  return "File(s) saved successfully!"
108
 
 
109
  def process_file():
110
  from langchain.document_loaders import PyPDFLoader
111
  from langchain.document_loaders import DirectoryLoader
@@ -116,23 +111,27 @@ def process_file():
116
  from langchain.text_splitter import CharacterTextSplitter
117
  import openai
118
 
119
- loader1 = DirectoryLoader('/home/user/app/file/', glob="./*.pdf", loader_cls=PyPDFLoader)
 
 
120
  document1 = loader1.load()
121
 
122
- loader2 = DirectoryLoader('/home/user/app/file/', glob="./*.txt", loader_cls=TextLoader)
 
 
123
  document2 = loader2.load()
124
 
125
- loader3 = DirectoryLoader('/home/user/app/file/', glob="./*.docx", loader_cls=Docx2txtLoader)
 
 
126
  document3 = loader3.load()
127
 
128
  document1.extend(document2)
129
  document1.extend(document3)
130
 
131
  text_splitter = CharacterTextSplitter(
132
- separator="\n",
133
- chunk_size=1000,
134
- chunk_overlap=200,
135
- length_function=len)
136
 
137
  docs = text_splitter.split_documents(document1)
138
  embeddings = OpenAIEmbeddings()
@@ -142,15 +141,16 @@ def process_file():
142
 
143
  return "File(s) processed successfully!"
144
 
 
145
  def formatted_response(docs, response):
146
  formatted_output = response + "\n\nSources"
147
 
148
  for i, doc in enumerate(docs):
149
- source_info = doc.metadata.get('source', 'Unknown source')
150
- page_info = doc.metadata.get('page', None)
151
 
152
  # Get the file name without the directory path
153
- file_name = source_info.split('/')[-1].strip()
154
 
155
  if page_info is not None:
156
  formatted_output += f"\n{file_name}\tpage no {page_info}"
@@ -159,6 +159,7 @@ def formatted_response(docs, response):
159
 
160
  return formatted_output
161
 
 
162
  def search_file(question):
163
  from langchain.embeddings.openai import OpenAIEmbeddings
164
  from langchain.vectorstores import FAISS
@@ -167,11 +168,12 @@ def search_file(question):
167
  from langchain.llms import OpenAI
168
  import openai
169
  from langchain.chat_models import ChatOpenAI
 
170
  embeddings = OpenAIEmbeddings()
171
  file_db = FAISS.load_local("/home/user/app/file_db/", embeddings)
172
  docs = file_db.similarity_search(question)
173
 
174
- llm = ChatOpenAI(model_name='gpt-3.5-turbo')
175
  chain = load_qa_chain(llm, chain_type="stuff")
176
  with get_openai_callback() as cb:
177
  response = chain.run(input_documents=docs, question=question)
@@ -179,6 +181,7 @@ def search_file(question):
179
 
180
  return formatted_response(docs, response)
181
 
 
182
  def search_local(question):
183
  from langchain.embeddings.openai import OpenAIEmbeddings
184
  from langchain.vectorstores import FAISS
@@ -187,13 +190,14 @@ def search_local(question):
187
  from langchain.llms import OpenAI
188
  import openai
189
  from langchain.chat_models import ChatOpenAI
 
190
  embeddings = OpenAIEmbeddings()
191
  file_db = FAISS.load_local("/home/user/app/local_db/", embeddings)
192
  docs = file_db.similarity_search(question)
193
 
194
  print(docs)
195
  type(docs)
196
- llm = ChatOpenAI(model_name='gpt-3.5-turbo')
197
  chain = load_qa_chain(llm, chain_type="stuff")
198
  with get_openai_callback() as cb:
199
  response = chain.run(input_documents=docs, question=question)
@@ -201,8 +205,8 @@ def search_local(question):
201
 
202
  return formatted_response(docs, response)
203
 
204
- def delete_file():
205
 
 
206
  import shutil
207
 
208
  path1 = "/home/user/app/file/"
@@ -216,38 +220,44 @@ def delete_file():
216
  except:
217
  return "Already Deleted"
218
 
 
219
  import os
220
  import gradio as gr
221
 
 
222
  def list_files():
223
- directory = '/home/user/app/docs'
224
  file_list = []
225
  for root, dirs, files in os.walk(directory):
226
  for file in files:
227
  file_list.append(file)
228
  return gr.Dropdown.update(choices=file_list)
229
 
 
230
  file_list = list_files()
231
 
232
  print("List of file names in the directory:")
233
  for file_name in file_list:
234
  print(file_name)
235
 
 
236
  def soap_report(doc_name, question):
237
  from langchain.llms import OpenAI
238
  from langchain import PromptTemplate, LLMChain
239
  import openai
240
  import docx
241
 
242
- docx_path = '/home/user/app/docs/'+doc_name
243
 
244
  doc = docx.Document(docx_path)
245
- extracted_text = 'Extracted text:\n\n\n'
246
 
247
  for paragraph in doc.paragraphs:
248
- extracted_text += paragraph.text + '\n'
249
 
250
- question = "\n\nUse the 'Extracted text' to answer the following question:\n" + question
 
 
251
  extracted_text += question
252
 
253
  if extracted_text:
@@ -266,6 +276,7 @@ def soap_report(doc_name, question):
266
 
267
  return response
268
 
 
269
  def search_gpt(question):
270
  from langchain.llms import OpenAI
271
  from langchain import PromptTemplate, LLMChain
@@ -281,6 +292,7 @@ def search_gpt(question):
281
 
282
  return response
283
 
 
284
  def local_gpt(question):
285
  from langchain.llms import OpenAI
286
  from langchain import PromptTemplate, LLMChain
@@ -296,11 +308,14 @@ def local_gpt(question):
296
 
297
  return response
298
 
 
299
  global output
300
  global response
301
 
 
302
  def audio_text(filepath):
303
  import openai
 
304
  global output
305
 
306
  audio = open(filepath, "rb")
@@ -309,12 +324,16 @@ def audio_text(filepath):
309
 
310
  return output
311
 
 
312
  def transcript(text):
313
  from langchain.llms import OpenAI
314
  from langchain import PromptTemplate, LLMChain
 
315
  global response
316
 
317
- question = "Use the following context given below to generate a detailed SOAP Report:\n\n"
 
 
318
  question += text
319
  print(question)
320
 
@@ -329,14 +348,18 @@ def transcript(text):
329
 
330
  return response
331
 
 
332
  def text_soap():
333
  from langchain.llms import OpenAI
334
  from langchain import PromptTemplate, LLMChain
 
335
  global output
336
  global response
337
  output = output
338
 
339
- question = "Use the following context given below to generate a detailed SOAP Report:\n\n"
 
 
340
  question += output
341
  print(question)
342
 
@@ -351,12 +374,15 @@ def text_soap():
351
 
352
  return response
353
 
 
354
  global path
355
 
 
356
  def docx(name):
357
  global response
358
  response = response
359
  import docx
 
360
  global path
361
  path = f"/home/user/app/docs/{name}.docx"
362
 
@@ -366,6 +392,7 @@ def docx(name):
366
 
367
  return "Successfully saved .docx File"
368
 
 
369
  import gradio as gr
370
 
371
  css = """
@@ -383,76 +410,73 @@ with gr.Blocks(css=css) as demo:
383
  gr.Markdown("File Chatting App")
384
 
385
  with gr.Tab("Chat with Files"):
386
- with gr.Column(elem_classes="col"):
 
 
 
 
 
387
 
388
- with gr.Tab("Upload and Process Files"):
389
- with gr.Column():
 
390
 
391
- api_key_input = gr.Textbox(label="Enter PI Key here")
392
- api_key_button = gr.Button("Submit")
393
- api_key_output = gr.Textbox(label="Output")
394
 
395
- file_input = gr.Files(label="Upload File(s) here")
396
- upload_button = gr.Button("Upload")
397
- file_output = gr.Textbox(label="Output")
 
 
398
 
399
- process_button = gr.Button("Process")
400
- process_output = gr.Textbox(label="Output")
401
 
402
- with gr.Tab("Ask Questions to Files"):
403
- with gr.Column():
404
-
405
- search_input = gr.Textbox(label="Enter Question here")
406
- search_button = gr.Button("Search")
407
- search_output = gr.Textbox(label="Output")
408
-
409
- search_gpt_button = gr.Button("Ask ChatGPT")
410
- search_gpt_output = gr.Textbox(label="Output")
411
-
412
- delete_button = gr.Button("Delete")
413
- delete_output = gr.Textbox(label="Output")
414
 
415
  with gr.Tab("Chat with Local Files"):
416
- with gr.Column(elem_classes="col"):
 
 
 
417
 
418
- local_search_input = gr.Textbox(label="Enter Question here")
419
- local_search_button = gr.Button("Search")
420
- local_search_output = gr.Textbox(label="Output")
421
-
422
- local_gpt_button = gr.Button("Ask ChatGPT")
423
- local_gpt_output = gr.Textbox(label="Output")
424
 
425
  with gr.Tab("Ask Question to SOAP Report"):
426
- with gr.Column(elem_classes="col"):
427
-
428
- refresh_button = gr.Button("Refresh")
429
- soap_input = gr.Dropdown(label="Choose File")
430
- soap_question = gr.Textbox(label="Enter Question here")
431
- soap_button = gr.Button("Submit")
432
- soap_output = gr.Textbox(label="Output")
433
 
434
  with gr.Tab("Convert Audio to SOAP Report"):
435
- with gr.Column(elem_classes="col"):
436
-
437
- mic_text_input = gr.Audio(source="microphone", type="filepath", label="Speak to the Microphone")
438
- mic_text_button = gr.Button("Generate Transcript")
439
- mic_text_output = gr.Textbox(label="Output")
440
-
441
- upload_text_input = gr.Audio(source="upload", type="filepath", label="Upload Audio File here")
442
- upload_text_button = gr.Button("Generate Transcript")
443
- upload_text_output = gr.Textbox(label="Output")
444
-
445
- transcript_input = gr.Textbox(label="Enter Transcript here")
446
- transcript_button = gr.Button("Generate SOAP Report")
447
- transcript_output = gr.Textbox(label="Output")
448
-
449
- text_soap_button = gr.Button("Generate SOAP Report")
450
- text_soap_output = gr.Textbox(label="Output")
451
-
452
- docx_input = gr.Textbox(label="Enter the name of .docx File")
453
- docx_button = gr.Button("Save .docx File")
454
- docx_output = gr.Textbox(label="Output")
455
-
 
 
456
 
457
  api_key_button.click(api_key, inputs=api_key_input, outputs=api_key_output)
458
 
@@ -465,30 +489,29 @@ with gr.Blocks(css=css) as demo:
465
 
466
  delete_button.click(delete_file, inputs=None, outputs=delete_output)
467
 
468
- local_search_button.click(search_local, inputs=local_search_input, outputs=local_search_output)
469
- local_gpt_button.click(local_gpt, inputs=local_search_input, outputs=local_gpt_output)
 
 
 
 
470
 
471
  refresh_button.click(list_files, inputs=None, outputs=soap_input)
472
- soap_button.click(soap_report, inputs=[soap_input, soap_question], outputs=soap_output)
 
 
473
 
474
  mic_text_button.click(audio_text, inputs=mic_text_input, outputs=mic_text_output)
475
- upload_text_button.click(audio_text, inputs=upload_text_input, outputs=upload_text_output)
 
 
476
 
477
- transcript_button.click(transcript, inputs=transcript_input, outputs=transcript_output)
 
 
478
  text_soap_button.click(text_soap, inputs=None, outputs=text_soap_output)
479
  docx_button.click(docx, inputs=docx_input, outputs=docx_output)
480
 
481
 
482
  demo.queue()
483
- demo.launch(debug=True, share=True)
484
-
485
-
486
- # Commented out IPython magic to ensure Python compatibility.
487
- #download file_db
488
-
489
- # %cd /home/user/app/
490
-
491
- !zip -r "file_db.zip" "file_db"
492
-
493
- from IPython.display import FileLink
494
- FileLink("file_db.zip")
 
74
 
75
  # copy_files(source_folder, destination_folder)
76
 
 
 
 
 
 
 
77
 
78
  def api_key(key):
 
79
  import os
80
  import openai
81
 
 
85
 
86
  return "Successful!"
87
 
88
+
89
  def save_file(input_file):
90
  import shutil
91
  import os
 
93
  destination_dir = "/home/user/app/file/"
94
  os.makedirs(destination_dir, exist_ok=True)
95
 
96
+ output_dir = "/home/user/app/file/"
97
 
98
  for file in input_file:
99
+ shutil.copy(file.name, output_dir)
100
 
101
  return "File(s) saved successfully!"
102
 
103
+
104
  def process_file():
105
  from langchain.document_loaders import PyPDFLoader
106
  from langchain.document_loaders import DirectoryLoader
 
111
  from langchain.text_splitter import CharacterTextSplitter
112
  import openai
113
 
114
+ loader1 = DirectoryLoader(
115
+ "/home/user/app/file/", glob="./*.pdf", loader_cls=PyPDFLoader
116
+ )
117
  document1 = loader1.load()
118
 
119
+ loader2 = DirectoryLoader(
120
+ "/home/user/app/file/", glob="./*.txt", loader_cls=TextLoader
121
+ )
122
  document2 = loader2.load()
123
 
124
+ loader3 = DirectoryLoader(
125
+ "/home/user/app/file/", glob="./*.docx", loader_cls=Docx2txtLoader
126
+ )
127
  document3 = loader3.load()
128
 
129
  document1.extend(document2)
130
  document1.extend(document3)
131
 
132
  text_splitter = CharacterTextSplitter(
133
+ separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len
134
+ )
 
 
135
 
136
  docs = text_splitter.split_documents(document1)
137
  embeddings = OpenAIEmbeddings()
 
141
 
142
  return "File(s) processed successfully!"
143
 
144
+
145
  def formatted_response(docs, response):
146
  formatted_output = response + "\n\nSources"
147
 
148
  for i, doc in enumerate(docs):
149
+ source_info = doc.metadata.get("source", "Unknown source")
150
+ page_info = doc.metadata.get("page", None)
151
 
152
  # Get the file name without the directory path
153
+ file_name = source_info.split("/")[-1].strip()
154
 
155
  if page_info is not None:
156
  formatted_output += f"\n{file_name}\tpage no {page_info}"
 
159
 
160
  return formatted_output
161
 
162
+
163
  def search_file(question):
164
  from langchain.embeddings.openai import OpenAIEmbeddings
165
  from langchain.vectorstores import FAISS
 
168
  from langchain.llms import OpenAI
169
  import openai
170
  from langchain.chat_models import ChatOpenAI
171
+
172
  embeddings = OpenAIEmbeddings()
173
  file_db = FAISS.load_local("/home/user/app/file_db/", embeddings)
174
  docs = file_db.similarity_search(question)
175
 
176
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
177
  chain = load_qa_chain(llm, chain_type="stuff")
178
  with get_openai_callback() as cb:
179
  response = chain.run(input_documents=docs, question=question)
 
181
 
182
  return formatted_response(docs, response)
183
 
184
+
185
  def search_local(question):
186
  from langchain.embeddings.openai import OpenAIEmbeddings
187
  from langchain.vectorstores import FAISS
 
190
  from langchain.llms import OpenAI
191
  import openai
192
  from langchain.chat_models import ChatOpenAI
193
+
194
  embeddings = OpenAIEmbeddings()
195
  file_db = FAISS.load_local("/home/user/app/local_db/", embeddings)
196
  docs = file_db.similarity_search(question)
197
 
198
  print(docs)
199
  type(docs)
200
+ llm = ChatOpenAI(model_name="gpt-3.5-turbo")
201
  chain = load_qa_chain(llm, chain_type="stuff")
202
  with get_openai_callback() as cb:
203
  response = chain.run(input_documents=docs, question=question)
 
205
 
206
  return formatted_response(docs, response)
207
 
 
208
 
209
+ def delete_file():
210
  import shutil
211
 
212
  path1 = "/home/user/app/file/"
 
220
  except:
221
  return "Already Deleted"
222
 
223
+
224
  import os
225
  import gradio as gr
226
 
227
+
228
  def list_files():
229
+ directory = "/home/user/app/docs"
230
  file_list = []
231
  for root, dirs, files in os.walk(directory):
232
  for file in files:
233
  file_list.append(file)
234
  return gr.Dropdown.update(choices=file_list)
235
 
236
+
237
  file_list = list_files()
238
 
239
  print("List of file names in the directory:")
240
  for file_name in file_list:
241
  print(file_name)
242
 
243
+
244
  def soap_report(doc_name, question):
245
  from langchain.llms import OpenAI
246
  from langchain import PromptTemplate, LLMChain
247
  import openai
248
  import docx
249
 
250
+ docx_path = "/home/user/app/docs/" + doc_name
251
 
252
  doc = docx.Document(docx_path)
253
+ extracted_text = "Extracted text:\n\n\n"
254
 
255
  for paragraph in doc.paragraphs:
256
+ extracted_text += paragraph.text + "\n"
257
 
258
+ question = (
259
+ "\n\nUse the 'Extracted text' to answer the following question:\n" + question
260
+ )
261
  extracted_text += question
262
 
263
  if extracted_text:
 
276
 
277
  return response
278
 
279
+
280
  def search_gpt(question):
281
  from langchain.llms import OpenAI
282
  from langchain import PromptTemplate, LLMChain
 
292
 
293
  return response
294
 
295
+
296
  def local_gpt(question):
297
  from langchain.llms import OpenAI
298
  from langchain import PromptTemplate, LLMChain
 
308
 
309
  return response
310
 
311
+
312
  global output
313
  global response
314
 
315
+
316
  def audio_text(filepath):
317
  import openai
318
+
319
  global output
320
 
321
  audio = open(filepath, "rb")
 
324
 
325
  return output
326
 
327
+
328
  def transcript(text):
329
  from langchain.llms import OpenAI
330
  from langchain import PromptTemplate, LLMChain
331
+
332
  global response
333
 
334
+ question = (
335
+ "Use the following context given below to generate a detailed SOAP Report:\n\n"
336
+ )
337
  question += text
338
  print(question)
339
 
 
348
 
349
  return response
350
 
351
+
352
  def text_soap():
353
  from langchain.llms import OpenAI
354
  from langchain import PromptTemplate, LLMChain
355
+
356
  global output
357
  global response
358
  output = output
359
 
360
+ question = (
361
+ "Use the following context given below to generate a detailed SOAP Report:\n\n"
362
+ )
363
  question += output
364
  print(question)
365
 
 
374
 
375
  return response
376
 
377
+
378
  global path
379
 
380
+
381
  def docx(name):
382
  global response
383
  response = response
384
  import docx
385
+
386
  global path
387
  path = f"/home/user/app/docs/{name}.docx"
388
 
 
392
 
393
  return "Successfully saved .docx File"
394
 
395
+
396
  import gradio as gr
397
 
398
  css = """
 
410
  gr.Markdown("File Chatting App")
411
 
412
  with gr.Tab("Chat with Files"):
413
+ with gr.Column(elem_classes="col"):
414
+ with gr.Tab("Upload and Process Files"):
415
+ with gr.Column():
416
+ api_key_input = gr.Textbox(label="Enter API Key here")
417
+ api_key_button = gr.Button("Submit")
418
+ api_key_output = gr.Textbox(label="Output")
419
 
420
+ file_input = gr.Files(label="Upload File(s) here")
421
+ upload_button = gr.Button("Upload")
422
+ file_output = gr.Textbox(label="Output")
423
 
424
+ process_button = gr.Button("Process")
425
+ process_output = gr.Textbox(label="Output")
 
426
 
427
+ with gr.Tab("Ask Questions to Files"):
428
+ with gr.Column():
429
+ search_input = gr.Textbox(label="Enter Question here")
430
+ search_button = gr.Button("Search")
431
+ search_output = gr.Textbox(label="Output")
432
 
433
+ search_gpt_button = gr.Button("Ask ChatGPT")
434
+ search_gpt_output = gr.Textbox(label="Output")
435
 
436
+ delete_button = gr.Button("Delete")
437
+ delete_output = gr.Textbox(label="Output")
 
 
 
 
 
 
 
 
 
 
438
 
439
  with gr.Tab("Chat with Local Files"):
440
+ with gr.Column(elem_classes="col"):
441
+ local_search_input = gr.Textbox(label="Enter Question here")
442
+ local_search_button = gr.Button("Search")
443
+ local_search_output = gr.Textbox(label="Output")
444
 
445
+ local_gpt_button = gr.Button("Ask ChatGPT")
446
+ local_gpt_output = gr.Textbox(label="Output")
 
 
 
 
447
 
448
  with gr.Tab("Ask Question to SOAP Report"):
449
+ with gr.Column(elem_classes="col"):
450
+ refresh_button = gr.Button("Refresh")
451
+ soap_input = gr.Dropdown(label="Choose File")
452
+ soap_question = gr.Textbox(label="Enter Question here")
453
+ soap_button = gr.Button("Submit")
454
+ soap_output = gr.Textbox(label="Output")
 
455
 
456
  with gr.Tab("Convert Audio to SOAP Report"):
457
+ with gr.Column(elem_classes="col"):
458
+ mic_text_input = gr.Audio(
459
+ source="microphone", type="filepath", label="Speak to the Microphone"
460
+ )
461
+ mic_text_button = gr.Button("Generate Transcript")
462
+ mic_text_output = gr.Textbox(label="Output")
463
+
464
+ upload_text_input = gr.Audio(
465
+ source="upload", type="filepath", label="Upload Audio File here"
466
+ )
467
+ upload_text_button = gr.Button("Generate Transcript")
468
+ upload_text_output = gr.Textbox(label="Output")
469
+
470
+ transcript_input = gr.Textbox(label="Enter Transcript here")
471
+ transcript_button = gr.Button("Generate SOAP Report")
472
+ transcript_output = gr.Textbox(label="Output")
473
+
474
+ text_soap_button = gr.Button("Generate SOAP Report")
475
+ text_soap_output = gr.Textbox(label="Output")
476
+
477
+ docx_input = gr.Textbox(label="Enter the name of .docx File")
478
+ docx_button = gr.Button("Save .docx File")
479
+ docx_output = gr.Textbox(label="Output")
480
 
481
  api_key_button.click(api_key, inputs=api_key_input, outputs=api_key_output)
482
 
 
489
 
490
  delete_button.click(delete_file, inputs=None, outputs=delete_output)
491
 
492
+ local_search_button.click(
493
+ search_local, inputs=local_search_input, outputs=local_search_output
494
+ )
495
+ local_gpt_button.click(
496
+ local_gpt, inputs=local_search_input, outputs=local_gpt_output
497
+ )
498
 
499
  refresh_button.click(list_files, inputs=None, outputs=soap_input)
500
+ soap_button.click(
501
+ soap_report, inputs=[soap_input, soap_question], outputs=soap_output
502
+ )
503
 
504
  mic_text_button.click(audio_text, inputs=mic_text_input, outputs=mic_text_output)
505
+ upload_text_button.click(
506
+ audio_text, inputs=upload_text_input, outputs=upload_text_output
507
+ )
508
 
509
+ transcript_button.click(
510
+ transcript, inputs=transcript_input, outputs=transcript_output
511
+ )
512
  text_soap_button.click(text_soap, inputs=None, outputs=text_soap_output)
513
  docx_button.click(docx, inputs=docx_input, outputs=docx_output)
514
 
515
 
516
  demo.queue()
517
+ demo.launch()