File size: 1,017 Bytes
364c74d
8c77b49
4a7f6e6
364c74d
 
 
4a7f6e6
 
 
8c77b49
364c74d
 
8c77b49
364c74d
 
 
4a7f6e6
 
 
 
 
8c77b49
4a7f6e6
 
364c74d
4a7f6e6
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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))