Spaces:
Sleeping
Sleeping
sheraznaseer
commited on
Commit
•
d101a5e
1
Parent(s):
bca8e5b
added code, dockerfile and requirements.txt
Browse files- .gitignore +3 -0
- Dockerfile +12 -0
- app.py +127 -0
- requirements.txt +7 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.gitattributes
|
2 |
+
.vscode/
|
3 |
+
*.ipynb
|
Dockerfile
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.9
|
2 |
+
|
3 |
+
WORKDIR /code
|
4 |
+
|
5 |
+
COPY ./requirements.txt /code/requirements.txt
|
6 |
+
|
7 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
8 |
+
|
9 |
+
COPY . .
|
10 |
+
|
11 |
+
#CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
12 |
+
CMD ["panel", "serve", "/code/app.py", "--address", "0.0.0.0","--port", "7860", "--allow-websocket-origin","sheraznaseer-test-pdfqa-2304.hf.space"]
|
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.chains import ConversationalRetrievalChain
|
2 |
+
from langchain.chat_models import ChatOpenAI
|
3 |
+
from langchain.document_loaders import TextLoader
|
4 |
+
from langchain.document_loaders import PyPDFLoader
|
5 |
+
from langchain.vectorstores.faiss import FAISS
|
6 |
+
from langchain.embeddings import OpenAIEmbeddings
|
7 |
+
from langchain.text_splitter import CharacterTextSplitter
|
8 |
+
import panel as pn
|
9 |
+
import os
|
10 |
+
import tempfile
|
11 |
+
from langchain.chains import RetrievalQA
|
12 |
+
|
13 |
+
file_input = pn.widgets.FileInput(width=300)
|
14 |
+
|
15 |
+
openaikey = pn.widgets.PasswordInput(
|
16 |
+
value="", placeholder="Enter your OpenAI API Key here...", width=300
|
17 |
+
)
|
18 |
+
prompt = pn.widgets.TextEditor(
|
19 |
+
value="", placeholder="Enter your questions here...", height=160, toolbar=False
|
20 |
+
)
|
21 |
+
run_button = pn.widgets.Button(name="Run!")
|
22 |
+
|
23 |
+
select_k = pn.widgets.IntSlider(
|
24 |
+
name="Number of relevant chunks", start=1, end=5, step=1, value=2
|
25 |
+
)
|
26 |
+
select_chain_type = pn.widgets.RadioButtonGroup(
|
27 |
+
name='Chain type',
|
28 |
+
options=['stuff', 'map_reduce', "refine", "map_rerank"]
|
29 |
+
)
|
30 |
+
|
31 |
+
widgets = pn.Row(
|
32 |
+
pn.Column(prompt, run_button, margin=5),
|
33 |
+
pn.Card(
|
34 |
+
"Chain type:",
|
35 |
+
pn.Column(select_chain_type, select_k),
|
36 |
+
title="Advanced settings", margin=10
|
37 |
+
), width=600
|
38 |
+
)
|
39 |
+
|
40 |
+
|
41 |
+
def qa(file, query, chain_type, k):
|
42 |
+
loader = PyPDFLoader(file)
|
43 |
+
documents = loader.load()
|
44 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
45 |
+
texts = text_splitter.split_documents(documents)
|
46 |
+
embeddings = OpenAIEmbeddings()
|
47 |
+
vector_store = FAISS.from_documents(texts, embeddings)
|
48 |
+
retriever = vector_store.as_retriever(
|
49 |
+
search_type="similarity", search_kwargs={"k": k})
|
50 |
+
model = ChatOpenAI(model='gpt-3.5-turbo')
|
51 |
+
# qa = ConversationalRetrievalChain.from_llm(
|
52 |
+
# model, retriever=retriever, chain_type=chain_type)
|
53 |
+
qa = RetrievalQA.from_chain_type(
|
54 |
+
model, chain_type=chain_type, retriever=retriever, return_source_documents=False)
|
55 |
+
|
56 |
+
result = qa({"query": query})
|
57 |
+
# print(result['result'])
|
58 |
+
# return result['answer']
|
59 |
+
return result['result']
|
60 |
+
|
61 |
+
|
62 |
+
convos = [] # store all panel objects in a list
|
63 |
+
|
64 |
+
|
65 |
+
def temfile_create(file_input):
|
66 |
+
with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as temp_file:
|
67 |
+
temp_file.write(file_input.value)
|
68 |
+
temp_file.flush()
|
69 |
+
temp_file.seek(0)
|
70 |
+
# Do something with the temporary file here, such as passing the file path to another function
|
71 |
+
return temp_file.name
|
72 |
+
|
73 |
+
|
74 |
+
def qa_result(_):
|
75 |
+
os.environ["OPENAI_API_KEY"] = openaikey.value
|
76 |
+
|
77 |
+
if file_input.value is not None:
|
78 |
+
# file_input.save("/.cache/temp.pdf")
|
79 |
+
pdf_file = temfile_create(file_input)
|
80 |
+
prompt_text = prompt.value
|
81 |
+
|
82 |
+
if prompt_text:
|
83 |
+
result = qa(file=pdf_file, query=prompt_text,
|
84 |
+
chain_type=select_chain_type.value, k=select_k.value)
|
85 |
+
convos.extend([
|
86 |
+
pn.Row(
|
87 |
+
pn.panel("\U0001F60A", width=10),
|
88 |
+
prompt_text,
|
89 |
+
width=600
|
90 |
+
),
|
91 |
+
pn.Row(
|
92 |
+
pn.panel("\U0001F916", width=10),
|
93 |
+
# result['answer'],
|
94 |
+
result,
|
95 |
+
width=600
|
96 |
+
)
|
97 |
+
])
|
98 |
+
return pn.Column(*convos, margin=15, width=575, min_height=400)
|
99 |
+
|
100 |
+
|
101 |
+
qa_interactive = pn.panel(
|
102 |
+
pn.bind(qa_result, run_button),
|
103 |
+
loading_indicator=True,
|
104 |
+
)
|
105 |
+
|
106 |
+
output = pn.WidgetBox('*Output will show up here:*',
|
107 |
+
qa_interactive, width=630, scroll=True)
|
108 |
+
|
109 |
+
# layout
|
110 |
+
|
111 |
+
# try:
|
112 |
+
pn.Column(
|
113 |
+
pn.pane.Markdown("""
|
114 |
+
## \U0001F60A! Question Answering with your PDF file
|
115 |
+
|
116 |
+
Step 1: Upload a PDF file \n
|
117 |
+
Step 2: Enter your OpenAI API key. This costs $$. You will need to set up billing info at [OpenAI](https://platform.openai.com/account). \n
|
118 |
+
Step 3: Type your question at the bottom and click "Run" \n
|
119 |
+
|
120 |
+
"""),
|
121 |
+
pn.Row(file_input, openaikey),
|
122 |
+
output,
|
123 |
+
widgets
|
124 |
+
|
125 |
+
).servable()
|
126 |
+
# except Exception as ex:
|
127 |
+
# pass
|
requirements.txt
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
langchain==0.0.149
|
2 |
+
panel
|
3 |
+
openai
|
4 |
+
pypdf
|
5 |
+
tiktoken
|
6 |
+
notebook
|
7 |
+
faiss-cpu
|