Delete app.py
Browse files
app.py
DELETED
|
@@ -1,162 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from sentence_transformers import SentenceTransformer
|
| 4 |
-
from ddgs import DDGS
|
| 5 |
-
|
| 6 |
-
# Load Model
|
| 7 |
-
model = SentenceTransformer(
|
| 8 |
-
"RikkaBotan/stable-static-embedding-fast-retrieval-mrl-en",
|
| 9 |
-
trust_remote_code=True,
|
| 10 |
-
device="cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
-
)
|
| 12 |
-
|
| 13 |
-
# Web Search
|
| 14 |
-
def web_search(query, max_results=100):
|
| 15 |
-
results = []
|
| 16 |
-
with DDGS() as ddgs:
|
| 17 |
-
for r in ddgs.text(query, max_results=max_results):
|
| 18 |
-
results.append({
|
| 19 |
-
"title": r.get("title", ""),
|
| 20 |
-
"body": r.get("body", ""),
|
| 21 |
-
"href": r.get("href", "")
|
| 22 |
-
})
|
| 23 |
-
return results
|
| 24 |
-
|
| 25 |
-
# Semantic Ranking
|
| 26 |
-
def semantic_web_search(query):
|
| 27 |
-
if query.strip() == "":
|
| 28 |
-
return "Please enter a search query."
|
| 29 |
-
|
| 30 |
-
docs = web_search(query, max_results=100)
|
| 31 |
-
texts = [d["title"] + " " + d["body"] for d in docs]
|
| 32 |
-
|
| 33 |
-
with torch.no_grad():
|
| 34 |
-
embeddings = model.encode(
|
| 35 |
-
[query] + texts[:256],
|
| 36 |
-
convert_to_tensor=True,
|
| 37 |
-
normalize_embeddings=True
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
query_emb = embeddings[0]
|
| 41 |
-
doc_embs = embeddings[1:]
|
| 42 |
-
scores = (query_emb @ doc_embs.T).cpu().numpy()
|
| 43 |
-
|
| 44 |
-
ranked = sorted(zip(scores, docs), key=lambda x: x[0], reverse=True)[:30]
|
| 45 |
-
|
| 46 |
-
md = ""
|
| 47 |
-
for i, (score, d) in enumerate(ranked):
|
| 48 |
-
md += f"""
|
| 49 |
-
### 💎 Rank {i+1}
|
| 50 |
-
**[{d['title']}]({d['href']})**
|
| 51 |
-
|
| 52 |
-
**Similarity Score:** `{score:.4f}`
|
| 53 |
-
|
| 54 |
-
{d['body']}
|
| 55 |
-
|
| 56 |
-
---
|
| 57 |
-
"""
|
| 58 |
-
return md
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
pastel_css = """
|
| 62 |
-
body {
|
| 63 |
-
background: linear-gradient(180deg, #f4f9ff 0%, #eaf4ff 40%, #f9fcff 100%);
|
| 64 |
-
}
|
| 65 |
-
|
| 66 |
-
.gradio-container {
|
| 67 |
-
font-family: 'Helvetica Neue', sans-serif;
|
| 68 |
-
color: #4a6fa5;
|
| 69 |
-
}
|
| 70 |
-
|
| 71 |
-
/* model card */
|
| 72 |
-
.model-card {
|
| 73 |
-
background: #ffffff;
|
| 74 |
-
border-radius: 18px;
|
| 75 |
-
padding: 22px;
|
| 76 |
-
border: 1px solid #e3efff;
|
| 77 |
-
box-shadow: 0 12px 18px rgba(100, 150, 255, 0.15);
|
| 78 |
-
margin-bottom: 20px;
|
| 79 |
-
}
|
| 80 |
-
|
| 81 |
-
/* result card */
|
| 82 |
-
.result-card {
|
| 83 |
-
background: #ffffff;
|
| 84 |
-
border-radius: 18px;
|
| 85 |
-
padding: 22px;
|
| 86 |
-
border: 1px solid #e3efff;
|
| 87 |
-
box-shadow: 0 12px 18px rgba(100, 150, 255, 0.15);
|
| 88 |
-
}
|
| 89 |
-
|
| 90 |
-
/* remove markdown bg */
|
| 91 |
-
.gr-markdown, .prose {
|
| 92 |
-
background: transparent !important;
|
| 93 |
-
border: none !important;
|
| 94 |
-
box-shadow: none !important;
|
| 95 |
-
padding: 0 !important;
|
| 96 |
-
}
|
| 97 |
-
|
| 98 |
-
/* input */
|
| 99 |
-
textarea, input {
|
| 100 |
-
border-radius: 12px !important;
|
| 101 |
-
border: 1px solid #dbe9ff !important;
|
| 102 |
-
background-color: #f8fbff !important;
|
| 103 |
-
}
|
| 104 |
-
|
| 105 |
-
/* button */
|
| 106 |
-
button {
|
| 107 |
-
background: linear-gradient(90deg, #a8d0ff, #bde0ff) !important;
|
| 108 |
-
color: white !important;
|
| 109 |
-
border-radius: 14px !important;
|
| 110 |
-
border: none !important;
|
| 111 |
-
font-weight: 600;
|
| 112 |
-
box-shadow: 0 4px 10px rgba(120,170,255,0.25);
|
| 113 |
-
}
|
| 114 |
-
|
| 115 |
-
button:hover {
|
| 116 |
-
background: linear-gradient(90deg, #91c4ff, #a9d6ff) !important;
|
| 117 |
-
}
|
| 118 |
-
"""
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
# UI
|
| 122 |
-
with gr.Blocks(css=pastel_css) as demo:
|
| 123 |
-
|
| 124 |
-
gr.Markdown('# Semantic Web Search')
|
| 125 |
-
gr.Markdown('## Fast Semantic Retrieval with Stable Static Embedding')
|
| 126 |
-
|
| 127 |
-
with gr.Column(elem_classes="model-card"):
|
| 128 |
-
gr.Markdown("""
|
| 129 |
-
## About this Model
|
| 130 |
-
|
| 131 |
-
**RikkaBotan/stable-static-embedding-fast-retrieval-mrl-en**
|
| 132 |
-
|
| 133 |
-
### Performance
|
| 134 |
-
- NanoBEIR NDCG@10 = **0.5124**
|
| 135 |
-
- Higher than static-retrieval-mrl-en-v1 (0.5032)
|
| 136 |
-
|
| 137 |
-
### Efficiency
|
| 138 |
-
- 512 dimensions (half size)
|
| 139 |
-
- ~2× faster retrieval
|
| 140 |
-
- Separable Dynamic Tanh normalization
|
| 141 |
-
""")
|
| 142 |
-
|
| 143 |
-
query = gr.Textbox(
|
| 144 |
-
value="What is Stable Static Embedding?",
|
| 145 |
-
label="Enter your search query",
|
| 146 |
-
placeholder="Example: What is Stable Static Embedding?"
|
| 147 |
-
)
|
| 148 |
-
|
| 149 |
-
search_btn = gr.Button("Search")
|
| 150 |
-
|
| 151 |
-
with gr.Column(elem_classes="result-card"):
|
| 152 |
-
results = gr.Markdown()
|
| 153 |
-
|
| 154 |
-
search_btn.click(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