Spaces:
Running
Running
MinAA
commited on
Commit
·
fce9d70
1
Parent(s):
0a73db1
cleanup
Browse files
app.py
CHANGED
|
@@ -303,53 +303,13 @@ def audio_zero_shot_classifier(audio, candidate_labels, model_name):
|
|
| 303 |
result = classifier(audio, candidate_labels=labels)
|
| 304 |
output = "Результаты классификации:\n"
|
| 305 |
|
| 306 |
-
#
|
| 307 |
-
if isinstance(result,
|
| 308 |
-
# Формат: {'labels': [...], 'scores': [...]}
|
| 309 |
-
if 'labels' in result and 'scores' in result:
|
| 310 |
-
for label, score in zip(result['labels'], result['scores']):
|
| 311 |
-
output += f"{label}: {score:.4f}\n"
|
| 312 |
-
else:
|
| 313 |
-
# Пытаемся найти любые ключи, которые могут содержать результаты
|
| 314 |
-
for key, value in result.items():
|
| 315 |
-
if isinstance(value, (list, tuple)) and len(value) > 0:
|
| 316 |
-
if isinstance(value[0], dict):
|
| 317 |
-
for item in value:
|
| 318 |
-
if isinstance(item, dict):
|
| 319 |
-
label = item.get('label', item.get('labels', 'unknown'))
|
| 320 |
-
score = item.get('score', item.get('scores', 0.0))
|
| 321 |
-
if isinstance(score, (list, tuple)) and len(score) > 0:
|
| 322 |
-
score = score[0]
|
| 323 |
-
if isinstance(label, (list, tuple)) and len(label) > 0:
|
| 324 |
-
label = label[0]
|
| 325 |
-
output += f"{label}: {score:.4f}\n"
|
| 326 |
-
elif isinstance(result, list):
|
| 327 |
-
# Формат: [{'label': '...', 'score': ...}, ...] или список словарей
|
| 328 |
for item in result:
|
| 329 |
-
if isinstance(item, dict):
|
| 330 |
-
|
| 331 |
-
if 'label' in item and 'score' in item:
|
| 332 |
-
label = item['label']
|
| 333 |
-
score = item['score']
|
| 334 |
-
elif 'labels' in item and 'scores' in item:
|
| 335 |
-
# Вложенный формат
|
| 336 |
-
for label, score in zip(item['labels'], item['scores']):
|
| 337 |
-
output += f"{label}: {score:.4f}\n"
|
| 338 |
-
continue
|
| 339 |
-
else:
|
| 340 |
-
# Пытаемся извлечь любым способом
|
| 341 |
-
label = item.get('label', item.get('labels', 'unknown'))
|
| 342 |
-
score = item.get('score', item.get('scores', 0.0))
|
| 343 |
-
if isinstance(score, (list, tuple)) and len(score) > 0:
|
| 344 |
-
score = score[0]
|
| 345 |
-
if isinstance(label, (list, tuple)) and len(label) > 0:
|
| 346 |
-
label = label[0]
|
| 347 |
-
output += f"{label}: {score:.4f}\n"
|
| 348 |
-
else:
|
| 349 |
-
# Если элемент не словарь, возможно это вложенная структура
|
| 350 |
-
output += f"Элемент: {str(item)}\n"
|
| 351 |
else:
|
| 352 |
-
return f"Ошибка: Неожиданный формат результата от pipeline: {type(result)}. Ожидался
|
| 353 |
|
| 354 |
return output
|
| 355 |
except Exception as e:
|
|
@@ -934,37 +894,21 @@ def image_zero_shot_classification(image, candidate_labels, model_name):
|
|
| 934 |
result = classifier(image, candidate_labels=labels)
|
| 935 |
output = "Результаты классификации:\n"
|
| 936 |
|
| 937 |
-
#
|
| 938 |
-
if isinstance(result,
|
| 939 |
-
#
|
| 940 |
-
for label, score in zip(result['labels'], result['scores']):
|
| 941 |
-
output += f"{label}: {score:.4f}\n"
|
| 942 |
-
elif isinstance(result, list):
|
| 943 |
-
# Формат может быть: [{'label': '...', 'score': ...}, ...] или просто список словарей
|
| 944 |
-
# Также может быть список, где каждый элемент - это словарь с ключами 'label' и 'score'
|
| 945 |
for item in result:
|
| 946 |
-
if isinstance(item, dict):
|
| 947 |
-
|
| 948 |
-
if 'label' in item and 'score' in item:
|
| 949 |
-
label = item['label']
|
| 950 |
-
score = item['score']
|
| 951 |
-
elif 'labels' in item and 'scores' in item:
|
| 952 |
-
# Вложенный формат
|
| 953 |
-
for label, score in zip(item['labels'], item['scores']):
|
| 954 |
-
output += f"{label}: {score:.4f}\n"
|
| 955 |
-
continue
|
| 956 |
-
else:
|
| 957 |
-
# Пытаемся извлечь любым способом
|
| 958 |
-
label = item.get('label', item.get('labels', 'unknown'))
|
| 959 |
-
score = item.get('score', item.get('scores', 0.0))
|
| 960 |
-
if isinstance(score, (list, tuple)) and len(score) > 0:
|
| 961 |
-
score = score[0]
|
| 962 |
-
if isinstance(label, (list, tuple)) and len(label) > 0:
|
| 963 |
-
label = label[0]
|
| 964 |
-
output += f"{label}: {score:.4f}\n"
|
| 965 |
else:
|
| 966 |
-
|
| 967 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 968 |
else:
|
| 969 |
return f"Ошибка: Неожиданный формат результата от pipeline: {type(result)}. Ожидался словарь с ключами 'labels' и 'scores' или список словарей. Получен: {result}"
|
| 970 |
|
|
|
|
| 303 |
result = classifier(audio, candidate_labels=labels)
|
| 304 |
output = "Результаты классификации:\n"
|
| 305 |
|
| 306 |
+
# Парсинг результата: [{'score': ..., 'label': ...}, ...]
|
| 307 |
+
if isinstance(result, list):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
for item in result:
|
| 309 |
+
if isinstance(item, dict) and 'label' in item and 'score' in item:
|
| 310 |
+
output += f"{item['label']}: {item['score']:.4f}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
else:
|
| 312 |
+
return f"Ошибка: Неожиданный формат результата от pipeline: {type(result)}. Ожидался список словарей с ключами 'label' и 'score'."
|
| 313 |
|
| 314 |
return output
|
| 315 |
except Exception as e:
|
|
|
|
| 894 |
result = classifier(image, candidate_labels=labels)
|
| 895 |
output = "Результаты классификации:\n"
|
| 896 |
|
| 897 |
+
# Парсим результат
|
| 898 |
+
if isinstance(result, list):
|
| 899 |
+
# Результат - ��писок словарей с 'score' и 'label'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 900 |
for item in result:
|
| 901 |
+
if isinstance(item, dict) and 'label' in item and 'score' in item:
|
| 902 |
+
output += f"{item['label']}: {item['score']:.4f}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 903 |
else:
|
| 904 |
+
return f"Ошибка: Неожиданный формат элемента в результате: {item}"
|
| 905 |
+
elif isinstance(result, dict):
|
| 906 |
+
# Результат - словарь с 'labels' и 'scores'
|
| 907 |
+
if 'labels' in result and 'scores' in result:
|
| 908 |
+
for label, score in zip(result['labels'], result['scores']):
|
| 909 |
+
output += f"{label}: {score:.4f}\n"
|
| 910 |
+
else:
|
| 911 |
+
return f"Ошибка: Неожиданный формат результата от pipeline: {result}. Ожидался словарь с ключами 'labels' и 'scores' или список словарей."
|
| 912 |
else:
|
| 913 |
return f"Ошибка: Неожиданный формат результата от pipeline: {type(result)}. Ожидался словарь с ключами 'labels' и 'scores' или список словарей. Получен: {result}"
|
| 914 |
|