File size: 7,264 Bytes
58a226a
 
 
 
61a66af
 
58a226a
61a66af
 
58a226a
61a66af
58a226a
 
 
61a66af
8c9f732
61a66af
58a226a
 
 
 
 
 
 
 
 
 
 
61a66af
58a226a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84aa20e
58a226a
84aa20e
 
58a226a
 
84aa20e
 
58a226a
cedfc32
 
567b5f3
 
 
 
58a226a
567b5f3
61a66af
cedfc32
 
 
 
 
567b5f3
2cb398d
cedfc32
567b5f3
cedfc32
 
567b5f3
cedfc32
567b5f3
 
 
58a226a
 
cedfc32
567b5f3
58a226a
 
 
 
 
 
61a66af
 
 
 
 
58a226a
 
 
 
 
 
 
 
 
bfe265d
 
 
 
 
 
 
34d6165
bfe265d
 
58a226a
 
61a66af
58a226a
 
 
61a66af
58a226a
 
 
bfe265d
58a226a
 
61a66af
fca7f7a
58a226a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bfe265d
 
58a226a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4f5a1cf
 
 
f5ae429
58a226a
4f5a1cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58a226a
 
 
4f5a1cf
58a226a
 
 
61a66af
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# docker build -t reward-simulator .docker run -p 7860:7860 -v $(pwd)/data:/app/data reward-simulator

from PIL import Image
import numpy as np
import io
import faiss

import requests
import torch

from request import get_ft, get_topk
from flickrapi import FlickrAPI

from flask import Flask, request, render_template, jsonify, send_from_directory
app = Flask(__name__)

PRESET_IMAGES = {
    1: "static/1.webp",
    2: "static/2.webp",
    3: "static/3.webp"
}

# Add Flickr configuration
FLICKR_API_KEY = '80ef21a6f7eb0984ea613c316a89ca69'
FLICKR_API_SECRET = '4d0e8ce6734f4b3f'
flickr = FlickrAPI(FLICKR_API_KEY, FLICKR_API_SECRET, format='parsed-json', store_token=False)

def get_photo_id(url):
    """Extract photo ID from Flickr URL"""
    try:
        return url.split('/')[-1].split('_')[0]
    except:
        return None

def get_other_info(url):
    """Get author information from Flickr"""
    try:
        photo_id = get_photo_id(url)
        if photo_id:
            photo_info = flickr.photos.getInfo(photo_id=photo_id)
            license = photo_info['photo']['license']
            owner = photo_info['photo']['owner']
            flickr_url = f"https://www.flickr.com/photos/{owner.get('nsid', '')}/{photo_id}"
            return {
                'username': owner.get('username', ''),
                'realname': owner.get('realname', ''),
                'nsid': owner.get('nsid', ''),
                'flickr_url': flickr_url,
                'license': license
            }
    except:
        pass
    return {
        'username': 'Unknown',
        'realname': 'Unknown',
        'nsid': '',
        'flickr_url': '',
        'license': 'Unknown'
    }


def load_model():
    """Load DINOv2 model once and cache it"""
    torch.hub.set_dir('static')
    model = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
    model.eval()
    model.to(torch.device('cuda' if torch.cuda.is_available() else 'cpu'))
    return model

def load_index(index_path):
    """Load FAISS index once and cache it"""
    return faiss.read_index(index_path)

def distance_to_similarity(distances, temp=1e-4):
    """Convert distance to similarity"""
    for ii in range(len(distances)):
        contribs = distances[ii].max() - distances[ii]
        contribs = contribs / temp
        sum_contribs = np.exp(contribs).sum()
        distances[ii] = np.exp(contribs) / sum_contribs
    return distances

import os

import os
from PIL import Image
import numpy as np

def calculate_rewards(subscription, num_generations, author_share, ro_share, num_users_k, similarities, num_authors=1800):
    """Calculate raw similarity (distance) between two static images"""
    
    try:
        if os.path.exists("static/1.webp") and os.path.exists("static/2.webp"):
            image1 = Image.open("static/1.webp")
            image2 = Image.open("static/2.webp")
            features1 = get_ft(model, image1)
            features2 = get_ft(model, image1)  # temporaire : remettre image2
            euclid = float(np.linalg.norm(features1 - features2))
        else:
            euclid = 0.0
    except Exception as e:
        print(f"Erreur lors du chargement des images : {e}")
        euclid = 0.0

    rewards = [{
        'raw_similarity': euclid
    }]
    return rewards



# Global variables for model and index
model = None
index = None
urls = None

def init_model():
    global model, index, urls
    model = load_model()
    index = load_index("data/openimages_index.bin")
    with open("data/openimages_urls.txt", "r") as f:
        urls = f.readlines()

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/static/<path:filename>')
def serve_static(filename):
    return send_from_directory('static', filename)

DEFAULT_PARAMS = {
    'subscription': 12,
    'num_generations': 60,
    'author_share': 5,
    'ro_share': 10,
    'num_users_k': 500,
    'num_neighbors': 10,
    'num_authors': 2000
}

@app.route('/select_preset/<int:preset_id>')
def select_preset(preset_id):
    if preset_id not in PRESET_IMAGES:
        return jsonify({'error': 'Invalid preset ID'}), 400
    
    try:
        image_path = PRESET_IMAGES[preset_id]
        image = Image.open(image_path).convert('RGB')
        
        # Use default parameters for presets
        params = DEFAULT_PARAMS.copy()
        
        # Get features and search
        features = get_ft(model, image)
        distances, indices = get_topk(index, features, topk=params['num_neighbors'])
        
        # Collect valid results first
        valid_results = []
        valid_similarities = []
        for i in range(params['num_neighbors']):
            image_url = urls[indices[0][i]].strip()
            try:
                response = requests.head(image_url)
                if response.status_code == 200:
                    valid_results.append({
                        'index': i,
                        'url': image_url
                    })
                    valid_similarities.append(distances[0][i])
            except requests.RequestException:
                continue
        
        # Renormalize similarities for valid results
        if valid_similarities:
            similarities = distance_to_similarity(np.array([valid_similarities]), temp=1e-5)
            
            # Calculate rewards with renormalized similarities
            rewards = calculate_rewards(
                params['subscription'],
                params['num_generations'],
                params['author_share'],
                params['ro_share'],
                params['num_users_k'],
                similarities,
                params['num_authors']
            )
            
            # Build final results
            results = []
            for i, result in enumerate(valid_results):
                other_info = get_other_info(result['url'])
                results.append({
                    'image_url': result['url'],
                    'rewards': rewards[i],
                    'other': other_info
                })
            
            return jsonify({'results': results})
        
    except Exception as e:
        return jsonify({'error': str(e)}), 500

@app.route('/process', methods=['POST'])
def process_images():
    if 'image1' not in request.files or 'image2' not in request.files:
        return jsonify({'error': 'Two images must be provided (image1 and image2)'}), 400

    try:
        # Charger les deux images
        image_file1 = request.files['image1']
        image1 = Image.open(io.BytesIO(image_file1.read())).convert('RGB')

        image_file2 = request.files['image2']
        image2 = Image.open(io.BytesIO(image_file2.read())).convert('RGB')

        # Extraire les features des deux images
        features1 = get_ft(model, image1)
        features2 = get_ft(model, image2)

        # Calculer la distance euclidienne entre les deux feature vectors
        distance = float(np.linalg.norm(features1 - features2))  # Convertir en float Python natif pour JSON

        # Retourner la distance
        return jsonify({'distance': distance})

    except Exception as e:
        return jsonify({'error': str(e)}), 500


if __name__ == '__main__':
    init_model()
    app.run(host='0.0.0.0', port=7860)