File size: 24,028 Bytes
340a140 | 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 | import streamlit as st
import requests
import json
import os
from pathlib import Path
# โโโโโโโโโโโโโโโโโโโโโโโโโโโ Page config โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.set_page_config(
page_title="Code Search",
page_icon="โ",
layout="wide",
initial_sidebar_state="expanded",
)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโ Custom CSS โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("""
<style>
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;600&family=Space+Grotesk:wght@300;400;500;600;700&display=swap');
/* โโ Root theme โโ */
:root {
--bg: #0d1117;
--bg2: #161b22;
--bg3: #21262d;
--border: #30363d;
--accent: #58a6ff;
--accent2: #3fb950;
--accent3: #f78166;
--accent4: #d2a8ff;
--text: #e6edf3;
--text2: #8b949e;
--text3: #484f58;
--mono: 'JetBrains Mono', monospace;
--sans: 'Space Grotesk', sans-serif;
}
/* โโ Global resets โโ */
html, body, [class*="css"] {
font-family: var(--sans) !important;
background-color: var(--bg) !important;
color: var(--text) !important;
}
.stApp { background-color: var(--bg) !important; }
/* โโ Sidebar โโ */
[data-testid="stSidebar"] {
background-color: var(--bg2) !important;
border-right: 1px solid var(--border) !important;
}
[data-testid="stSidebar"] * { color: var(--text) !important; }
/* โโ Inputs โโ */
.stTextInput input, .stTextArea textarea, .stSelectbox select,
[data-testid="stTextInput"] input, [data-baseweb="input"] input {
background-color: var(--bg3) !important;
border: 1px solid var(--border) !important;
border-radius: 6px !important;
color: var(--text) !important;
font-family: var(--mono) !important;
font-size: 13px !important;
}
.stTextInput input:focus, .stTextArea textarea:focus {
border-color: var(--accent) !important;
box-shadow: 0 0 0 3px rgba(88,166,255,0.1) !important;
}
/* โโ Buttons โโ */
.stButton > button {
background-color: var(--accent) !important;
color: #0d1117 !important;
border: none !important;
border-radius: 6px !important;
font-family: var(--mono) !important;
font-weight: 600 !important;
font-size: 13px !important;
padding: 8px 20px !important;
transition: all 0.2s !important;
}
.stButton > button:hover {
background-color: #79b8ff !important;
transform: translateY(-1px) !important;
box-shadow: 0 4px 12px rgba(88,166,255,0.3) !important;
}
.stButton > button[kind="secondary"] {
background-color: var(--bg3) !important;
color: var(--text) !important;
border: 1px solid var(--border) !important;
}
.stButton > button[kind="secondary"]:hover {
border-color: var(--accent3) !important;
color: var(--accent3) !important;
background-color: rgba(247,129,102,0.1) !important;
transform: none !important;
box-shadow: none !important;
}
/* โโ Tabs โโ */
.stTabs [data-baseweb="tab-list"] {
background-color: var(--bg2) !important;
border-bottom: 1px solid var(--border) !important;
gap: 0 !important;
}
.stTabs [data-baseweb="tab"] {
background-color: transparent !important;
color: var(--text2) !important;
font-family: var(--mono) !important;
font-size: 13px !important;
padding: 12px 20px !important;
border-radius: 0 !important;
border-bottom: 2px solid transparent !important;
}
.stTabs [aria-selected="true"] {
background-color: transparent !important;
color: var(--accent) !important;
border-bottom: 2px solid var(--accent) !important;
}
.stTabs [data-baseweb="tab-panel"] {
background-color: var(--bg) !important;
padding-top: 24px !important;
}
/* โโ Metric cards โโ */
[data-testid="metric-container"] {
background-color: var(--bg2) !important;
border: 1px solid var(--border) !important;
border-radius: 8px !important;
padding: 16px !important;
}
[data-testid="metric-container"] label {
color: var(--text2) !important;
font-family: var(--mono) !important;
font-size: 11px !important;
text-transform: uppercase !important;
letter-spacing: 0.08em !important;
}
[data-testid="metric-container"] [data-testid="stMetricValue"] {
color: var(--accent) !important;
font-family: var(--mono) !important;
font-size: 28px !important;
font-weight: 600 !important;
}
/* โโ File uploader โโ */
[data-testid="stFileUploader"] {
background-color: var(--bg2) !important;
border: 1px dashed var(--border) !important;
border-radius: 8px !important;
}
[data-testid="stFileUploader"]:hover {
border-color: var(--accent) !important;
}
/* โโ Success / error / info boxes โโ */
.stSuccess { background-color: rgba(63,185,80,0.1) !important; border-left: 3px solid var(--accent2) !important; }
.stError { background-color: rgba(247,129,102,0.1) !important; border-left: 3px solid var(--accent3) !important; }
.stInfo { background-color: rgba(88,166,255,0.08) !important; border-left: 3px solid var(--accent) !important; }
.stWarning { background-color: rgba(210,168,255,0.1) !important; border-left: 3px solid var(--accent4) !important; }
/* โโ JSON / code blocks โโ */
.stJson, pre, code {
background-color: var(--bg2) !important;
border: 1px solid var(--border) !important;
border-radius: 6px !important;
font-family: var(--mono) !important;
font-size: 12px !important;
}
/* โโ Divider โโ */
hr { border-color: var(--border) !important; margin: 24px 0 !important; }
/* โโ Result cards โโ */
.result-card {
background: var(--bg2);
border: 1px solid var(--border);
border-radius: 8px;
padding: 16px;
margin-bottom: 12px;
font-family: var(--mono);
font-size: 13px;
transition: border-color 0.2s;
}
.result-card:hover { border-color: var(--accent); }
.result-rank {
display: inline-block;
background: var(--accent);
color: #0d1117;
border-radius: 4px;
padding: 2px 8px;
font-size: 11px;
font-weight: 700;
margin-bottom: 8px;
}
.result-score {
float: right;
color: var(--accent2);
font-size: 12px;
font-weight: 600;
}
.result-text {
white-space: pre-wrap;
color: var(--text);
line-height: 1.6;
margin-top: 8px;
border-top: 1px solid var(--border);
padding-top: 8px;
}
/* โโ Doc table โโ */
.doc-row {
display: flex;
align-items: center;
background: var(--bg2);
border: 1px solid var(--border);
border-radius: 6px;
padding: 10px 16px;
margin-bottom: 8px;
gap: 12px;
font-family: var(--mono);
font-size: 13px;
}
.doc-id { color: var(--accent); flex: 1; font-weight: 500; }
.doc-chunks { color: var(--text2); font-size: 11px; }
/* โโ Header โโ */
.page-header {
display: flex;
align-items: center;
gap: 12px;
margin-bottom: 32px;
padding-bottom: 20px;
border-bottom: 1px solid var(--border);
}
.page-header h1 {
font-family: var(--mono) !important;
font-size: 22px !important;
font-weight: 600 !important;
color: var(--text) !important;
margin: 0 !important;
letter-spacing: -0.02em;
}
.page-header .subtitle {
color: var(--text2);
font-size: 13px;
font-family: var(--mono);
margin-top: 2px;
}
.status-dot {
width: 8px; height: 8px;
border-radius: 50%;
display: inline-block;
margin-right: 6px;
}
.status-dot.online { background: #3fb950; box-shadow: 0 0 6px #3fb950; }
.status-dot.offline { background: #f78166; box-shadow: 0 0 6px #f78166; }
/* โโ Slider โโ */
[data-testid="stSlider"] .st-emotion-cache-1gv3bxi { color: var(--text2) !important; }
</style>
""", unsafe_allow_html=True)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโ Helpers โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def api(method: str, path: str, **kwargs) -> requests.Response:
base = st.session_state.get("api_url", "").rstrip("/")
timeout = kwargs.pop("timeout", 60)
return requests.request(method, f"{base}{path}", timeout=timeout, **kwargs)
def health_check(url: str) -> bool:
try:
r = requests.get(url.rstrip("/") + "/health", timeout=5)
return r.ok and r.json().get("models_loaded", False)
except Exception:
return False
def render_result_card(r: dict):
st.markdown(f"""
<div class="result-card">
<span class="result-rank">#{r['rank']}</span>
<span class="result-score">score {r['score']:.4f}</span>
<div class="result-text">{r['text']}</div>
</div>
""", unsafe_allow_html=True)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโ Sidebar โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with st.sidebar:
st.markdown("### โ Code Search")
st.markdown("---")
api_url = st.text_input(
"API URL",
value=st.session_state.get("api_url", os.getenv("API_URL", "")),
placeholder="https://your-space.hf.space",
help="Your Code Search API HuggingFace Space URL",
)
st.session_state["api_url"] = api_url
if api_url:
is_up = health_check(api_url)
dot = "online" if is_up else "offline"
label = "API online ยท models loaded" if is_up else "API offline or unreachable"
st.markdown(
f'<span class="status-dot {dot}"></span>'
f'<span style="font-family:var(--mono);font-size:12px;color:var(--text2)">{label}</span>',
unsafe_allow_html=True,
)
st.markdown("---")
st.markdown(
'<span style="font-family:var(--mono);font-size:11px;color:var(--text3)">'
"jina-embeddings-v2-base-code<br>FAISS ยท AST chunking ยท /data persistence"
"</span>",
unsafe_allow_html=True,
)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโ Header โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
st.markdown("""
<div class="page-header">
<div>
<h1>โ Code Search</h1>
<div class="subtitle">semantic code search ยท powered by jina-embeddings-v2-base-code</div>
</div>
</div>
""", unsafe_allow_html=True)
if not api_url:
st.info("๐ Enter your API URL in the sidebar to get started.")
st.stop()
# โโโโโโโโโโโโโโโโโโโโโโโโโโโ Tabs โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
t_search, t_index, t_batch, t_docs, t_embed = st.tabs([
"๐ Search",
"๐ Index File",
"๐ฆ Batch Index",
"๐ Documents",
"๐งฎ Embed",
])
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# TAB 1 โ SEARCH
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with t_search:
st.markdown("#### Search an indexed codebase")
# Fetch doc list for autocomplete
doc_ids = []
try:
docs_resp = api("GET", "/documents", timeout=10)
if docs_resp.ok:
doc_ids = [d["doc_id"] for d in docs_resp.json().get("documents", [])]
except Exception:
pass
col1, col2 = st.columns([2, 1])
with col1:
if doc_ids:
doc_id = st.selectbox("Document / Project ID", options=doc_ids)
else:
doc_id = st.text_input("Document / Project ID", placeholder="my_project")
with col2:
top_k = st.slider("Top K results", min_value=1, max_value=20, value=5)
query = st.text_area(
"Query",
placeholder="e.g. fetch user from database\nor: async function that handles authentication",
height=80,
)
if st.button("Search โ", use_container_width=True):
if not query.strip():
st.warning("Enter a query.")
elif not doc_id:
st.warning("Enter a document ID.")
else:
with st.spinner("Searchingโฆ"):
try:
r = api("POST", "/search", json={
"doc_id": doc_id,
"query": query.strip(),
"top_k": top_k,
})
if r.ok:
data = r.json()
results = data.get("results", [])
st.markdown(f"**{len(results)} results** for `{query[:60]}`")
st.markdown("---")
for res in results:
render_result_card(res)
else:
st.error(f"API error {r.status_code}: {r.text}")
except Exception as e:
st.error(f"Request failed: {e}")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# TAB 2 โ INDEX SINGLE FILE
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with t_index:
st.markdown("#### Index a single source file")
uploaded = st.file_uploader(
"Upload file",
type=["py","js","ts","tsx","jsx","go","rs","java","cpp","c","cs","rb","php","md","txt"],
help="Supported: Python, JS, TS, Go, Rust, Java, C/C++, C#, Ruby, PHP, Markdown, text",
)
col1, col2 = st.columns(2)
with col1:
custom_id = st.text_input(
"Custom doc_id (optional)",
placeholder="Leave blank to use filename",
)
if uploaded:
st.markdown(
f'<div class="doc-row"><span class="doc-id">{uploaded.name}</span>'
f'<span class="doc-chunks">{uploaded.size:,} bytes</span></div>',
unsafe_allow_html=True,
)
if st.button("Index File โ", use_container_width=True):
if not uploaded:
st.warning("Upload a file first.")
else:
with st.spinner(f"Indexing `{uploaded.name}`โฆ"):
try:
files = {"file": (uploaded.name, uploaded.getvalue(), "text/plain")}
data = {"doc_id": custom_id.strip()}
r = api("POST", "/index", files=files, data=data, timeout=120)
if r.ok:
d = r.json()
c1, c2, c3 = st.columns(3)
c1.metric("doc_id", d["doc_id"])
c2.metric("Chunks", d["chunks_indexed"])
c3.metric("Status", "โ indexed")
st.success(d["message"])
else:
st.error(f"API error {r.status_code}: {r.text}")
except Exception as e:
st.error(f"Request failed: {e}")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# TAB 3 โ BATCH INDEX
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with t_batch:
st.markdown("#### Batch index an entire project")
st.markdown(
'<span style="font-family:var(--mono);font-size:12px;color:var(--text2)">'
"Upload multiple files โ they will all be indexed under one shared doc_id.</span>",
unsafe_allow_html=True,
)
batch_files = st.file_uploader(
"Upload files",
accept_multiple_files=True,
type=["py","js","ts","tsx","jsx","go","rs","java","cpp","c","cs","rb","php","md","txt"],
key="batch_uploader",
)
col1, col2 = st.columns([2, 1])
with col1:
batch_id = st.text_input("Project doc_id", placeholder="my_project", key="batch_id")
with col2:
replace = st.checkbox("Replace existing index", value=True)
if batch_files:
st.markdown(f"**{len(batch_files)} file(s) queued:**")
for f in batch_files:
st.markdown(
f'<div class="doc-row">'
f'<span class="doc-id">{f.name}</span>'
f'<span class="doc-chunks">{f.size:,} bytes</span>'
f'</div>',
unsafe_allow_html=True,
)
if st.button("Batch Index โ", use_container_width=True, key="batch_btn"):
if not batch_files:
st.warning("Upload at least one file.")
elif not batch_id.strip():
st.warning("Enter a project doc_id.")
else:
payload = {
"doc_id": batch_id.strip(),
"replace": replace,
"files": [
{"filename": f.name, "content": f.getvalue().decode("utf-8", errors="replace")}
for f in batch_files
],
}
with st.spinner(f"Indexing {len(batch_files)} files into `{batch_id}`โฆ"):
try:
r = api("POST", "/index/batch", json=payload, timeout=300)
if r.ok:
d = r.json()
c1, c2, c3 = st.columns(3)
c1.metric("doc_id", d["doc_id"])
c2.metric("Files indexed", d["files_indexed"])
c3.metric("Chunks indexed", d["chunks_indexed"])
st.success("Batch index complete!")
else:
st.error(f"API error {r.status_code}: {r.text}")
except Exception as e:
st.error(f"Request failed: {e}")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# TAB 4 โ DOCUMENTS
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with t_docs:
st.markdown("#### Indexed documents")
col_refresh, _ = st.columns([1, 4])
with col_refresh:
refresh = st.button("โป Refresh", key="refresh_docs")
try:
r = api("GET", "/documents", timeout=10)
if r.ok:
docs = r.json().get("documents", [])
if not docs:
st.info("No documents indexed yet. Use the Index or Batch Index tabs.")
else:
total_chunks = sum(d["chunks"] for d in docs)
m1, m2 = st.columns(2)
m1.metric("Documents", len(docs))
m2.metric("Total chunks", total_chunks)
st.markdown("---")
for doc in docs:
col_info, col_del = st.columns([5, 1])
with col_info:
st.markdown(
f'<div class="doc-row">'
f'<span class="doc-id">๐ {doc["doc_id"]}</span>'
f'<span class="doc-chunks">{doc["chunks"]:,} chunks</span>'
f'</div>',
unsafe_allow_html=True,
)
with col_del:
if st.button("Delete", key=f"del_{doc['doc_id']}", type="secondary"):
try:
dr = api("DELETE", f"/documents/{doc['doc_id']}", timeout=10)
if dr.ok:
st.success(f"Deleted `{doc['doc_id']}`")
st.rerun()
else:
st.error(dr.text)
except Exception as e:
st.error(str(e))
else:
st.error(f"API error {r.status_code}: {r.text}")
except Exception as e:
st.error(f"Could not reach API: {e}")
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# TAB 5 โ EMBED
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
with t_embed:
st.markdown("#### Embed arbitrary texts")
st.markdown(
'<span style="font-family:var(--mono);font-size:12px;color:var(--text2)">'
"Returns raw 768-dim float vectors. One text per line.</span>",
unsafe_allow_html=True,
)
raw_texts = st.text_area(
"Texts (one per line)",
placeholder="def getUserById(id):\n return db.query(User).filter(User.id == id).first()\nfetch user from database",
height=160,
)
if st.button("Embed โ", use_container_width=True):
texts = [t.strip() for t in raw_texts.strip().splitlines() if t.strip()]
if not texts:
st.warning("Enter at least one text.")
elif len(texts) > 64:
st.warning("Maximum 64 texts per request.")
else:
with st.spinner(f"Embedding {len(texts)} text(s)โฆ"):
try:
r = api("POST", "/embed", json={"texts": texts}, timeout=60)
if r.ok:
d = r.json()
embs = d["embeddings"]
st.metric("Dimensions", d["dimensions"])
st.markdown(f"**{len(embs)} embedding(s) returned**")
st.markdown("---")
for i, (txt, vec) in enumerate(zip(texts, embs)):
with st.expander(f"[{i}] `{txt[:60]}{'โฆ' if len(txt)>60 else ''}`"):
preview = vec[:16]
st.markdown(
f'<code style="font-size:11px;line-height:1.8">'
f'[{", ".join(f"{v:.5f}" for v in preview)}, โฆ]<br>'
f'<span style="color:var(--text3)">dim 768 ยท showing first 16</span>'
f'</code>',
unsafe_allow_html=True,
)
if st.toggle("Show full vector", key=f"full_{i}"):
st.json(vec)
else:
st.error(f"API error {r.status_code}: {r.text}")
except Exception as e:
st.error(f"Request failed: {e}") |