File size: 3,658 Bytes
77d88ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
538e8da
 
 
 
 
 
 
 
 
 
 
 
 
 
77d88ce
 
538e8da
 
 
 
 
77d88ce
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os, json
from typing import List
import numpy as np
import gradio as gr
import faiss
from sentence_transformers import SentenceTransformer

HF_TOKEN = os.getenv("HF_TOKEN")

# # ---------- Paths ----------
# APP_DIR    = os.path.dirname(__file__)
# CORPUS_JSON = os.path.join(APP_DIR, "corpus.json")
# FAISS_MAIN  = os.path.join(APP_DIR, "faiss_ip_768.index")

# # ---------- Load corpus ----------
# with open(CORPUS_JSON, "r", encoding="utf-8") as f:
#     corpus = json.load(f)  # [{"title", "text"}, ...]

# # ---------- Load FAISS index ----------
# if not os.path.exists(FAISS_MAIN):
#     raise FileNotFoundError(f"Missing FAISS index at {FAISS_MAIN}")
# index = faiss.read_index(FAISS_MAIN)

# # Infer dimension from index
# EMB_DIM = index.d

# ---------- Model ----------
model = SentenceTransformer("google/embeddinggemma-300m", token=HF_TOKEN)

# ---------- 1. Display Similarity ----------
def do_similarity(text_a: str, text_b: str) -> float:
    embeddings = model.encode(
        [text_a, text_b],
        normalize_embeddings=True,
        convert_to_numpy=True
    )

    # The dot product of two normalized vectors is their cosine similarity.
    similarity_score = np.dot(embeddings[0], embeddings[1])
    return float(similarity_score)

# # ---------- 2. Semantic Search ----------
# def do_search(query: str, top_k: int = 5) -> List[List[str]]:
#     if not query.strip():
#         return []
#     ##################################
#     # INSERT YOUR CODE HERE
#     ##################################
#     rows = []
#     for score, i in zip(scores[0], idxs[0]):
#         if i == -1: continue
#         item = corpus[i]
#         snippet = item["text"][:380] + ("…" if len(item["text"]) > 380 else "")
#         rows.append([f"{score:.4f}", item["title"], snippet])
#     return rows

# ---------- UI ----------
with gr.Blocks(theme=gr.themes.Soft(), title="Embeddings Lab") as demo:
    with gr.Row():
        with gr.Column(scale=3):
            gr.Markdown("""
                # 🧬 Google Gemma Embeddings Lab
                Compare the semantic similarity between two texts using Google Gemma embeddings.
            """)
        with gr.Column(scale=1):
            gr.Markdown("""
                ### ℹ️ Instructions
                - Enter two texts to compare their similarity.
                - Click **Compute Similarity** to see the cosine similarity score.
                - Example: Try comparing a question and its answer!
            """)
    with gr.Tabs():
        with gr.TabItem("Display Similarity"):
            with gr.Group():
                a = gr.Textbox(lines=4, label="Text A", placeholder="Enter first text...", value="What is AI?", show_copy_button=True)
                b = gr.Textbox(lines=4, label="Text B", placeholder="Enter second text...", value="Artificial Intelligence is the simulation of human intelligence in machines.", show_copy_button=True)
                sim_btn = gr.Button("πŸ” Compute Similarity", variant="primary")
                sim_out = gr.Number(label="Cosine similarity", precision=4)
            sim_btn.click(lambda x, y: do_similarity(x, y), [a, b], sim_out, api_name="similarity")
        # with gr.TabItem("Semantic Search"):
        #     q = gr.Textbox(label="Query")
        #     topk = gr.Slider(1, 20, value=5, step=1, label="Top-K")
        #     run = gr.Button("Search")
        #     out = gr.Dataframe(headers=["score", "title", "snippet"], wrap=True)
        #     run.click(lambda query, k: do_search(query, int(k)), [q, topk], out, api_name="search")

if __name__ == "__main__":
    demo.launch(server_name="0.0.0.0", server_port=7860)