johnmuchiri commited on
Commit
e53ab56
·
1 Parent(s): 17a4fcf

Add application file

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +124 -0
  3. requirements.txt +7 -0
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/app.py", "--address", "0.0.0.0", "--port", "7860", "--allow-websocket-origin", "johnmuchiri-Attorneychatke.hf.space", "--allow-websocket-origin", "0.0.0.0:7860"]
12
+
13
+ RUN mkdir /.cache
14
+ RUN chmod 777 /.cache
15
+ RUN mkdir .chroma
16
+ RUN chmod 777 .chroma
app.py ADDED
@@ -0,0 +1,124 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain.chains import RetrievalQA
3
+ from langchain.llms import OpenAI
4
+ from langchain.document_loaders import TextLoader
5
+ from langchain.document_loaders import PyPDFLoader
6
+ from langchain.indexes import VectorstoreIndexCreator
7
+ from langchain.text_splitter import CharacterTextSplitter
8
+ from langchain.embeddings import OpenAIEmbeddings
9
+ from langchain.vectorstores import Chroma
10
+ import panel as pn
11
+ import tempfile
12
+
13
+
14
+ pn.extension('texteditor', template="bootstrap", sizing_mode='stretch_width')
15
+ pn.state.template.param.update(
16
+ main_max_width="690px",
17
+ header_background="#F08080",
18
+ )
19
+
20
+ file_input = pn.widgets.FileInput(width=300)
21
+
22
+ openaikey = pn.widgets.PasswordInput(
23
+ value="", placeholder="Enter your OpenAI API Key here...", width=300
24
+ )
25
+ prompt = pn.widgets.TextEditor(
26
+ value="", placeholder="Enter your questions here...", height=160, toolbar=False
27
+ )
28
+ run_button = pn.widgets.Button(name="Run!")
29
+
30
+ select_k = pn.widgets.IntSlider(
31
+ name="Number of relevant chunks", start=1, end=5, step=1, value=2
32
+ )
33
+ select_chain_type = pn.widgets.RadioButtonGroup(
34
+ name='Chain type',
35
+ options=['stuff', 'map_reduce', "refine", "map_rerank"]
36
+ )
37
+
38
+ widgets = pn.Row(
39
+ pn.Column(prompt, run_button, margin=5),
40
+ pn.Card(
41
+ "Chain type:",
42
+ pn.Column(select_chain_type, select_k),
43
+ title="Advanced settings", margin=10
44
+ ), width=600
45
+ )
46
+
47
+
48
+ def qa(file, query, chain_type, k):
49
+ # load document
50
+ loader = PyPDFLoader(file)
51
+ documents = loader.load()
52
+ # split the documents into chunks
53
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
54
+ texts = text_splitter.split_documents(documents)
55
+ # select which embeddings we want to use
56
+ embeddings = OpenAIEmbeddings()
57
+ # create the vectorestore to use as the index
58
+ db = Chroma.from_documents(texts, embeddings)
59
+ # expose this index in a retriever interface
60
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": k})
61
+ # create a chain to answer questions
62
+ qa = RetrievalQA.from_chain_type(
63
+ llm=OpenAI(), chain_type=chain_type, retriever=retriever, return_source_documents=True)
64
+ result = qa({"query": query})
65
+ print(result['result'])
66
+ return result
67
+
68
+
69
+ convos = [] # store all panel objects in a list
70
+
71
+
72
+ def qa_result(_):
73
+ os.environ["OPENAI_API_KEY"] = openaikey.value
74
+
75
+ # save pdf file to a temp file
76
+ if file_input.value is not None:
77
+ file_input.save("/.cache/temp.pdf")
78
+
79
+ prompt_text = prompt.value
80
+ if prompt_text:
81
+ result = qa(file="/.cache/temp.pdf", query=prompt_text, chain_type=select_chain_type.value,
82
+ k=select_k.value)
83
+ convos.extend([
84
+ pn.Row(
85
+ pn.panel("\U0001F60A", width=10),
86
+ prompt_text,
87
+ width=600
88
+ ),
89
+ pn.Row(
90
+ pn.panel("\U0001F916", width=10),
91
+ pn.Column(
92
+ result["result"],
93
+ "Relevant source text:",
94
+ pn.pane.Markdown(
95
+ '\n--------------------------------------------------------------------\n'.join(
96
+ doc.page_content for doc in result["source_documents"]))
97
+ )
98
+ )
99
+ ])
100
+ # return convos
101
+ return pn.Column(*convos, margin=15, width=575, min_height=400)
102
+
103
+
104
+
105
+ qa_interactive = pn.panel(
106
+ pn.bind(qa_result, run_button),
107
+ loading_indicator=True,
108
+ )
109
+
110
+ output = pn.WidgetBox('*Output will show up here:*', qa_interactive, width=630, scroll=True)
111
+
112
+ # layout
113
+ pn.Column(
114
+ pn.pane.Markdown("""
115
+ ## \U0001F60A! Question Answering with your PDF file
116
+
117
+ 1) Upload a PDF. 2) Enter OpenAI API key. This costs $. Set up billing at [OpenAI](https://platform.openai.com/account). 3) Type a question and click "Run".
118
+
119
+ """),
120
+ pn.Row(file_input, openaikey),
121
+ output,
122
+ widgets
123
+
124
+ ).servable()
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ langchain
2
+ openai
3
+ chromadb
4
+ pypdf
5
+ tiktoken
6
+ panel
7
+ notebook