File size: 15,092 Bytes
252c37d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252c37d
4aee695
 
 
252c37d
 
 
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252c37d
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
0a3a31f
4aee695
 
0a3a31f
4aee695
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
 
 
0a3a31f
 
 
4aee695
 
0a3a31f
 
 
4aee695
 
0a3a31f
 
 
4aee695
 
 
 
 
 
0a3a31f
 
4aee695
 
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
0a3a31f
 
4aee695
 
0a3a31f
4aee695
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
0a3a31f
4aee695
252c37d
4aee695
0a3a31f
252c37d
0a3a31f
4aee695
 
 
 
252c37d
4aee695
0a3a31f
252c37d
4aee695
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
0a3a31f
4aee695
 
0a3a31f
4aee695
 
0a3a31f
4aee695
0a3a31f
 
 
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
0a3a31f
4aee695
 
0a3a31f
4aee695
 
 
 
 
 
 
 
0a3a31f
252c37d
 
 
0a3a31f
252c37d
 
0a3a31f
 
 
252c37d
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
0a3a31f
4aee695
 
 
 
0a3a31f
 
 
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
0a3a31f
252c37d
 
0a3a31f
4aee695
 
 
 
0a3a31f
252c37d
4aee695
 
 
 
 
 
 
 
0a3a31f
4aee695
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252c37d
0a3a31f
 
 
 
 
 
4aee695
0a3a31f
 
 
4aee695
0a3a31f
 
4aee695
0a3a31f
 
 
 
 
4aee695
0a3a31f
 
4aee695
0a3a31f
 
4aee695
252c37d
0a3a31f
 
 
 
4aee695
0a3a31f
 
4aee695
 
0a3a31f
 
 
 
 
 
4aee695
 
0a3a31f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
252c37d
4aee695
 
 
252c37d
4aee695
 
 
 
 
 
 
0a3a31f
 
 
 
 
 
4aee695
252c37d
0a3a31f
 
 
4aee695
252c37d
0a3a31f
 
 
4aee695
0a3a31f
 
 
4aee695
 
 
 
 
0a3a31f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
# # -*- coding: utf-8 -*-
# """fiver-app.ipynb

# Automatically generated by Colaboratory.

# Original file is located at
#     https://colab.research.google.com/drive/1YQm_fGxa2nfiV8pTN4oBrlzzfefGadaP
# """

# !pip uninstall -y numpy
# !pip install --ignore-installed numpy==1.22.0

# !pip install langchain
# !pip install PyPDF2
# !pip install docx2txt
# !pip install gradio
# !pip install faiss-gpu
# !pip install openai
# !pip install tiktoken
# !pip install python-docx

# !pip install git+https://github.com/openai/whisper.git
# !pip install sounddevice

# import shutil
# import os

# def copy_files(source_folder, destination_folder):
#     # Create the destination folder if it doesn't exist
#     if not os.path.exists(destination_folder):
#         os.makedirs(destination_folder)

#     # Get a list of files in the source folder

#     files_to_copy = os.listdir(source_folder)
#     for file_name in files_to_copy:
#         source_file_path = os.path.join(source_folder, file_name)
#         destination_file_path = os.path.join(destination_folder, file_name)

#         # Copy the file to the destination folder
#         shutil.copy(source_file_path, destination_file_path)

#         print(f"Copied {file_name} to {destination_folder}")

# # Specify the source folder and destination folder paths
# source_folder = "/kaggle/input/fiver-app5210"
# destination_folder = "/home/user/app/local_db"

# copy_files(source_folder, destination_folder)

# import shutil
# import os

# def copy_files(source_folder, destination_folder):
#     # Create the destination folder if it doesn't exist
#     if not os.path.exists(destination_folder):
#         os.makedirs(destination_folder)

#     # Get a list of files in the source folder
#     files_to_copy = os.listdir(source_folder)

#     for file_name in files_to_copy:
#         source_file_path = os.path.join(source_folder, file_name)
#         destination_file_path = os.path.join(destination_folder, file_name)

#         # Copy the file to the destination folder
#         shutil.copy(source_file_path, destination_file_path)

#         print(f"Copied {file_name} to {destination_folder}")

# # Specify the source folder and destination folder paths
# source_folder = "/kaggle/input/fiver-app-docs"
# destination_folder = "/home/user/app/docs"

# copy_files(source_folder, destination_folder)


def api_key(key):
    import os
    import openai

    os.environ["TOKENIZERS_PARALLELISM"] = "false"
    os.environ["OPENAI_API_KEY"] = key
    openai.api_key = key

    return "Successful!"


def save_file(input_file):
    import shutil
    import os

    destination_dir = "/home/user/app/file/"
    os.makedirs(destination_dir, exist_ok=True)

    output_dir = "/home/user/app/file/"

    for file in input_file:
        shutil.copy(file.name, output_dir)

    return "File(s) saved successfully!"


def process_file():
    from langchain.document_loaders import PyPDFLoader
    from langchain.document_loaders import DirectoryLoader
    from langchain.document_loaders import TextLoader
    from langchain.document_loaders import Docx2txtLoader
    from langchain.vectorstores import FAISS
    from langchain.embeddings.openai import OpenAIEmbeddings
    from langchain.text_splitter import CharacterTextSplitter
    import openai

    loader1 = DirectoryLoader(
        "/home/user/app/file/", glob="./*.pdf", loader_cls=PyPDFLoader
    )
    document1 = loader1.load()

    loader2 = DirectoryLoader(
        "/home/user/app/file/", glob="./*.txt", loader_cls=TextLoader
    )
    document2 = loader2.load()

    loader3 = DirectoryLoader(
        "/home/user/app/file/", glob="./*.docx", loader_cls=Docx2txtLoader
    )
    document3 = loader3.load()

    document1.extend(document2)
    document1.extend(document3)

    text_splitter = CharacterTextSplitter(
        separator="\n", chunk_size=1000, chunk_overlap=200, length_function=len
    )

    docs = text_splitter.split_documents(document1)
    embeddings = OpenAIEmbeddings()

    file_db = FAISS.from_documents(docs, embeddings)
    file_db.save_local("/home/user/app/file_db/")

    return "File(s) processed successfully!"


def formatted_response(docs, response):
    formatted_output = response + "\n\nSources"

    for i, doc in enumerate(docs):
        source_info = doc.metadata.get("source", "Unknown source")
        page_info = doc.metadata.get("page", None)

        # Get the file name without the directory path
        file_name = source_info.split("/")[-1].strip()

        if page_info is not None:
            formatted_output += f"\n{file_name}\tpage no {page_info}"
        else:
            formatted_output += f"\n{file_name}"

    return formatted_output


def search_file(question):
    from langchain.embeddings.openai import OpenAIEmbeddings
    from langchain.vectorstores import FAISS
    from langchain.chains.question_answering import load_qa_chain
    from langchain.callbacks import get_openai_callback
    from langchain.llms import OpenAI
    import openai
    from langchain.chat_models import ChatOpenAI

    embeddings = OpenAIEmbeddings()
    file_db = FAISS.load_local("/home/user/app/file_db/", embeddings)
    docs = file_db.similarity_search(question)

    llm = ChatOpenAI(model_name="gpt-3.5-turbo")
    chain = load_qa_chain(llm, chain_type="stuff")
    with get_openai_callback() as cb:
        response = chain.run(input_documents=docs, question=question)
        print(cb)

    return formatted_response(docs, response)


def search_local(question):
    from langchain.embeddings.openai import OpenAIEmbeddings
    from langchain.vectorstores import FAISS
    from langchain.chains.question_answering import load_qa_chain
    from langchain.callbacks import get_openai_callback
    from langchain.llms import OpenAI
    import openai
    from langchain.chat_models import ChatOpenAI

    embeddings = OpenAIEmbeddings()
    file_db = FAISS.load_local("/home/user/app/local_db/", embeddings)
    docs = file_db.similarity_search(question)

    print(docs)
    type(docs)
    llm = ChatOpenAI(model_name="gpt-3.5-turbo")
    chain = load_qa_chain(llm, chain_type="stuff")
    with get_openai_callback() as cb:
        response = chain.run(input_documents=docs, question=question)
        print(cb)

    return formatted_response(docs, response)


def delete_file():
    import shutil

    path1 = "/home/user/app/file/"
    path2 = "/home/user/app/file_db/"

    try:
        shutil.rmtree(path1)
        shutil.rmtree(path2)
        return "Deleted Successfully"

    except:
        return "Already Deleted"


import os
import gradio as gr


def list_files():
    directory = "/home/user/app/docs"
    file_list = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            file_list.append(file)
    return gr.Dropdown.update(choices=file_list)


file_list = list_files()

print("List of file names in the directory:")
for file_name in file_list:
    print(file_name)


def soap_report(doc_name, question):
    from langchain.llms import OpenAI
    from langchain import PromptTemplate, LLMChain
    import openai
    import docx

    docx_path = "/home/user/app/docs/" + doc_name

    doc = docx.Document(docx_path)
    extracted_text = "Extracted text:\n\n\n"

    for paragraph in doc.paragraphs:
        extracted_text += paragraph.text + "\n"

    question = (
        "\n\nUse the 'Extracted text' to answer the following question:\n" + question
    )
    extracted_text += question

    if extracted_text:
        print(extracted_text)
    else:
        print("failed")

    template = """Question: {question}

    Answer: Let's think step by step."""

    prompt = PromptTemplate(template=template, input_variables=["question"])
    llm = OpenAI()
    llm_chain = LLMChain(prompt=prompt, llm=llm)
    response = llm_chain.run(extracted_text)

    return response


def search_gpt(question):
    from langchain.llms import OpenAI
    from langchain import PromptTemplate, LLMChain

    template = """Question: {question}

    Answer: Let's think step by step."""

    prompt = PromptTemplate(template=template, input_variables=["question"])
    llm = OpenAI()
    llm_chain = LLMChain(prompt=prompt, llm=llm)
    response = llm_chain.run(question)

    return response


def local_gpt(question):
    from langchain.llms import OpenAI
    from langchain import PromptTemplate, LLMChain

    template = """Question: {question}

    Answer: Let's think step by step."""

    prompt = PromptTemplate(template=template, input_variables=["question"])
    llm = OpenAI()
    llm_chain = LLMChain(prompt=prompt, llm=llm)
    response = llm_chain.run(question)

    return response


global output
global response


def audio_text(filepath):
    import openai

    global output

    audio = open(filepath, "rb")
    transcript = openai.Audio.transcribe("whisper-1", audio)
    output = transcript["text"]

    return output


def transcript(text):
    from langchain.llms import OpenAI
    from langchain import PromptTemplate, LLMChain

    global response

    question = (
        "Use the following context given below to generate a detailed SOAP Report:\n\n"
    )
    question += text
    print(question)

    template = """Question: {question}

    Answer: Let's think step by step."""

    prompt = PromptTemplate(template=template, input_variables=["question"])
    llm = OpenAI()
    llm_chain = LLMChain(prompt=prompt, llm=llm)
    response = llm_chain.run(question)

    return response


def text_soap():
    from langchain.llms import OpenAI
    from langchain import PromptTemplate, LLMChain

    global output
    global response
    output = output

    question = (
        "Use the following context given below to generate a detailed SOAP Report:\n\n"
    )
    question += output
    print(question)

    template = """Question: {question}

    Answer: Let's think step by step."""

    prompt = PromptTemplate(template=template, input_variables=["question"])
    llm = OpenAI()
    llm_chain = LLMChain(prompt=prompt, llm=llm)
    response = llm_chain.run(question)

    return response


global path


def docx(name):
    global response
    response = response
    import docx

    global path
    path = f"/home/user/app/docs/{name}.docx"

    doc = docx.Document()
    doc.add_paragraph(response)
    doc.save(path)

    return "Successfully saved .docx File"


import gradio as gr

css = """
.col{
    max-width: 50%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
"""

with gr.Blocks(css=css) as demo:
    gr.Markdown("File Chatting App")

    with gr.Tab("Chat with Files"):
        with gr.Column(elem_classes="col"):
            with gr.Tab("Upload and Process Files"):
                with gr.Column():
                    api_key_input = gr.Textbox(label="Enter API Key here")
                    api_key_button = gr.Button("Submit")
                    api_key_output = gr.Textbox(label="Output")

                    file_input = gr.Files(label="Upload File(s) here")
                    upload_button = gr.Button("Upload")
                    file_output = gr.Textbox(label="Output")

                    process_button = gr.Button("Process")
                    process_output = gr.Textbox(label="Output")

            with gr.Tab("Ask Questions to Files"):
                with gr.Column():
                    search_input = gr.Textbox(label="Enter Question here")
                    search_button = gr.Button("Search")
                    search_output = gr.Textbox(label="Output")

                    search_gpt_button = gr.Button("Ask ChatGPT")
                    search_gpt_output = gr.Textbox(label="Output")

                    delete_button = gr.Button("Delete")
                    delete_output = gr.Textbox(label="Output")

    with gr.Tab("Chat with Local Files"):
        with gr.Column(elem_classes="col"):
            local_search_input = gr.Textbox(label="Enter Question here")
            local_search_button = gr.Button("Search")
            local_search_output = gr.Textbox(label="Output")

            local_gpt_button = gr.Button("Ask ChatGPT")
            local_gpt_output = gr.Textbox(label="Output")

    with gr.Tab("Ask Question to SOAP Report"):
        with gr.Column(elem_classes="col"):
            refresh_button = gr.Button("Refresh")
            soap_input = gr.Dropdown(label="Choose File")
            soap_question = gr.Textbox(label="Enter Question here")
            soap_button = gr.Button("Submit")
            soap_output = gr.Textbox(label="Output")

    with gr.Tab("Convert Audio to SOAP Report"):
        with gr.Column(elem_classes="col"):
            mic_text_input = gr.Audio(
                source="microphone", type="filepath", label="Speak to the Microphone"
            )
            mic_text_button = gr.Button("Generate Transcript")
            mic_text_output = gr.Textbox(label="Output")

            upload_text_input = gr.Audio(
                source="upload", type="filepath", label="Upload Audio File here"
            )
            upload_text_button = gr.Button("Generate Transcript")
            upload_text_output = gr.Textbox(label="Output")

            transcript_input = gr.Textbox(label="Enter Transcript here")
            transcript_button = gr.Button("Generate SOAP Report")
            transcript_output = gr.Textbox(label="Output")

            text_soap_button = gr.Button("Generate SOAP Report")
            text_soap_output = gr.Textbox(label="Output")

            docx_input = gr.Textbox(label="Enter the name of .docx File")
            docx_button = gr.Button("Save .docx File")
            docx_output = gr.Textbox(label="Output")

    api_key_button.click(api_key, inputs=api_key_input, outputs=api_key_output)

    upload_button.click(save_file, inputs=file_input, outputs=file_output)

    process_button.click(process_file, inputs=None, outputs=process_output)

    search_button.click(search_file, inputs=search_input, outputs=search_output)
    search_gpt_button.click(search_gpt, inputs=search_input, outputs=search_gpt_output)

    delete_button.click(delete_file, inputs=None, outputs=delete_output)

    local_search_button.click(
        search_local, inputs=local_search_input, outputs=local_search_output
    )
    local_gpt_button.click(
        local_gpt, inputs=local_search_input, outputs=local_gpt_output
    )

    refresh_button.click(list_files, inputs=None, outputs=soap_input)
    soap_button.click(
        soap_report, inputs=[soap_input, soap_question], outputs=soap_output
    )

    mic_text_button.click(audio_text, inputs=mic_text_input, outputs=mic_text_output)
    upload_text_button.click(
        audio_text, inputs=upload_text_input, outputs=upload_text_output
    )

    transcript_button.click(
        transcript, inputs=transcript_input, outputs=transcript_output
    )
    text_soap_button.click(text_soap, inputs=None, outputs=text_soap_output)
    docx_button.click(docx, inputs=docx_input, outputs=docx_output)


demo.queue()
demo.launch()