RAG-based-chatbot / PDF_Reader.py
adi-123's picture
Update PDF_Reader.py
8c77b49 verified
raw
history blame contribute delete
No virus
1.02 kB
import os
from together as Together
import PyPDF2
client = Together(api_key=os.environ.get("TOGETHER_API_KEY"))
def process_text(question, pdf_paths):
texts = [get_pdf_content(pdf) for pdf in pdf_paths]
combined_text = "\n".join(texts)
full_question = f"{question}\n\n{combined_text}" # Append combined PDF contents to the question
response = client.chat.completions.create(
model="meta-llama/Llama-3-8b-chat-hf",
messages=[{"role": "user", "content": full_question}],
)
return response.choices[0].message.content
def get_pdf_content(pdf_path):
with open(pdf_path, "rb") as file:
reader = PyPDF2.PdfReader(file)
text = ""
for page in reader.pages:
text += page.extract_text() if page.extract_text() else ""
return text
if __name__ == "__main__":
pdf_paths = ["doc1.pdf", "doc2.pdf"] # Adjust paths as necessary
question = "What can I learn about New York from these texts?"
print(process_text(question, pdf_paths))