Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files
app.py
CHANGED
|
@@ -142,16 +142,21 @@ def compute_quality_score(text: str) -> tuple[float, str, str]:
|
|
| 142 |
Retorna: (similarity_score, quality_label, verdict)
|
| 143 |
"""
|
| 144 |
with torch.no_grad():
|
| 145 |
-
|
|
|
|
| 146 |
|
| 147 |
-
#
|
|
|
|
|
|
|
|
|
|
| 148 |
text_norm = torch.norm(text_embedding).item()
|
| 149 |
-
anchor_norm = torch.norm(
|
| 150 |
-
print(f"📊 DEBUG - Text embedding norm: {text_norm:.4f}")
|
| 151 |
-
print(f"📊 DEBUG - Anchor
|
| 152 |
print(f"📊 DEBUG - Text[:50]: {text[:50]}...")
|
| 153 |
|
| 154 |
-
|
|
|
|
| 155 |
print(f"📊 DEBUG - Similaridade calculada: {similarity:.4f}")
|
| 156 |
|
| 157 |
# Classificação baseada no threshold
|
|
@@ -353,8 +358,9 @@ async def compute_similarity(request: SimilarityRequest):
|
|
| 353 |
raise HTTPException(status_code=400, detail="Texto não pode estar vazio")
|
| 354 |
|
| 355 |
with torch.no_grad():
|
| 356 |
-
text_embedding = sbert_model.encode(request.text, convert_to_tensor=True)
|
| 357 |
-
|
|
|
|
| 358 |
|
| 359 |
return SimilarityResponse(
|
| 360 |
similarity=round(similarity, 4),
|
|
|
|
| 142 |
Retorna: (similarity_score, quality_label, verdict)
|
| 143 |
"""
|
| 144 |
with torch.no_grad():
|
| 145 |
+
# Encode com normalização para garantir cálculo correto de cosseno
|
| 146 |
+
text_embedding = sbert_model.encode(text, convert_to_tensor=True, normalize_embeddings=True)
|
| 147 |
|
| 148 |
+
# Normalizar o anchor também (se não estiver normalizado)
|
| 149 |
+
anchor_normalized = ANCHOR_EMBEDDING / torch.norm(ANCHOR_EMBEDDING)
|
| 150 |
+
|
| 151 |
+
# Debug
|
| 152 |
text_norm = torch.norm(text_embedding).item()
|
| 153 |
+
anchor_norm = torch.norm(anchor_normalized).item()
|
| 154 |
+
print(f"📊 DEBUG - Text embedding norm (deve ser ~1.0): {text_norm:.4f}")
|
| 155 |
+
print(f"📊 DEBUG - Anchor norm (deve ser ~1.0): {anchor_norm:.4f}")
|
| 156 |
print(f"📊 DEBUG - Text[:50]: {text[:50]}...")
|
| 157 |
|
| 158 |
+
# Similaridade de cosseno (com vetores normalizados = dot product)
|
| 159 |
+
similarity = util.cos_sim(text_embedding, anchor_normalized).item()
|
| 160 |
print(f"📊 DEBUG - Similaridade calculada: {similarity:.4f}")
|
| 161 |
|
| 162 |
# Classificação baseada no threshold
|
|
|
|
| 358 |
raise HTTPException(status_code=400, detail="Texto não pode estar vazio")
|
| 359 |
|
| 360 |
with torch.no_grad():
|
| 361 |
+
text_embedding = sbert_model.encode(request.text, convert_to_tensor=True, normalize_embeddings=True)
|
| 362 |
+
anchor_normalized = ANCHOR_EMBEDDING / torch.norm(ANCHOR_EMBEDDING)
|
| 363 |
+
similarity = util.cos_sim(text_embedding, anchor_normalized).item()
|
| 364 |
|
| 365 |
return SimilarityResponse(
|
| 366 |
similarity=round(similarity, 4),
|