Spaces:
Running
Running
File size: 1,138 Bytes
4a86a4b |
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 |
from .models import classifier
label_map = {
"LABEL_0": "Business",
"LABEL_1": "Entertainment",
"LABEL_2": "Health",
"LABEL_3": "Science",
"LABEL_4": "Sports",
"LABEL_5": "Technology",
"LABEL_6": "Politics",
"LABEL_7": "World"
}
def classify_text(text):
chunk_size = 1024
chunks = [text[i:i + chunk_size] for i in range(0, len(text), chunk_size)]
label_scores = {}
for i, chunk in enumerate(chunks):
try:
results = classifier(chunk)
for res in results:
label = res['label']
score = res['score']
label_scores[label] = label_scores.get(label, 0) + score
except Exception as e:
print(f"⚠️ Error classifying chunk {i+1}: {e}")
continue
if not label_scores:
return "⚠️ Could not classify any chunk."
final_label = max(label_scores, key=label_scores.get)
confidence = round(label_scores[final_label] / len(chunks), 3)
readable_label = label_map.get(final_label, final_label)
return f"{readable_label.upper()} (Aggregated Score: {confidence})"
|