Spaces:
Build error
Build error
Create SentanceVector.py
Browse files- SentanceVector.py +76 -0
SentanceVector.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Load sentence transformer model
|
| 7 |
+
model = SentenceTransformer("sentence-transformers/all-mpnet-base-v2")
|
| 8 |
+
|
| 9 |
+
def process_sentences(sentences_text):
|
| 10 |
+
sentences = [s.strip() for s in sentences_text.split('\n') if s.strip()]
|
| 11 |
+
embeddings = model.encode(sentences).tolist()
|
| 12 |
+
|
| 13 |
+
preview = "\n".join([f"🔹 {s}" for s in sentences])
|
| 14 |
+
full_embedding = "\n\n".join([
|
| 15 |
+
f"🔹 **{s}**:\n{np.round(vec, 4).tolist()}" for s, vec in zip(sentences, embeddings)
|
| 16 |
+
])
|
| 17 |
+
return preview, full_embedding, sentences, embeddings
|
| 18 |
+
|
| 19 |
+
def ask_question(question, sentences, embeddings):
|
| 20 |
+
if not sentences or not embeddings:
|
| 21 |
+
return "⚠️ Please enter sentences first."
|
| 22 |
+
|
| 23 |
+
q_embedding = model.encode([question])[0]
|
| 24 |
+
similarities = cosine_similarity([q_embedding], embeddings)[0]
|
| 25 |
+
|
| 26 |
+
top_idx = np.argmax(similarities)
|
| 27 |
+
top_sentence = sentences[top_idx]
|
| 28 |
+
top_score = similarities[top_idx]
|
| 29 |
+
|
| 30 |
+
score_table = "\n".join([f"- {sentences[i]}: **{similarities[i]:.4f}**" for i in range(len(sentences))])
|
| 31 |
+
|
| 32 |
+
return f"✅ Closest sentence: **{top_sentence}** (Similarity: {top_score:.4f})\n\n📊 All Scores:\n{score_table}"
|
| 33 |
+
|
| 34 |
+
# Gradio UI
|
| 35 |
+
with gr.Blocks() as demo:
|
| 36 |
+
gr.Markdown("## 🧠 Sentence Embedding Search")
|
| 37 |
+
|
| 38 |
+
with gr.Row():
|
| 39 |
+
sentence_input = gr.Textbox(
|
| 40 |
+
lines=6,
|
| 41 |
+
placeholder="Enter up to 5 sentences (one per line)",
|
| 42 |
+
label="Enter Sentences"
|
| 43 |
+
)
|
| 44 |
+
generate_btn = gr.Button("Generate Embeddings")
|
| 45 |
+
|
| 46 |
+
sentence_preview = gr.Markdown(label="Entered Sentences")
|
| 47 |
+
sentence_embedding_full = gr.Markdown()
|
| 48 |
+
sentence_list_state = gr.State()
|
| 49 |
+
embedding_state = gr.State()
|
| 50 |
+
|
| 51 |
+
with gr.Accordion("🔽 View Full Embeddings", open=False):
|
| 52 |
+
gr.Markdown("Embeddings per sentence:")
|
| 53 |
+
# Fix: Update the component instance using its update method
|
| 54 |
+
# We pass the current value of sentence_embedding_full as the value to be updated
|
| 55 |
+
# sentence_embedding_full.update(value=sentence_embedding_full)
|
| 56 |
+
full_display = gr.Markdown() #changed to markdown for this accordion
|
| 57 |
+
|
| 58 |
+
generate_btn.click(
|
| 59 |
+
fn=process_sentences,
|
| 60 |
+
inputs=sentence_input,
|
| 61 |
+
outputs=[sentence_preview, full_display, sentence_list_state, embedding_state] #change this output to full_display
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
question_input = gr.Textbox(placeholder="Ask your question...", label="Semantic Question")
|
| 66 |
+
ask_btn = gr.Button("Submit")
|
| 67 |
+
|
| 68 |
+
question_output = gr.Markdown(label="Answer")
|
| 69 |
+
|
| 70 |
+
ask_btn.click(
|
| 71 |
+
fn=ask_question,
|
| 72 |
+
inputs=[question_input, sentence_list_state, embedding_state],
|
| 73 |
+
outputs=[question_output]
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
demo.launch()
|