Spaces:
Build error
Build error
Update app.py
Browse filesadded a prompt to the chain
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import gradio as gr
|
|
| 3 |
from langchain.document_loaders import OnlinePDFLoader
|
| 4 |
|
| 5 |
from langchain.text_splitter import CharacterTextSplitter
|
|
|
|
|
|
|
| 6 |
text_splitter = CharacterTextSplitter(chunk_size=350, chunk_overlap=0)
|
| 7 |
|
| 8 |
from langchain.llms import HuggingFaceHub
|
|
@@ -22,8 +24,27 @@ def pdf_changes(pdf_doc):
|
|
| 22 |
texts = text_splitter.split_documents(documents)
|
| 23 |
db = Chroma.from_documents(texts, embeddings)
|
| 24 |
retriever = db.as_retriever()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
global qa
|
| 26 |
-
qa = RetrievalQA.from_chain_type(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
return "Ready"
|
| 28 |
|
| 29 |
def add_text(history, text):
|
|
|
|
| 3 |
from langchain.document_loaders import OnlinePDFLoader
|
| 4 |
|
| 5 |
from langchain.text_splitter import CharacterTextSplitter
|
| 6 |
+
from langchain.prompts import PromptTemplate
|
| 7 |
+
|
| 8 |
text_splitter = CharacterTextSplitter(chunk_size=350, chunk_overlap=0)
|
| 9 |
|
| 10 |
from langchain.llms import HuggingFaceHub
|
|
|
|
| 24 |
texts = text_splitter.split_documents(documents)
|
| 25 |
db = Chroma.from_documents(texts, embeddings)
|
| 26 |
retriever = db.as_retriever()
|
| 27 |
+
|
| 28 |
+
prompt_template = """You have been given a pdf or pdfs. You must search these pdfs.
|
| 29 |
+
If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
| 30 |
+
Only answer the question.
|
| 31 |
+
|
| 32 |
+
{context}
|
| 33 |
+
|
| 34 |
+
Question: {question}
|
| 35 |
+
Answer:"""
|
| 36 |
+
PROMPT = PromptTemplate(
|
| 37 |
+
template=prompt_template, input_variables=["context", "question"]
|
| 38 |
+
)
|
| 39 |
+
chain_type_kwargs = {"prompt": PROMPT}
|
| 40 |
global qa
|
| 41 |
+
qa = RetrievalQA.from_chain_type(
|
| 42 |
+
llm=flan_ul2,
|
| 43 |
+
chain_type="stuff",
|
| 44 |
+
retriever=retriever,
|
| 45 |
+
return_source_documents=True,
|
| 46 |
+
chain_type_kwargs=chain_type_kwargs,
|
| 47 |
+
)
|
| 48 |
return "Ready"
|
| 49 |
|
| 50 |
def add_text(history, text):
|