Spaces:
Runtime error
Runtime error
Commit
•
71dd084
1
Parent(s):
975790d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from PyPDF2 import PdfReader
|
3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
+
from langchain.text_splitter import CharacterTextSplitter
|
5 |
+
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
|
6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.chains.question_answering import load_qa_chain
|
8 |
+
import gradio as gr
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
def submitYourDocument(doc):
|
14 |
+
reader = PdfReader(doc)
|
15 |
+
# read data from the file and put them into a variable called raw_text
|
16 |
+
raw_text = ''
|
17 |
+
for i, page in enumerate(reader.pages):
|
18 |
+
text = page.extract_text()
|
19 |
+
if text:
|
20 |
+
raw_text += text
|
21 |
+
text_splitter = CharacterTextSplitter(
|
22 |
+
separator = "\n",
|
23 |
+
chunk_size = 1000,
|
24 |
+
chunk_overlap = 200,
|
25 |
+
length_function = len,
|
26 |
+
)
|
27 |
+
texts = text_splitter.split_text(raw_text)
|
28 |
+
return texts
|
29 |
+
|
30 |
+
|
31 |
+
def main(doc,prompt):
|
32 |
+
result=submitYourDocument(doc)
|
33 |
+
embeddings = HuggingFaceEmbeddings()
|
34 |
+
db = FAISS.from_texts(result, embeddings)
|
35 |
+
llm=HuggingFaceHub(repo_id="google/flan-t5-xxl",model_kwargs={"temperature":1, "max_length":512})
|
36 |
+
chain=load_qa_chain(llm,chain_type="stuff")
|
37 |
+
query =prompt
|
38 |
+
docs = db.similarity_search(query)
|
39 |
+
return chain.run(input_documents=docs, question=query)
|
40 |
+
|
41 |
+
interface=gr.Interface(fn=main,inputs=[gr.File(label="Upload file"),gr.components.Textbox(label="Type Question Related to Uploaded Document")],
|
42 |
+
outputs=gr.components.Textbox("Answer.."))
|
43 |
+
interface.launch(debug=True)
|
44 |
+
|
45 |
+
|