CoolFace
Apppublic

Atmosphere89/PromptAligner

sourceHugging Facemitupdated 11mo agoView on Hugging Face
0likes
feedback_module.py42 linesDownload Raw Back to root
1# ===========================2# core/feedback_module.py — Feedback Consistency Module (FCM)3# ===========================4 5from sentence_transformers import SentenceTransformer, util6 7# Load the sentence transformer model8# You can replace this with any other similarity model later9model = SentenceTransformer('all-MiniLM-L6-v2')10 11def calc_cds(prompt: str, feedback: str, caption: str) -> float:12    """13    Calculate the Consistency Deviation Score (CDS) based on semantic similarities.14 15    Parameters16    ----------17    prompt : str18        The original user prompt.19    feedback : str20        User feedback (e.g., improvement request or correction).21    caption : str22        Description generated from the image or model output.23 24    Returns25    -------26    float27        Consistency deviation score between 0 and 1.28        (Higher = larger mismatch between intent and result)29    """30    # Encode the text inputs31    p_emb = model.encode(prompt, convert_to_tensor=True)32    f_emb = model.encode(feedback, convert_to_tensor=True)33    c_emb = model.encode(caption, convert_to_tensor=True)34 35    # Compute pairwise cosine similarities36    pfs = util.cos_sim(p_emb, f_emb).item()37    pia = util.cos_sim(p_emb, c_emb).item()38    fia = util.cos_sim(f_emb, c_emb).item()39 40    # Combine into a single deviation score41    cds = 1 - (pfs + pia + fia) / 342    return max(0, min(1, cds))  # Normalize to 0–1