-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartNamer_gemini.py
85 lines (58 loc) · 2.28 KB
/
smartNamer_gemini.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
import os
llm = ChatGoogleGenerativeAI(
model="gemini-1.5-pro-latest",
google_api_key="api_key")
# Directory containing the PDFs
pdfs_dir = 'C:\\Users\\walte\\Desktop\\smartNamer\\pdfs'
origin_files = os.listdir(pdfs_dir)
i=0
for file in origin_files:
old_file_path = os.path.join(pdfs_dir, file)
new_file_path = os.path.join(pdfs_dir, f"{str(i)}.pdf")
print(new_file_path)
os.rename(old_file_path, new_file_path)
i+=1
files = os.listdir(pdfs_dir)
def ai_reader():
res = []
# Static question for LLM
user_input = 'Based on file provided, generate a file name in this foramt:[Year]_[Main Topic]_[Specific Focus]_[Geographic Area].pdf. Please do not give any response except for the file name.'
# Instruction for LLM to generate the response
rag_prompt = ChatPromptTemplate.from_messages([
("system", 'You are a helpful assistant. Use the following context when responding:\n\n{context}.'),
("human", "{question}")
])
# Organize LLM's response into structured output
output_parser = StrOutputParser()
rag_chain = rag_prompt | llm | StrOutputParser()
for f in files:
pdf_path = os.path.join(pdfs_dir, f)
loader = PyPDFLoader(file_path=pdf_path)
documents = loader.load_and_split()
# Summarize document content to avoid exceeding token limit
context = " ".join(page.page_content for page in documents)
if len(context) > 32800 : # Adjust this limit as needed
context = context[:32800 ]
# Initiate the LLM response
response = rag_chain.invoke({
"question": user_input,
"context": context
})
res.append(response.strip())
print(response)
return res
def rename():
names = ai_reader()
print(names)
for old_name,new_name in zip(files, names):
old_file_path = os.path.join(pdfs_dir, old_name)
new_file_path = os.path.join(pdfs_dir, new_name)
os.rename(old_file_path,new_file_path)
def main():
rename()
if __name__ == "__main__":
main()