Karthikeyan commited on
Commit
b0609c9
0 Parent(s):

Duplicate from ADOPLE/Adopleai-DocumentQA

Browse files
Files changed (5) hide show
  1. .gitattributes +35 -0
  2. README.md +13 -0
  3. app.py +112 -0
  4. requirements.txt +14 -0
  5. style.css +28 -0
.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: DocumentQA
3
+ emoji: 🏃
4
+ colorFrom: red
5
+ colorTo: red
6
+ sdk: gradio
7
+ sdk_version: 3.35.2
8
+ app_file: app.py
9
+ pinned: false
10
+ duplicated_from: ADOPLE/Adopleai-DocumentQA
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,112 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import NoneStr
2
+ import os
3
+ from langchain.chains.question_answering import load_qa_chain
4
+ from langchain.document_loaders import UnstructuredFileLoader
5
+ from langchain.embeddings.openai import OpenAIEmbeddings
6
+ from langchain.llms import OpenAI
7
+ from langchain.text_splitter import CharacterTextSplitter
8
+ from langchain.vectorstores import FAISS
9
+ from pypdf import PdfReader
10
+ import mimetypes
11
+ import validators
12
+ import requests
13
+ import tempfile
14
+ import gradio as gr
15
+ import openai
16
+
17
+ def get_empty_state():
18
+ return {"knowledge_base": None}
19
+
20
+
21
+ def create_knowledge_base(docs):
22
+ # split into chunks
23
+ text_splitter = CharacterTextSplitter(
24
+ separator="\n", chunk_size=500, chunk_overlap=0, length_function=len
25
+ )
26
+ chunks = text_splitter.split_documents(docs)
27
+
28
+ # Create embeddings
29
+ embeddings = OpenAIEmbeddings()
30
+ knowledge_base = FAISS.from_documents(chunks, embeddings)
31
+ return knowledge_base
32
+
33
+
34
+ def upload_file(file_obj):
35
+ try:
36
+ loader = UnstructuredFileLoader(file_obj.name, strategy="fast")
37
+ docs = loader.load()
38
+
39
+ knowledge_base = create_knowledge_base(docs)
40
+ except:
41
+ text="Try Another file"
42
+ return file_obj.name, text
43
+
44
+ return file_obj.name, {"knowledge_base": knowledge_base}
45
+
46
+
47
+ def upload_via_url(url):
48
+ if validators.url(url):
49
+ r = requests.get(url)
50
+
51
+ if r.status_code != 200:
52
+ raise ValueError(
53
+ "Check the url of your file; returned status code %s" % r.status_code
54
+ )
55
+
56
+ content_type = r.headers.get("content-type")
57
+ file_extension = mimetypes.guess_extension(content_type)
58
+ temp_file = tempfile.NamedTemporaryFile(suffix=file_extension, delete=False)
59
+ temp_file.write(r.content)
60
+ file_path = temp_file.name
61
+ loader = UnstructuredFileLoader(file_path, strategy="fast")
62
+ docs = loader.load()
63
+ with open(file_path, mode="rb") as f:
64
+ pass
65
+ knowledge_base = create_knowledge_base(docs)
66
+ return file_path, {"knowledge_base": knowledge_base}
67
+ else:
68
+ raise ValueError("Please enter a valid URL")
69
+
70
+
71
+ def answer_question(question, state):
72
+
73
+ try:
74
+ knowledge_base = state["knowledge_base"]
75
+ docs = knowledge_base.similarity_search(question)
76
+
77
+ llm = OpenAI(temperature=0.4)
78
+ chain = load_qa_chain(llm, chain_type="stuff")
79
+ response = chain.run(input_documents=docs, question=question)
80
+ return response
81
+ except:
82
+ return "Please upload Proper Document"
83
+ title = """<br><br><br><div style="text-align: center;max-width: 700px;">
84
+ <h1><a style="display:inline-block; margin-left: 1em; text-decoration:none; font-weight:bold;" href="https://www.adople.com">ADOPLE AI</a> - Document QA</h1>
85
+ </p>"""
86
+ with gr.Blocks(css="style.css",theme=gr.themes.Soft()) as demo:
87
+ state = gr.State(get_empty_state())
88
+ with gr.Column(elem_id="col-container"):
89
+ gr.HTML(title)
90
+ gr.Markdown("**Upload your file**")
91
+ with gr.Row(elem_id="row-flex"):
92
+ with gr.Column(scale=0.85):
93
+ file_url = gr.Textbox(
94
+ value="",
95
+ label="Upload your file",
96
+ placeholder="Enter a url",
97
+ show_label=False,
98
+ visible=True,elem_classes="filenameshow")
99
+ with gr.Column(scale=0.15, min_width=160):
100
+ upload_button = gr.UploadButton("Browse File", file_types=[".txt", ".pdf", ".doc", ".docx"],elem_classes="filenameshow")
101
+ file_output = gr.File(elem_classes="filenameshow")
102
+ with gr.Row():
103
+ with gr.Column(scale=1, min_width=0):
104
+ user_question = gr.Textbox(value="",label='Question Box :',show_label=True, placeholder="Ask a question about your file:",elem_classes="spaceH")
105
+ with gr.Row():
106
+ with gr.Column(scale=1, min_width=0):
107
+ answer = gr.Textbox(value="",label='Answer Box :',show_label=True, placeholder="",lines=5)
108
+ file_url.submit(upload_via_url, file_url, [file_output, state])
109
+ upload_button.upload(upload_file, upload_button, [file_output,state])
110
+ user_question.submit(answer_question, [user_question, state], [answer])
111
+
112
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ tiktoken
3
+ langchain
4
+ gradio
5
+ pypdf
6
+ requests
7
+ unstructured
8
+ validators
9
+ pytesseract
10
+ pdf2image
11
+ tabulate
12
+ nltk
13
+ python-dotenv
14
+ faiss-cpu
style.css ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #col-container {
2
+ max-width: 700px;
3
+ margin-left: auto;
4
+ margin-right: auto;
5
+ }
6
+ #row-flex {
7
+ display: flex;
8
+ align-items: center;
9
+ justify-content: center;
10
+ }
11
+ .filenameshow{
12
+ height:85px;
13
+ }
14
+ .spaceH{
15
+ padding-top:45px;
16
+ }
17
+ .leftimage .rightimage{
18
+ float:left;
19
+ }
20
+ .leftimage{
21
+ padding-top:26px;
22
+ margin-left:340px;
23
+ }
24
+ .rightimage{
25
+ margin-right:360px;
26
+ margin-top:15px;
27
+ }
28
+