Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 model
|
| 7 |
+
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
|
| 8 |
+
|
| 9 |
+
def process_words(word_input):
|
| 10 |
+
words = [w.strip() for w in word_input.split(',') if w.strip()]
|
| 11 |
+
embeddings = model.encode(words).tolist()
|
| 12 |
+
|
| 13 |
+
short_preview = "\n".join([f"🔹 {word}" for word in words])
|
| 14 |
+
|
| 15 |
+
full_embedding_display = "\n\n".join(
|
| 16 |
+
[f"🔹 **{word}**: {np.round(vec, 4).tolist()}" for word, vec in zip(words, embeddings)]
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
return short_preview, full_embedding_display, words, embeddings
|
| 20 |
+
|
| 21 |
+
def ask_question(question, words, embeddings):
|
| 22 |
+
if not words or not embeddings:
|
| 23 |
+
return "⚠️ Please enter words first."
|
| 24 |
+
|
| 25 |
+
q_embedding = model.encode([question])[0]
|
| 26 |
+
similarities = cosine_similarity([q_embedding], embeddings)[0]
|
| 27 |
+
|
| 28 |
+
top_n = 3
|
| 29 |
+
top_indices = np.argsort(similarities)[::-1][:top_n]
|
| 30 |
+
|
| 31 |
+
top_matches = "\n".join(
|
| 32 |
+
[f"✅ **{words[i]}** (Similarity: {similarities[i]:.4f})" for i in top_indices]
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
full_scores = "\n".join(
|
| 36 |
+
[f"- {word}: {similarities[i]:.4f}" for i, word in enumerate(words)]
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
return f"🔍 **Top Matches:**\n{top_matches}\n\n📊 **All Similarities:**\n{full_scores}"
|
| 40 |
+
|
| 41 |
+
with gr.Blocks() as demo:
|
| 42 |
+
gr.Markdown("## 🔍 Semantic Search on Custom Word Embeddings")
|
| 43 |
+
|
| 44 |
+
with gr.Row():
|
| 45 |
+
word_input = gr.Textbox(placeholder="e.g., jesus, AI, christ, bird", label="Enter Words (comma-separated)")
|
| 46 |
+
generate_btn = gr.Button("Generate Embeddings")
|
| 47 |
+
|
| 48 |
+
preview = gr.Markdown(label="Words Entered")
|
| 49 |
+
full_embedding_output = gr.Markdown(label="Embeddings")
|
| 50 |
+
word_list_state = gr.State()
|
| 51 |
+
embedding_state = gr.State()
|
| 52 |
+
|
| 53 |
+
with gr.Accordion("🔽 View Full Embeddings", open=False):
|
| 54 |
+
full_display = gr.Markdown()
|
| 55 |
+
|
| 56 |
+
generate_btn.click(fn=process_words,
|
| 57 |
+
inputs=word_input,
|
| 58 |
+
outputs=[preview, full_display, word_list_state, embedding_state])
|
| 59 |
+
|
| 60 |
+
with gr.Row():
|
| 61 |
+
question_input = gr.Textbox(placeholder="Ask a question here...", label="Semantic Question")
|
| 62 |
+
ask_btn = gr.Button("Submit")
|
| 63 |
+
|
| 64 |
+
answer_output = gr.Markdown(label="Answer")
|
| 65 |
+
|
| 66 |
+
ask_btn.click(fn=ask_question,
|
| 67 |
+
inputs=[question_input, word_list_state, embedding_state],
|
| 68 |
+
outputs=answer_output)
|
| 69 |
+
|
| 70 |
+
demo.launch()
|