-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (140 loc) · 6.48 KB
/
main.py
File metadata and controls
151 lines (140 loc) · 6.48 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import os
import sys
import time
import typer
from leader.leader import Leader
from leader.leader_adaptive import LeaderAdaptive
from follower.follower import Follower
app = typer.Typer(help='MetaFusion distributed photo system CLI')
@app.command()
def leader(
host: str = typer.Option('localhost', help='Leader IP address'),
port: int = typer.Option(8000, help='Leader port'),
base_dir: str = typer.Option('state/',
help='Follower vector index base directory'),
model_name: str = typer.Option('ViT-B/32',
help='Follower image embedding model name'),
device: str = typer.Option('cpu',
help='Follower image embedding model device'),
normalize: bool = typer.Option(True,
help='Follower image embedding normalization'),
adaptive: bool = typer.Option(False,
help='Adaptive metadata-vector fusion')
):
"""Start the leader node."""
if adaptive:
leader_node = LeaderAdaptive(host, port, base_dir, model_name, device, normalize)
else:
leader_node = Leader(host, port, base_dir, model_name, device, normalize)
while True:
print('Enter command')
try:
line = input("> ").strip()
if not line:
continue
parts = line.split(maxsplit=1)
cmd = parts[0]
arg = parts[1] if len(parts) > 1 else ""
match cmd:
case 'ls':
leader_node.list_member()
case 'ls_num_photo':
leader_node.list_num_photo()
case 'set_missing_rate':
try:
arg = float(arg)
except ValueError:
print('Usage: set_missing_rate <missing rate>')
continue
leader_node.set_metadata_missing_rate(arg)
case 'reset_missing_rate':
leader_node.reset_metadata_missing_rate()
case 'upload':
if not arg:
print('Usage: upload <image path>')
continue
leader_node.upload(arg)
case 'mass_upload':
if not arg:
print('Usage: upload <image directory>')
leader_node.mass_upload(arg)
case 'upload_from_msgpack':
pass
# leader_node.upload_from_msgpack(arg)
case 'upload_from_sqlite':
if not arg:
leader_node.upload_from_sqlite()
else:
args = arg.split()
if len(args) != 2:
print('Usage: upload_from_sqlite <db path> <photo table name>')
continue
leader_node.upload_from_sqlite(db_path=args[0], photo_table=args[1])
case 'clear':
leader_node.clear()
case 'search':
if not arg:
print("Usage: search <natural language prompt>")
continue
leader_node.search(arg, search_mode='meta_fusion')
case 'mass_search':
if arg:
args = arg.split()
if len(args) == 3:
leader_node.mass_search(search_mode=args[0], prompt_file_path=args[1], gt_file_path=args[2])
continue
print("Usage: mass_search <search mode> <prompt file path> <ground truth file path>")
case 'search_metadata':
if not arg:
print("Usage: search_metadata <natural language prompt>")
continue
leader_node.search(arg, search_mode='metadata_only')
case 'search_vector':
if not arg:
print("Usage: search_vector <natural language prompt>")
continue
leader_node.search(arg, search_mode='vector_only')
case 'get':
parts = arg.split(maxsplit=1)
if len(parts) <= 1:
print('Usage: get <output directory> <natural language prompt>')
continue
leader_node.search(parts[1], parts[0], search_mode='meta_fusion')
case 'help':
print("\n可用命令:")
print(" ls - 列出所有follower节点")
print(" upload <path> - 上传单张图片")
print(" mass_upload <dir> - 批量上传图片目录")
print(" clear - 清空所有数据")
print(" search <prompt> - MetaFusion搜索 (默认)")
print(" search_metadata <prompt> - 仅元数据搜索")
print(" search_vector <prompt> - 仅向量搜索")
print(" compare <prompt> - 比较三种搜索方法")
print(" get <dir> <prompt> - 搜索并下载图片")
print(" help - 显示帮助信息")
print(" exit/quit - 退出程序\n")
case 'exit' | 'quit':
print("Bye.")
leader_node.quit()
break
case _:
print(f"Unknown command: {cmd}. Type 'help' for available commands.")
except (KeyboardInterrupt, EOFError):
print("\nBye.")
leader_node.quit()
break
time.sleep(0.5)
@app.command()
def follower(
host: str = typer.Option('localhost', help='Follower IP address'),
port: int = typer.Option(9000, help='Follower port'),
leader_host: str = typer.Option('localhost', help='Leader IP address'),
leader_port: int = typer.Option(8000, help='Leader port'),
):
"""Start a follower node."""
follower_node = Follower(host, port)
follower_node.register(leader_host, leader_port)
if __name__ == "__main__":
if sys.platform == "darwin":
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
app()