-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_sref.py
More file actions
178 lines (146 loc) · 5.77 KB
/
search_sref.py
File metadata and controls
178 lines (146 loc) · 5.77 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env python3
"""
SREF Style Search Interface
Interactive search tool for finding SREF styles based on text descriptions.
Uses the pre-computed embeddings and search index from analyze_sref_styles.py
Usage:
python3 search_sref.py
"""
import json
import os
import numpy as np
import torch
from transformers import CLIPProcessor, CLIPModel
from sklearn.metrics.pairwise import cosine_similarity
# Configuration
SREF_ANALYSIS_DIR = "sref_analysis"
SEARCH_INDEX_FILE = "sref_search_index.json"
OUTPUT_DIR = "output_test"
# Load models
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
print(f"Loading models on {DEVICE}...")
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
clip_model.to(DEVICE)
def load_search_index():
"""Load the search index from file."""
index_path = os.path.join(SREF_ANALYSIS_DIR, SEARCH_INDEX_FILE)
if not os.path.exists(index_path):
raise FileNotFoundError(f"Search index not found: {index_path}")
with open(index_path, 'r') as f:
data = json.load(f)
# Convert embeddings back to numpy arrays
search_index = {}
for sref_code, info in data.items():
search_index[sref_code] = {
'embedding': np.array(info['embedding']),
'summary': info['summary'],
'image_count': info['image_count'],
'combined_captions': info['combined_captions']
}
return search_index
def search_sref_styles(query_text, search_index, top_k=10):
"""Search for SREF styles based on text query."""
if not search_index:
print("No search index available")
return []
# Generate embedding for query text
text_inputs = clip_processor(text=[query_text], return_tensors="pt", padding=True, truncation=True).to(DEVICE)
with torch.no_grad():
text_features = clip_model.get_text_features(**text_inputs)
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
query_embedding = text_features.cpu().numpy().flatten()
# Calculate similarities
similarities = []
for sref_code, data in search_index.items():
similarity = cosine_similarity([query_embedding], [data['embedding']])[0][0]
similarities.append({
'sref_code': sref_code,
'similarity': similarity,
'summary': data['summary'],
'image_count': data['image_count'],
'combined_captions': data['combined_captions']
})
# Sort by similarity
similarities.sort(key=lambda x: x['similarity'], reverse=True)
return similarities[:top_k]
def display_results(results, query):
"""Display search results in a formatted way."""
print(f"\n{'='*60}")
print(f"SEARCH RESULTS FOR: '{query}'")
print(f"{'='*60}")
if not results:
print("No results found.")
return
for i, result in enumerate(results, 1):
print(f"\n{i}. SREF {result['sref_code']}")
print(f" Similarity: {result['similarity']:.3f}")
print(f" Images: {result['image_count']}")
print(f" Summary: {result['summary']}")
# Show first few words of combined captions
caption_preview = result['combined_captions'][:100] + "..." if len(result['combined_captions']) > 100 else result['combined_captions']
print(f" Captions: {caption_preview}")
def interactive_search():
"""Interactive search interface."""
print("Loading SREF search index...")
try:
search_index = load_search_index()
print(f"Loaded {len(search_index)} SREF styles")
except FileNotFoundError as e:
print(f"Error: {e}")
print("Please run analyze_sref_styles.py first to create the search index.")
return
print(f"\n{'='*60}")
print("SREF STYLE SEARCH INTERFACE")
print(f"{'='*60}")
print("Enter text descriptions to find matching SREF styles.")
print("Examples:")
print(" - 'abstract art'")
print(" - 'photorealistic portrait'")
print(" - 'geometric design'")
print(" - 'nature landscape'")
print(" - 'minimalist style'")
print(" - 'colorful painting'")
print(" - 'dark moody atmosphere'")
print("\nType 'quit' to exit.")
print(f"{'='*60}")
while True:
try:
query = input("\nEnter your search query: ").strip()
if query.lower() in ['quit', 'exit', 'q']:
print("Goodbye!")
break
if not query:
print("Please enter a search query.")
continue
print(f"\nSearching for: '{query}'...")
results = search_sref_styles(query, search_index, top_k=5)
display_results(results, query)
except KeyboardInterrupt:
print("\nGoodbye!")
break
except Exception as e:
print(f"Error during search: {e}")
def batch_search(queries):
"""Search for multiple queries at once."""
print("Loading SREF search index...")
try:
search_index = load_search_index()
print(f"Loaded {len(search_index)} SREF styles")
except FileNotFoundError as e:
print(f"Error: {e}")
print("Please run analyze_sref_styles.py first to create the search index.")
return
for query in queries:
print(f"\nSearching for: '{query}'...")
results = search_sref_styles(query, search_index, top_k=3)
display_results(results, query)
if __name__ == "__main__":
import sys
if len(sys.argv) > 1:
# Batch mode - search for provided queries
queries = sys.argv[1:]
batch_search(queries)
else:
# Interactive mode
interactive_search()