Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
from typing import List, Dict, Any
|
| 3 |
+
|
| 4 |
+
def preprocess_text(text: str) -> str:
|
| 5 |
+
"""Basic text preprocessing"""
|
| 6 |
+
# Add your custom preprocessing logic here
|
| 7 |
+
text = text.strip()
|
| 8 |
+
text = ' '.join(text.split()) # Remove extra whitespace
|
| 9 |
+
return text
|
| 10 |
+
|
| 11 |
+
def batch_predict(texts: List[str], classifier, batch_size: int = 32):
|
| 12 |
+
"""Process predictions in batches"""
|
| 13 |
+
results = []
|
| 14 |
+
for i in range(0, len(texts), batch_size):
|
| 15 |
+
batch = texts[i:i + batch_size]
|
| 16 |
+
batch_results = classifier(batch)
|
| 17 |
+
results.extend(batch_results)
|
| 18 |
+
return results
|
| 19 |
+
|
| 20 |
+
def confidence_score(predictions: List[Dict]) -> List[Dict]:
|
| 21 |
+
"""Add confidence scores to predictions"""
|
| 22 |
+
for pred in predictions:
|
| 23 |
+
if 'score' in pred:
|
| 24 |
+
pred['confidence'] = f"{pred['score']*100:.2f}%"
|
| 25 |
+
return predictions
|