-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
220 lines (163 loc) · 7.04 KB
/
Copy pathapp.py
File metadata and controls
220 lines (163 loc) · 7.04 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from flask import Flask, request, jsonify, render_template
from flask_cors import CORS # Import CORS
import cloudinary
import cloudinary.api
import cloudinary.uploader
cloudinary.config(
cloud_name='ddynskqb7',
api_key='884368654531583',
api_secret='MpP5_H2XkHALtzhQpwm-YOHxnf8'
)
import clip
from PIL import Image
import os
import torch
import json
import urllib.parse
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}}) # Allow all origins
# CORS(app) # Apply CORS to the entire app
# Load the device
#device = "cuda" if torch.cuda.is_available() else "cpu"
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load the custom-trained CLIP model
model, preprocess = clip.load("ViT-B/32", device=device)
model.load_state_dict(torch.load('model.pth', map_location=device), strict=False)
model.to(device)
model.eval()
resources = cloudinary.api.resources(type='upload', prefix='your_folder_name', max_results=300)
image_urls = [resource['url'] for resource in resources['resources']]
# Rename Files
# def rename_files(directory):
# # List all files in the directory
# files = os.listdir(directory)
# # Sort files to ensure they are renamed in order
# files.sort()
# # Loop through all the files
# for index, filename in enumerate(files):
# # Get the file extension
# file_extension = os.path.splitext(filename)[1]
# # Construct the new file name with the prefix "img" followed by an increasing number
# new_filename = f"img{index + 1}{file_extension}"
# # Get the full path for the old and new file names
# old_file = os.path.join(directory, filename)
# new_file = os.path.join(directory, new_filename)
# # Rename the file
# os.rename(old_file, new_file)
# print(f"Renamed {len(files)} files.")
# directory_path = "./images/"
# rename_files(directory_path)
# Path to your images directory
image_dir = './images' # Assuming your images are in the "images" folder
# @app.get('/')
# def home():
# print(device)
# return render_template("index.html", image=0)
@app.route('/', methods=['GET'])
def get_data():
data = {'message': 'Hello from Flask!'}
return jsonify(data)
@app.route('/predict', methods=['POST'])
def predict():
# Get the prompt from the request body
data = request.json
prompt = data.get('prompt')
print(prompt)
print(device)
if not prompt:
return jsonify({"error": "No prompt provided"}), 400
# Encode the text prompt
text_features = model.encode_text(clip.tokenize(prompt).to(device))
text_features /= text_features.norm(dim=-1, keepdim=True)
# # Initialize variables to store the best match
# best_image = None
# best_similarity = -1
# Initialize a list to store image names and their similarity scores
image_scores = []
# # Iterate through images in the directory
# for image_name in os.listdir(image_dir):
# image_path = os.path.join(image_dir, image_name)
# # Load and preprocess the image
# image = Image.open(image_path)
# image_input = preprocess(image).unsqueeze(0).to(device)
# # Encode the image
# image_features = model.encode_image(image_input)
# image_features /= image_features.norm(dim=-1, keepdim=True)
# # Calculate similarity
# similarity = torch.matmul(text_features, image_features.T).item()
# # Check if this is the best match
# if similarity > best_similarity:
# best_similarity = similarity
# best_image = image_name
# Iterate through images in the directory
for image_name in os.listdir(image_dir):
image_path = os.path.join(image_dir, image_name)
# Load and preprocess the image
image = Image.open(image_path)
image_input = preprocess(image).unsqueeze(0).to(device)
# Encode the image
image_features = model.encode_image(image_input)
image_features /= image_features.norm(dim=-1, keepdim=True)
# Calculate similarity
similarity = torch.matmul(text_features, image_features.T).item()
# Append the image name and its similarity score to the list
image_scores.append((image_name, similarity))
# Sort the list by similarity scores in descending order
image_scores.sort(key=lambda x: x[1], reverse=True)
# Select the top 4 images
top_images = image_scores[:4]
# Create the response with the top 4 images and their similarity scores
if top_images:
torch.cuda.empty_cache()
result = [{"image_url": f"./images/{urllib.parse.quote(img)}", "similarity_score": score} for img, score in top_images]
return jsonify({"top_images": result}), 200
# # Return the best matching image and its similarity score
# if best_image:
# torch.cuda.empty_cache()
# # image_url = f"../../../../../images/{urllib.parse.quote(best_image)}"
# image_url = f"./images/{urllib.parse.quote(best_image)}"
# return jsonify({"best_image": image_url, "similarity_score": best_similarity}), 200
else:
torch.cuda.empty_cache()
return jsonify({"error": "No images found"}), 404
# @app.route('/predict/<prompt>', methods=['GET'])
# def predict(prompt):
# Get the prompt from the request
# print(prompt)
# print(device)
# if not prompt:
# return jsonify({"error": "No prompt provided"}), 400
# # Encode the text prompt
# text_features = model.encode_text(clip.tokenize(prompt).to(device))
# text_features /= text_features.norm(dim=-1, keepdim=True)
# # Initialize variables to store the best match
# best_image = None
# best_similarity = -1
# # Iterate through images in the directory
# for image_name in os.listdir(image_dir):
# image_path = os.path.join(image_dir, image_name)
# # Load and preprocess the image
# image = Image.open(image_path)
# image_input = preprocess(image).unsqueeze(0).to(device)
# # Encode the image
# image_features = model.encode_image(image_input)
# image_features /= image_features.norm(dim=-1, keepdim=True)
# # Calculate similarity
# similarity = torch.matmul(text_features, image_features.T).item()
# # Check if this is the best match
# if similarity > best_similarity:
# best_similarity = similarity
# best_image = image_name
# # Return the best matching image and its similarity score
# if best_image:
# torch.cuda.empty_cache()
# image_url = f"/images/{urllib.parse.quote(best_image)}"
# return jsonify({"best_image": image_url, "similarity_score": best_similarity}), 200
# # print(os.path.join(image_dir, best_image))
# # return render_template("index.html", image= "images/"+urllib.parse.quote(best_image))
# else:
# torch.cuda.empty_cache()
# return jsonify({"error": "No images found"}), 404
if __name__ == '__main__':
app.run(debug=True)
# collection in mdb -> array of obj, 2 things