Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from sentence_transformers import CrossEncoder | |
| # Load a Semantic Textual Similarity CrossEncoder | |
| model = CrossEncoder("cross-encoder/stsb-roberta-large") | |
| def predict_similarity(s1, s2): | |
| score = model.predict([(s1, s2)])[0] # already continuous | |
| return round(float(score), 4) | |
| with gr.Blocks(theme=gr.themes.Soft()) as demo: | |
| gr.Markdown("## π Semantic Textual Similarity (CrossEncoder)") | |
| gr.Markdown("Score ranges 0 (unrelated) β 1 (identical meaning).") | |
| with gr.Row(): | |
| s1 = gr.Textbox(label="Sentence 1", placeholder="Type first sentence...") | |
| s2 = gr.Textbox(label="Sentence 2", placeholder="Type second sentence...") | |
| btn = gr.Button("Compute Similarity π") | |
| out = gr.Number(label="Similarity Score (0β1)") | |
| btn.click(fn=predict_similarity, inputs=[s1, s2], outputs=out) | |
| gr.Examples( | |
| examples=[ | |
| ["I am a boy", "I am a girl"], | |
| ["I am happy today", "I am feeling a bit curious and joyful today"], | |
| ["I am going", "I am moving from you"], | |
| ["The sun is hot", "Ice cream is cold"], | |
| ], | |
| inputs=[s1, s2], | |
| ) | |
| demo.launch() | |