-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinitialize.py
More file actions
53 lines (44 loc) · 1.7 KB
/
initialize.py
File metadata and controls
53 lines (44 loc) · 1.7 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
import os
import tempfile
from pathlib import Path
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from oauth2client.client import GoogleCredentials
from google.colab import auth
from code_searcher import CodeSearcher
def download_indices_from_gdrive(drive_folder_name="code_embeddings", local_folder="code_embeddings"):
if local_folder is None:
local_folder = tempfile.mkdtemp()
else:
os.makedirs(local_folder, exist_ok=True)
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
folder_list = drive.ListFile({
'q': f"title='{drive_folder_name}' and mimeType='application/vnd.google-apps.folder' and trashed=false"
}).GetList()
if not folder_list:
print(f"Folder '{drive_folder_name}' not found.")
return None
folder_id = folder_list[0]['id']
files = drive.ListFile({'q': f"'{folder_id}' in parents and trashed=false"}).GetList()
if not files:
print(f"No files in folder '{drive_folder_name}'.")
return None
for f in files:
f.GetContentFile(os.path.join(local_folder, f['title']))
print(f"Downloaded {f['title']}")
return local_folder
def initialize_searcher(folder_name="code_embeddings", quantize=True):
print("Initializing Code Searcher...")
index_dir = download_indices_from_gdrive(folder_name)
if not index_dir:
return None
try:
return CodeSearcher(index_dir, quantize=quantize)
except Exception:
if quantize:
print("Retrying without quantization...")
return CodeSearcher(index_dir, quantize=False)
return None