Spaces:
Running
Running
mckplus
commited on
Commit
β’
d38c97d
0
Parent(s):
Duplicate from mckplus/DocuChat
Browse files- .gitattributes +35 -0
- Dockerfile +16 -0
- DocuChat.py +87 -0
- README.md +41 -0
- requirements.txt +7 -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
|
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
RUN python3 -m pip install --no-cache-dir --upgrade pip
|
7 |
+
RUN python3 -m pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
CMD ["panel", "serve", "/code/DocuChat.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "*"]
|
12 |
+
|
13 |
+
RUN mkdir /.cache
|
14 |
+
RUN chmod 777 /.cache
|
15 |
+
RUN mkdir .chroma
|
16 |
+
RUN chmod 777 .chroma
|
DocuChat.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import re
|
3 |
+
import time
|
4 |
+
import threading
|
5 |
+
from langchain.chains import RetrievalQA
|
6 |
+
from langchain.llms import OpenAI
|
7 |
+
from langchain.document_loaders import PyPDFLoader
|
8 |
+
from langchain.text_splitter import CharacterTextSplitter
|
9 |
+
from langchain.embeddings import OpenAIEmbeddings
|
10 |
+
from langchain.vectorstores import Chroma
|
11 |
+
import panel as pn
|
12 |
+
|
13 |
+
# Set global sizing mode
|
14 |
+
pn.config.sizing_mode = 'stretch_width'
|
15 |
+
|
16 |
+
# Panel extension
|
17 |
+
pn.extension('texteditor', template="bootstrap")
|
18 |
+
|
19 |
+
file_input = pn.widgets.FileInput(height=45)
|
20 |
+
openaikey = pn.widgets.PasswordInput(value="", placeholder="Enter your OpenAI API Key here...", height=45)
|
21 |
+
prompt = pn.widgets.TextEditor(value="", placeholder="Ask a question...", height=100, toolbar=False)
|
22 |
+
run_button = pn.widgets.Button(name="β", width=30, height=30)
|
23 |
+
|
24 |
+
text_editor_with_button = pn.Row(prompt, run_button, align='end')
|
25 |
+
|
26 |
+
def remove_empty_lines(text):
|
27 |
+
lines = re.split(r'\r\n|\r|\n', text)
|
28 |
+
return '\n'.join([line.strip() for line in lines if line.strip()])
|
29 |
+
|
30 |
+
def toggle_run_button(state):
|
31 |
+
run_button.disabled = state
|
32 |
+
|
33 |
+
def run_button_reappear():
|
34 |
+
time.sleep(10)
|
35 |
+
toggle_run_button(False)
|
36 |
+
|
37 |
+
def qa(file, query):
|
38 |
+
loader = PyPDFLoader(file)
|
39 |
+
documents = loader.load()
|
40 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
41 |
+
texts = text_splitter.split_documents(documents)
|
42 |
+
embeddings = OpenAIEmbeddings()
|
43 |
+
db = Chroma.from_documents(texts, embeddings)
|
44 |
+
retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 3})
|
45 |
+
qa = RetrievalQA.from_chain_type(llm=OpenAI(), chain_type="stuff", retriever=retriever, return_source_documents=True)
|
46 |
+
result = qa({"query": query})
|
47 |
+
return result['result']
|
48 |
+
|
49 |
+
convos = []
|
50 |
+
|
51 |
+
def qa_result(_):
|
52 |
+
toggle_run_button(True)
|
53 |
+
threading.Thread(target=run_button_reappear).start() # Start a timer to re-enable the button
|
54 |
+
|
55 |
+
os.environ["OPENAI_API_KEY"] = openaikey.value
|
56 |
+
if file_input.value is not None:
|
57 |
+
file_input.save("/.cache/temp.pdf")
|
58 |
+
prompt_text = remove_empty_lines(prompt.value)
|
59 |
+
prompt.value = "" # Clear the prompt textbox
|
60 |
+
if prompt_text:
|
61 |
+
result = qa(file="/.cache/temp.pdf", query=prompt_text)
|
62 |
+
convos.extend([
|
63 |
+
pn.Row(pn.panel("π€", width=5), prompt_text),
|
64 |
+
pn.Row(pn.panel("π", width=5), pn.panel(result, style={'color': '#5b5b5b'}), height_policy='min', min_height=300) # Adjusted height policy here
|
65 |
+
])
|
66 |
+
toggle_run_button(False)
|
67 |
+
return pn.Column(*convos, sizing_mode='stretch_both', margin=10, min_height=300)
|
68 |
+
|
69 |
+
|
70 |
+
qa_interactive = pn.panel(pn.bind(qa_result, run_button), loading_indicator=False)
|
71 |
+
|
72 |
+
output = pn.WidgetBox('', qa_interactive, scroll=True)
|
73 |
+
|
74 |
+
layout = pn.Column(
|
75 |
+
pn.pane.Markdown("""
|
76 |
+
# DocuChat
|
77 |
+
AI-Powered Query Engine for Document Insights (powered by LangChain & OpenAI)
|
78 |
+
|
79 |
+
## How it works:
|
80 |
+
1) Upload a PDF
|
81 |
+
2) Enter your OpenAI API key (get one via [OpenAI](https://platform.openai.com/account))
|
82 |
+
3) Type a question and your document will get analyzed for an answer
|
83 |
+
|
84 |
+
Built by [McKenzie](https://www.mckenzielloydsmith.com/home?utm_source=HuggingFace&utm_medium=PDF+Analyzer).
|
85 |
+
"""),
|
86 |
+
pn.Row(file_input, openaikey), output, text_editor_with_button
|
87 |
+
).servable()
|
README.md
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: DocuChat
|
3 |
+
emoji: π
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: pink
|
6 |
+
sdk: docker
|
7 |
+
pinned: true
|
8 |
+
duplicated_from: mckplus/DocuChat
|
9 |
+
---
|
10 |
+
|
11 |
+
# DocuChat: The LangChain-Powered PDF Analysis π
|
12 |
+
|
13 |
+
Here's how it works:
|
14 |
+
|
15 |
+
1. Upload a PDF: Select any PDF you want to analyze.
|
16 |
+
2. Enter Your OpenAI API Key: To leverage OpenAI's powerful language models, you'll need to provide your API key.
|
17 |
+
3. Ask a Question: Type your question into the text box and hit "Run."
|
18 |
+
4. Get the Answer: The system will analyze your PDF and respond with the most relevant answer.
|
19 |
+
|
20 |
+
|
21 |
+
# Technical Overview
|
22 |
+
1. PDF Loading and Parsing
|
23 |
+
- PDF Loader: Utilizes the PyPDFLoader from LangChain to read the uploaded PDF file.
|
24 |
+
- Document Splitter: The CharacterTextSplitter divides the PDF into manageable chunks for further processing.
|
25 |
+
2. Embeddings and Vector Storage
|
26 |
+
- Embeddings: The OpenAIEmbeddings class is used to transform the text into a format that can be understood by the models.
|
27 |
+
- Vector Storage: Chroma is used to store the vectors of the documents for quick retrieval.
|
28 |
+
3. Question Answering
|
29 |
+
- RetrievalQA: This class, from LangChain's chains, handles the core of the question-answering process.
|
30 |
+
- OpenAI as Language Model: The OpenAI class is utilized as the core language model for the retrieval.
|
31 |
+
4. Interactive UI
|
32 |
+
- Panel Library: The entire interface is built using the Panel library, making it highly interactive and user-friendly.
|
33 |
+
- Responsive Design: The UI adapts to various screen sizes, providing a smooth experience across devices.
|
34 |
+
|
35 |
+
|
36 |
+
# Built by McKenzie
|
37 |
+
This project is created and maintained by [McKenzie](https://www.mckenzielloydsmith.com/home?utm_source=HuggingFace&utm_medium=PDF+Analyzer). Contributions and feedback are always welcome!
|
38 |
+
& credit to sophiamyang for the inspo & intro to panel π
|
39 |
+
|
40 |
+
|
41 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain
|
2 |
+
openai
|
3 |
+
chromadb
|
4 |
+
pypdf
|
5 |
+
tiktoken
|
6 |
+
panel>=0.12.0
|
7 |
+
notebook
|