-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
116 lines (97 loc) · 3.69 KB
/
Copy pathutils.py
File metadata and controls
116 lines (97 loc) · 3.69 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import os
import time
import shutil
import google.generativeai as genai
from pathlib import Path
def configure_genai(api_key):
"""Configures the Gemini API with the provided key."""
if not api_key:
return False, "API Key is missing."
try:
genai.configure(api_key=api_key)
return True, "Success"
except Exception as e:
return False, str(e)
def get_ai_filename(file_path, model_name):
"""Uploads file to Gemini and asks for a descriptive filename."""
try:
model = genai.GenerativeModel(model_name)
# Upload file
sample_file = genai.upload_file(path=file_path, display_name="User Document")
# Wait for processing
while sample_file.state.name == "PROCESSING":
time.sleep(1)
sample_file = genai.get_file(sample_file.name)
prompt = (
"Analyze this document. Output ONLY a short, descriptive filename "
"using snake_case (like: Data_Engineering_SQL_Basics). "
"Do not include the file extension. Do not use spaces. "
"If you cannot determine a name, return 'Unknown_Document'."
)
response = model.generate_content([sample_file, prompt])
# Cleanup
try:
genai.delete_file(sample_file.name)
except:
pass
return response.text.strip()
except Exception as e:
return None
def get_category(filename, model_name):
"""Asks Gemini to categorize the filename."""
try:
model = genai.GenerativeModel(model_name)
prompt = (
f"Categorize this file based on its name: '{filename}'. "
"Return ONLY a single, short category name (e.g., 'SQL', 'Python', 'Finance', 'Personal'). "
"Do not use special characters or spaces. "
"If unclear, return 'Uncategorized'."
)
response = model.generate_content(prompt)
return response.text.strip()
except Exception as e:
return "Uncategorized"
def clean_filename(text):
"""Sanitizes the filename."""
if not text:
return "Untitled"
# Keep only safe chars
safe_text = "".join([c if c.isalnum() or c in "-_" else "_" for c in text])
# Remove duplicates
while "__" in safe_text:
safe_text = safe_text.replace("__", "_")
return safe_text.strip("_")[:60]
def safe_rename(old_path, new_name_base):
"""Renames a file safely, handling duplicates."""
folder = os.path.dirname(old_path)
ext = os.path.splitext(old_path)[1]
clean_name = clean_filename(new_name_base)
new_filename = f"{clean_name}{ext}"
new_path = os.path.join(folder, new_filename)
counter = 1
while os.path.exists(new_path):
new_filename = f"{clean_name}_{counter}{ext}"
new_path = os.path.join(folder, new_filename)
counter += 1
try:
os.rename(old_path, new_path)
return True, new_filename
except Exception as e:
return False, str(e)
def safe_move(file_path, category):
"""Moves a file to a category folder safely."""
folder = os.path.dirname(file_path)
filename = os.path.basename(file_path)
# Sanitize category
category = "".join([c for c in category if c.isalnum() or c in "_-"])
if not category:
category = "Uncategorized"
dest_folder = os.path.join(folder, category)
dest_path = os.path.join(dest_folder, filename)
try:
if not os.path.exists(dest_folder):
os.makedirs(dest_folder)
shutil.move(file_path, dest_path)
return True, f"{category}/{filename}"
except Exception as e:
return False, str(e)