from huggingface_hub import InferenceClient
from config import HF_TOKEN
from custom_css import SIMILARITY_BARS
client = InferenceClient(
provider="hf-inference",
api_key=HF_TOKEN,
)
def compute_similarity(reference_sentence: str, *comparison_sentences):
"""Compute similarity scores between reference and comparison sentences."""
sentences_list = [s.strip() for s in comparison_sentences if s and s.strip()]
if not sentences_list:
missing_reference_documents_message = "⚠️ Please enter at least one comparison sentence."
return f"
{missing_reference_documents_message}
"
try:
result = client.sentence_similarity(
sentence=reference_sentence,
other_sentences=sentences_list,
model="sentence-transformers/embeddinggemma-300m-medical",
)
sorted_results = sorted(zip(sentences_list, result), key=lambda x: x[1], reverse=True)
output = SIMILARITY_BARS
for idx, (sentence, score) in enumerate(sorted_results, 1):
percentage = score * 100
if score >= 0.7:
border_color = "#48bb78"
gradient = "linear-gradient(90deg, #48bb78 0%, #38a169 100%)"
elif score >= 0.5:
border_color = "#ed8936"
gradient = "linear-gradient(90deg, #ed8936 0%, #dd6b20 100%)"
else:
border_color = "#667eea"
gradient = "linear-gradient(90deg, #667eea 0%, #764ba2 100%)"
output += f"""
"""
output += ""
return output
except Exception as e:
return f"Error: {str(e)}
"