Aaryan1314 commited on
Commit
38da3b0
·
verified ·
1 Parent(s): b16faa9

Update process.py

Browse files
Files changed (1) hide show
  1. process.py +27 -22
process.py CHANGED
@@ -1,37 +1,42 @@
1
  import pdfplumber
2
  from langchain.text_splitter import RecursiveCharacterTextSplitter
3
  from langchain.schema import Document
4
- from transformers import pipeline
5
- from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
6
 
7
- pipe = pipeline("text2text-generation", model="google/flan-t5-large")
 
 
8
 
 
9
 
10
- tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
11
- model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large",device_map = "auto",torch_type="auto")
12
  def extracted_text(file):
13
-
14
- with pdfplumber.open(file) as pdf:
15
- text = ""
16
- for page in pdf.pages:
17
- # text +=page.extract_text() +"\n"
18
- text = "\n".join(page.extract_text() for page in pdf.pages if page.extract_text())
19
  return text
20
 
 
21
  def chunks(text):
22
- # docs=None
23
  docs = [Document(page_content=text)]
24
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=512,chunk_overlap=50)
25
- chunked_docs = text_splitter.split_documents(text_splitter)
26
  return chunked_docs
27
- def answer_question_from_chunk(chunks,question):
28
- # result = pipe({
29
- #'context'=chunks,
30
- #'question':question
31
- # })
32
- # return result['answer']
33
- context = "\n".join([chunk.page_content for chunk in chunks]) # Combine chunk texts
34
  prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
35
 
 
36
  result = pipe(prompt, max_length=200, temperature=0.7)
37
- return result[0]["generated_text"].split("Answer:")[-1].strip()
 
 
 
 
1
  import pdfplumber
2
  from langchain.text_splitter import RecursiveCharacterTextSplitter
3
  from langchain.schema import Document
4
+ from langchain.document_loaders import TextLoader
5
+ from transformers import pipeline, AutoTokenizer, AutoModelForSeq2SeqLM
6
 
7
+ # Load the tokenizer and model
8
+ tokenizer = AutoTokenizer.from_pretrained("google/flan-t5-large")
9
+ model = AutoModelForSeq2SeqLM.from_pretrained("google/flan-t5-large", device_map="auto", torch_dtype="auto")
10
 
11
+ pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer, device_map="auto", torch_dtype="auto")
12
 
13
+ # Function to extract text from a PDF using LangChain
 
14
  def extracted_text(file):
15
+ # Convert PDF to text using langchain's text loading mechanism (via pdfplumber under the hood)
16
+ loader = TextLoader(file) # TextLoader can handle text extraction from various document formats
17
+ documents = loader.load() # This will extract and return the text from the PDF as Document objects
18
+ text = "\n".join([doc.page_content for doc in documents]) # Combine extracted content
 
 
19
  return text
20
 
21
+ # Function to chunk the extracted text
22
  def chunks(text):
23
+ # Prepare the document for chunking using LangChain's RecursiveCharacterTextSplitter
24
  docs = [Document(page_content=text)]
25
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=50)
26
+ chunked_docs = text_splitter.split_documents(docs) # Split the document into chunks
27
  return chunked_docs
28
+
29
+ # Function to answer a question based on the chunked documents
30
+ def answer_question_from_chunk(chunks, question):
31
+ # Combine chunk texts into one context
32
+ context = "\n".join([chunk.page_content for chunk in chunks])
33
+
34
+ # Create the prompt
35
  prompt = f"Context: {context}\n\nQuestion: {question}\n\nAnswer:"
36
 
37
+ # Use the pipeline to generate an answer
38
  result = pipe(prompt, max_length=200, temperature=0.7)
39
+
40
+ # Return the answer part of the generated result
41
+ return result[0]["generated_text"].split("Answer:")[-1].strip()
42
+