Spaces:
Sleeping
Sleeping
Commit
·
f5ae429
1
Parent(s):
a685300
Update app.py
Browse filesla version précédente calcule et affiche correctement la distance entre static1 et static2.
Cette version neutralise avec trois ### certaines lignes superflues, à vérifier
app.py
CHANGED
@@ -84,14 +84,14 @@ import os
|
|
84 |
|
85 |
def calculate_rewards(subscription, num_generations, author_share, ro_share, num_users_k, similarities, num_authors=1800):
|
86 |
"""Calculate rewards based on user inputs and similarities"""
|
87 |
-
num_users = num_users_k * 1000
|
88 |
|
89 |
try:
|
90 |
if os.path.exists("static/1.webp") and os.path.exists("static/2.webp"):
|
91 |
image1 = Image.open("static/1.webp")
|
92 |
image2 = Image.open("static/2.webp")
|
93 |
features1 = get_ft(model, image1)
|
94 |
-
features2 = get_ft(model, image2
|
95 |
euclid = float(np.linalg.norm(features1 - features2))
|
96 |
else:
|
97 |
euclid = 0 # pas d'image, euclidienne à 0
|
@@ -100,28 +100,28 @@ def calculate_rewards(subscription, num_generations, author_share, ro_share, num
|
|
100 |
euclid = 0 # sécurité
|
101 |
|
102 |
# Monthly revenue allocated to authors
|
103 |
-
authors_monthly_revenue = subscription * num_users * (author_share / 100)
|
104 |
|
105 |
rewards = []
|
106 |
-
for sim in similarities[0]:
|
107 |
# Attribution bonus based on similarity score and number of neighbors
|
108 |
-
attribution_bonus = sim * len(similarities[0])
|
109 |
|
110 |
# Calculate monthly rewards
|
111 |
-
author_month_reward = (authors_monthly_revenue / num_authors) * attribution_bonus
|
112 |
-
ro_month_reward = author_month_reward / (author_share / 100) * (ro_share / 100)
|
113 |
|
114 |
-
try:
|
115 |
-
|
116 |
-
except Exception as e:
|
117 |
-
|
118 |
-
|
119 |
|
120 |
rewards.append({
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
'raw_similarity': euclid
|
126 |
})
|
127 |
return rewards
|
@@ -223,60 +223,62 @@ def select_preset(preset_id):
|
|
223 |
def process_image():
|
224 |
if 'image' not in request.files:
|
225 |
return jsonify({'error': 'No image provided'}), 400
|
226 |
-
|
|
|
227 |
try:
|
228 |
-
image_file = request.files['image']
|
229 |
image = Image.open(io.BytesIO(image_file.read())).convert('RGB')
|
230 |
|
231 |
# Use default parameters if none provided
|
232 |
-
params = DEFAULT_PARAMS.copy()
|
233 |
-
if request.form:
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
|
244 |
# Process image
|
245 |
features = get_ft(model, image)
|
246 |
distances, indices = get_topk(index, features, topk=params['num_neighbors'])
|
247 |
|
248 |
# Collect valid results first
|
249 |
-
|
250 |
-
valid_similarities = []
|
251 |
-
for i in range(params['num_neighbors']):
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
|
264 |
# Renormalize similarities for valid results
|
265 |
-
if valid_similarities:
|
266 |
-
|
267 |
|
268 |
# Calculate rewards with renormalized similarities
|
269 |
-
rewards = calculate_rewards(
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
)
|
278 |
|
279 |
# Build final results
|
|
|
280 |
results = []
|
281 |
for i, result in enumerate(valid_results):
|
282 |
other_info = get_other_info(result['url'])
|
|
|
84 |
|
85 |
def calculate_rewards(subscription, num_generations, author_share, ro_share, num_users_k, similarities, num_authors=1800):
|
86 |
"""Calculate rewards based on user inputs and similarities"""
|
87 |
+
###num_users = num_users_k * 1000
|
88 |
|
89 |
try:
|
90 |
if os.path.exists("static/1.webp") and os.path.exists("static/2.webp"):
|
91 |
image1 = Image.open("static/1.webp")
|
92 |
image2 = Image.open("static/2.webp")
|
93 |
features1 = get_ft(model, image1)
|
94 |
+
features2 = get_ft(model, image1) #temporaire : remettre image2
|
95 |
euclid = float(np.linalg.norm(features1 - features2))
|
96 |
else:
|
97 |
euclid = 0 # pas d'image, euclidienne à 0
|
|
|
100 |
euclid = 0 # sécurité
|
101 |
|
102 |
# Monthly revenue allocated to authors
|
103 |
+
###authors_monthly_revenue = subscription * num_users * (author_share / 100)
|
104 |
|
105 |
rewards = []
|
106 |
+
###for sim in similarities[0]:
|
107 |
# Attribution bonus based on similarity score and number of neighbors
|
108 |
+
###attribution_bonus = sim * len(similarities[0])
|
109 |
|
110 |
# Calculate monthly rewards
|
111 |
+
###author_month_reward = (authors_monthly_revenue / num_authors) * attribution_bonus
|
112 |
+
###ro_month_reward = author_month_reward / (author_share / 100) * (ro_share / 100)
|
113 |
|
114 |
+
###try:
|
115 |
+
### sim_value = float(sim.item()) if hasattr(sim, "item") else float(sim)
|
116 |
+
###except Exception as e:
|
117 |
+
### print("Erreur de conversion de sim:", e)
|
118 |
+
### sim_value = 0.0 # valeur par défaut en cas d'erreur
|
119 |
|
120 |
rewards.append({
|
121 |
+
### 'paid_per_month': f"{subscription:.0f}€",
|
122 |
+
### 'attribution': f"{sim*100:.0f}%",
|
123 |
+
### 'author_month_reward': f"{author_month_reward:.0f}€",
|
124 |
+
### 'ro_month_reward': f"{ro_month_reward:.0f}€",
|
125 |
'raw_similarity': euclid
|
126 |
})
|
127 |
return rewards
|
|
|
223 |
def process_image():
|
224 |
if 'image' not in request.files:
|
225 |
return jsonify({'error': 'No image provided'}), 400
|
226 |
+
|
227 |
+
#Récupère l'image envoyée, la lit avec PIL (Image.open), la convertit en RGB
|
228 |
try:
|
229 |
+
image_file = request.files['image']
|
230 |
image = Image.open(io.BytesIO(image_file.read())).convert('RGB')
|
231 |
|
232 |
# Use default parameters if none provided
|
233 |
+
### params = DEFAULT_PARAMS.copy()
|
234 |
+
### if request.form:
|
235 |
+
### params.update({
|
236 |
+
### 'subscription': float(request.form.get('subscription', params['subscription'])),
|
237 |
+
### 'num_generations': int(request.form.get('num_generations', params['num_generations'])),
|
238 |
+
### 'author_share': float(request.form.get('author_share', params['author_share'])),
|
239 |
+
### 'ro_share': float(request.form.get('ro_share', params['ro_share'])),
|
240 |
+
### 'num_users_k': int(request.form.get('num_users_k', params['num_users_k'])),
|
241 |
+
### 'num_neighbors': int(request.form.get('num_neighbors', params['num_neighbors'])),
|
242 |
+
### 'num_authors': int(request.form.get('num_authors', DEFAULT_PARAMS['num_authors'])),
|
243 |
+
### })
|
244 |
|
245 |
# Process image
|
246 |
features = get_ft(model, image)
|
247 |
distances, indices = get_topk(index, features, topk=params['num_neighbors'])
|
248 |
|
249 |
# Collect valid results first
|
250 |
+
valid_results = [] #Pour chaque image trouvée : Vérifie que l'URL est valide (avec requests.head)
|
251 |
+
### valid_similarities = [] #Si l'URL est OK, ajoute l'image à valid_results et garde sa distance
|
252 |
+
### for i in range(params['num_neighbors']):
|
253 |
+
### image_url = urls[indices[0][i]].strip()
|
254 |
+
### try:
|
255 |
+
### response = requests.head(image_url)
|
256 |
+
### if response.status_code == 200:
|
257 |
+
### valid_results.append({
|
258 |
+
### 'index': i,
|
259 |
+
### 'url': image_url
|
260 |
+
### })
|
261 |
+
### valid_similarities.append(distances[0][i])
|
262 |
+
### except requests.RequestException:
|
263 |
+
### continue
|
264 |
|
265 |
# Renormalize similarities for valid results
|
266 |
+
### if valid_similarities:
|
267 |
+
### similarities = distance_to_similarity(np.array([valid_similarities]), temp=1e-5)
|
268 |
|
269 |
# Calculate rewards with renormalized similarities
|
270 |
+
### rewards = calculate_rewards(
|
271 |
+
### params['subscription'],
|
272 |
+
### params['num_generations'],
|
273 |
+
### params['author_share'],
|
274 |
+
### params['ro_share'],
|
275 |
+
### params['num_users_k'],
|
276 |
+
### similarities,
|
277 |
+
### params['num_authors']
|
278 |
+
### )
|
279 |
|
280 |
# Build final results
|
281 |
+
# Pour chaque image valide, associe l'URL, la récompense calculée, et des infos supplémentaires récupérées avec get_other_info()
|
282 |
results = []
|
283 |
for i, result in enumerate(valid_results):
|
284 |
other_info = get_other_info(result['url'])
|