Spaces:
Sleeping
Sleeping
Commit
·
61a66af
1
Parent(s):
84aa20e
Update app.py
Browse files
app.py
CHANGED
@@ -2,22 +2,19 @@
|
|
2 |
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
-
import io
|
6 |
-
import faiss
|
7 |
|
8 |
-
import requests
|
9 |
-
import torch
|
10 |
|
11 |
-
from request import get_ft, get_topk
|
12 |
from flickrapi import FlickrAPI
|
13 |
|
14 |
from flask import Flask, request, render_template, jsonify, send_from_directory
|
15 |
-
|
16 |
-
#rend un fichier HTML (template) en y injectant des variables.
|
17 |
-
app = Flask(__name__) #crée l'objet de l'application web
|
18 |
|
19 |
-
|
20 |
-
PRESET_IMAGES = { #les images présélectionnées sont rangées sous /static
|
21 |
1: "static/1.webp",
|
22 |
2: "static/2.webp",
|
23 |
3: "static/3.webp"
|
@@ -29,7 +26,7 @@ FLICKR_API_SECRET = '4d0e8ce6734f4b3f'
|
|
29 |
flickr = FlickrAPI(FLICKR_API_KEY, FLICKR_API_SECRET, format='parsed-json', store_token=False)
|
30 |
|
31 |
def get_photo_id(url):
|
32 |
-
"""Extract photo ID from Flickr URL"""
|
33 |
try:
|
34 |
return url.split('/')[-1].split('_')[0]
|
35 |
except:
|
@@ -86,41 +83,45 @@ def distance_to_similarity(distances, temp=1e-4):
|
|
86 |
def calculate_rewards(subscription, num_generations, author_share, ro_share, num_users_k, similarities, num_authors=1800):
|
87 |
"""Calculate rewards based on user inputs and similarities"""
|
88 |
num_users = num_users_k * 1000
|
89 |
-
|
90 |
# Monthly revenue allocated to authors
|
91 |
authors_monthly_revenue = subscription * num_users * (author_share / 100)
|
92 |
-
|
93 |
rewards = []
|
94 |
for sim in similarities[0]:
|
95 |
-
# Attribution bonus based on similarity score
|
96 |
attribution_bonus = sim * len(similarities[0])
|
97 |
-
|
98 |
# Calculate monthly rewards
|
99 |
author_month_reward = (authors_monthly_revenue / num_authors) * attribution_bonus
|
100 |
ro_month_reward = author_month_reward / (author_share / 100) * (ro_share / 100)
|
101 |
-
|
102 |
rewards.append({
|
103 |
'paid_per_month': f"{subscription:.0f}€",
|
104 |
-
'attribution': f"{sim
|
105 |
'author_month_reward': f"{author_month_reward:.0f}€",
|
106 |
-
'ro_month_reward': f"{ro_month_reward:.0f}€"
|
107 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
})
|
109 |
-
|
110 |
return rewards
|
111 |
|
112 |
-
|
113 |
# Global variables for model and index
|
114 |
model = None
|
115 |
index = None
|
116 |
urls = None
|
117 |
|
118 |
def init_model():
|
119 |
-
global model, index, urls
|
120 |
-
model = load_model()
|
121 |
-
index = load_index("data/openimages_index.bin")
|
122 |
-
with open("data/openimages_urls.txt", "r") as f
|
123 |
-
urls = f.readlines()
|
124 |
|
125 |
@app.route('/')
|
126 |
def home():
|
@@ -142,44 +143,19 @@ DEFAULT_PARAMS = {
|
|
142 |
|
143 |
@app.route('/select_preset/<int:preset_id>')
|
144 |
def select_preset(preset_id):
|
145 |
-
if preset_id not in PRESET_IMAGES:
|
146 |
return jsonify({'error': 'Invalid preset ID'}), 400
|
147 |
|
148 |
try:
|
149 |
-
image_path = PRESET_IMAGES[preset_id]
|
150 |
image = Image.open(image_path).convert('RGB')
|
151 |
|
152 |
# Use default parameters for presets
|
153 |
params = DEFAULT_PARAMS.copy()
|
154 |
|
155 |
# Get features and search
|
156 |
-
features = get_ft(model, image)
|
157 |
distances, indices = get_topk(index, features, topk=params['num_neighbors'])
|
158 |
-
#utilise l’index pour trouver les k voisins de l’image
|
159 |
-
#retourne
|
160 |
-
# - distances avec les voisins
|
161 |
-
# - indices : les positions (dans l'index) des voisins
|
162 |
-
|
163 |
-
# Process image
|
164 |
-
#features = get_ft(model, image) ######## extrait le vecteur de l'image
|
165 |
-
#_, indices = get_topk(index, features, topk=params['num_neighbors']) ######## extrait les distances avec les premiers voisins
|
166 |
-
|
167 |
-
# Supposons que vous ayez une fonction pour charger une image
|
168 |
-
#def load_image(path):
|
169 |
-
# return Image.open(path).convert('RGB')
|
170 |
-
|
171 |
-
# Charger les deux images
|
172 |
-
#image1 = load_image(PRESET_IMAGES[1])
|
173 |
-
#image2 = load_image(PRESET_IMAGES[2])
|
174 |
-
|
175 |
-
# Extraire les features
|
176 |
-
#image1 = Image.open("static/1.webp")
|
177 |
-
#image2 = Image.open("static/2.webp")
|
178 |
-
#features1 = get_ft(model, image1)
|
179 |
-
#features2 = get_ft(model, image2)
|
180 |
-
|
181 |
-
# Calculer la distance euclidienne
|
182 |
-
#distances = np.linalg.norm(features1 - features2)
|
183 |
|
184 |
# Collect valid results first
|
185 |
valid_results = []
|
@@ -233,8 +209,8 @@ def process_image():
|
|
233 |
return jsonify({'error': 'No image provided'}), 400
|
234 |
|
235 |
try:
|
236 |
-
image_file = request.files['image']
|
237 |
-
image = Image.open(io.BytesIO(image_file.read())).convert('RGB')
|
238 |
|
239 |
# Use default parameters if none provided
|
240 |
params = DEFAULT_PARAMS.copy()
|
@@ -250,27 +226,9 @@ def process_image():
|
|
250 |
})
|
251 |
|
252 |
# Process image
|
253 |
-
features = get_ft(model, image)
|
254 |
-
distances, indices = get_topk(index, features, topk=params['num_neighbors'])
|
255 |
-
|
256 |
-
# Supposons que vous ayez une fonction pour charger une image
|
257 |
-
#def load_image(path):
|
258 |
-
# return Image.open(path).convert('RGB')
|
259 |
-
|
260 |
-
# Charger les deux images
|
261 |
-
#image1 = load_image(PRESET_IMAGES[1])
|
262 |
-
#image2 = load_image(PRESET_IMAGES[2])
|
263 |
-
|
264 |
-
# Extraire les features
|
265 |
-
#features1 = get_ft(model, image1)
|
266 |
-
#features2 = get_ft(model, image2)
|
267 |
-
|
268 |
-
# Calculer la distance euclidienne
|
269 |
-
#distances = np.linalg.norm(features1 - features2)
|
270 |
-
|
271 |
-
#print(f"Distance euclidienne entre l'image 1 et l'image 2 : {distances}")
|
272 |
|
273 |
-
|
274 |
# Collect valid results first
|
275 |
valid_results = []
|
276 |
valid_similarities = []
|
@@ -320,3 +278,12 @@ def process_image():
|
|
320 |
if __name__ == '__main__':
|
321 |
init_model()
|
322 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
+
import io
|
6 |
+
import faiss
|
7 |
|
8 |
+
import requests
|
9 |
+
import torch
|
10 |
|
11 |
+
from request import get_ft, get_topk
|
12 |
from flickrapi import FlickrAPI
|
13 |
|
14 |
from flask import Flask, request, render_template, jsonify, send_from_directory
|
15 |
+
app = Flask(__name__)
|
|
|
|
|
16 |
|
17 |
+
PRESET_IMAGES = {
|
|
|
18 |
1: "static/1.webp",
|
19 |
2: "static/2.webp",
|
20 |
3: "static/3.webp"
|
|
|
26 |
flickr = FlickrAPI(FLICKR_API_KEY, FLICKR_API_SECRET, format='parsed-json', store_token=False)
|
27 |
|
28 |
def get_photo_id(url):
|
29 |
+
"""Extract photo ID from Flickr URL"""
|
30 |
try:
|
31 |
return url.split('/')[-1].split('_')[0]
|
32 |
except:
|
|
|
83 |
def calculate_rewards(subscription, num_generations, author_share, ro_share, num_users_k, similarities, num_authors=1800):
|
84 |
"""Calculate rewards based on user inputs and similarities"""
|
85 |
num_users = num_users_k * 1000
|
86 |
+
|
87 |
# Monthly revenue allocated to authors
|
88 |
authors_monthly_revenue = subscription * num_users * (author_share / 100)
|
89 |
+
|
90 |
rewards = []
|
91 |
for sim in similarities[0]:
|
92 |
+
# Attribution bonus based on similarity score and number of neighbors
|
93 |
attribution_bonus = sim * len(similarities[0])
|
94 |
+
|
95 |
# Calculate monthly rewards
|
96 |
author_month_reward = (authors_monthly_revenue / num_authors) * attribution_bonus
|
97 |
ro_month_reward = author_month_reward / (author_share / 100) * (ro_share / 100)
|
98 |
+
|
99 |
rewards.append({
|
100 |
'paid_per_month': f"{subscription:.0f}€",
|
101 |
+
'attribution': f"{sim*100:.0f}%",
|
102 |
'author_month_reward': f"{author_month_reward:.0f}€",
|
103 |
+
'ro_month_reward': f"{ro_month_reward:.0f}€"
|
104 |
+
# 'paid_per_month': f"{subscription:.0f}€",
|
105 |
+
# 'paid_per_gen': f"{paid_per_gen:.2f}€",
|
106 |
+
# 'aro_share': f"{aro_share:.2f}c€",
|
107 |
+
# 'attribution': f"{sim*100:.0f}%",
|
108 |
+
# 'training_data_reward': f"{training_data_reward:.2f}c€",
|
109 |
+
# 'author_month_reward': f"{author_month_reward:.0f}€",
|
110 |
+
# 'ro_month_reward': f"{ro_month_reward:.0f}€"
|
111 |
})
|
|
|
112 |
return rewards
|
113 |
|
|
|
114 |
# Global variables for model and index
|
115 |
model = None
|
116 |
index = None
|
117 |
urls = None
|
118 |
|
119 |
def init_model():
|
120 |
+
global model, index, urls
|
121 |
+
model = load_model()
|
122 |
+
index = load_index("data/openimages_index.bin")
|
123 |
+
with open("data/openimages_urls.txt", "r") as f:
|
124 |
+
urls = f.readlines()
|
125 |
|
126 |
@app.route('/')
|
127 |
def home():
|
|
|
143 |
|
144 |
@app.route('/select_preset/<int:preset_id>')
|
145 |
def select_preset(preset_id):
|
146 |
+
if preset_id not in PRESET_IMAGES:
|
147 |
return jsonify({'error': 'Invalid preset ID'}), 400
|
148 |
|
149 |
try:
|
150 |
+
image_path = PRESET_IMAGES[preset_id]
|
151 |
image = Image.open(image_path).convert('RGB')
|
152 |
|
153 |
# Use default parameters for presets
|
154 |
params = DEFAULT_PARAMS.copy()
|
155 |
|
156 |
# Get features and search
|
157 |
+
features = get_ft(model, image)
|
158 |
distances, indices = get_topk(index, features, topk=params['num_neighbors'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
# Collect valid results first
|
161 |
valid_results = []
|
|
|
209 |
return jsonify({'error': 'No image provided'}), 400
|
210 |
|
211 |
try:
|
212 |
+
image_file = request.files['image']
|
213 |
+
image = Image.open(io.BytesIO(image_file.read())).convert('RGB')
|
214 |
|
215 |
# Use default parameters if none provided
|
216 |
params = DEFAULT_PARAMS.copy()
|
|
|
226 |
})
|
227 |
|
228 |
# Process image
|
229 |
+
features = get_ft(model, image)
|
230 |
+
distances, indices = get_topk(index, features, topk=params['num_neighbors'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
|
|
|
232 |
# Collect valid results first
|
233 |
valid_results = []
|
234 |
valid_similarities = []
|
|
|
278 |
if __name__ == '__main__':
|
279 |
init_model()
|
280 |
app.run(host='0.0.0.0', port=7860)
|
281 |
+
|
282 |
+
# Extraire les features
|
283 |
+
#image1 = Image.open("static/1.webp")
|
284 |
+
#image2 = Image.open("static/2.webp")
|
285 |
+
#features1 = get_ft(model, image1)
|
286 |
+
#features2 = get_ft(model, image2)
|
287 |
+
|
288 |
+
# Calculer la distance euclidienne
|
289 |
+
#distances = np.linalg.norm(features1 - features2)
|