Chris Alexiuk
commited on
Commit
•
9adcdf6
1
Parent(s):
153fa6c
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,23 @@
|
|
1 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
2 |
-
from langchain.text_splitter import NLTKTextSplitter
|
3 |
from langchain.vectorstores import Chroma
|
4 |
-
from langchain.docstore.document import Document
|
5 |
-
from langchain.prompts import PromptTemplate
|
6 |
-
from langchain.indexes.vectorstore import VectorstoreIndexCreator
|
7 |
from langchain.chains.question_answering import load_qa_chain
|
8 |
from langchain.llms import OpenAI
|
9 |
-
import nltk
|
10 |
import os
|
11 |
|
12 |
-
nltk.download("punkt")
|
13 |
-
|
14 |
with open("guide1.txt") as f:
|
15 |
hitchhikersguide = f.read()
|
16 |
|
17 |
-
text_splitter =
|
18 |
texts = text_splitter.split_text(hitchhikersguide)
|
19 |
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
def make_inference(query
|
22 |
-
|
23 |
-
embeddings = OpenAIEmbeddings()
|
24 |
-
docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
|
25 |
-
chain = load_qa_chain(OpenAI(temperature=0), chain_type="refine")
|
26 |
-
docs = docsearch.get_relevant_documents(query)
|
27 |
-
return chain({"input_documents": docs, "question": query}, return_only_outputs=True)
|
28 |
|
29 |
if __name__ == "__main__":
|
30 |
# make a gradio interface
|
@@ -34,7 +27,6 @@ if __name__ == "__main__":
|
|
34 |
make_inference,
|
35 |
[
|
36 |
gr.inputs.Textbox(lines=2, label="Query"),
|
37 |
-
gr.inputs.Textbox(lines=2, label="OpenAI Key"),
|
38 |
],
|
39 |
gr.outputs.Textbox(label="Response"),
|
40 |
title="🗣️TalkToMyDoc📄",
|
|
|
1 |
from langchain.embeddings.openai import OpenAIEmbeddings
|
|
|
2 |
from langchain.vectorstores import Chroma
|
|
|
|
|
|
|
3 |
from langchain.chains.question_answering import load_qa_chain
|
4 |
from langchain.llms import OpenAI
|
|
|
5 |
import os
|
6 |
|
|
|
|
|
7 |
with open("guide1.txt") as f:
|
8 |
hitchhikersguide = f.read()
|
9 |
|
10 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0, separator = "\n")
|
11 |
texts = text_splitter.split_text(hitchhikersguide)
|
12 |
|
13 |
+
embeddings = OpenAIEmbeddings()
|
14 |
+
|
15 |
+
docsearch = Chroma.from_texts(texts, embeddings, metadatas=[{"source": str(i)} for i in range(len(texts))]).as_retriever()
|
16 |
+
|
17 |
+
chain = load_qa_chain(OpenAI(temperature=0), chain_type="stuff")
|
18 |
|
19 |
+
def make_inference(query):
|
20 |
+
chain.run(input_documents=docs, question=query)
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
if __name__ == "__main__":
|
23 |
# make a gradio interface
|
|
|
27 |
make_inference,
|
28 |
[
|
29 |
gr.inputs.Textbox(lines=2, label="Query"),
|
|
|
30 |
],
|
31 |
gr.outputs.Textbox(label="Response"),
|
32 |
title="🗣️TalkToMyDoc📄",
|