vish85521 commited on
Commit
842c8ab
·
verified ·
1 Parent(s): 32fb489

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import chromadb
3
+ import pandas as pd
4
+ import os
5
+
6
+ # Fetch environment variables (defaults to your provided HF Space)
7
+ CHROMA_HOST = os.getenv("CHROMA_HOST", "vish85521-chromadb.hf.space")
8
+ CHROMA_PORT = os.getenv("CHROMA_PORT", "443")
9
+ CHROMA_SSL = os.getenv("CHROMA_SSL", "True").lower() in ("true", "1", "t")
10
+
11
+ st.set_page_config(page_title="ChromaDB Monitor", page_icon="📊", layout="wide")
12
+ st.title("📊 ChromaDB Monitor")
13
+
14
+ @st.cache_resource
15
+ def get_chroma_client():
16
+ return chromadb.HttpClient(
17
+ host=CHROMA_HOST,
18
+ port=CHROMA_PORT,
19
+ ssl=CHROMA_SSL
20
+ )
21
+
22
+ try:
23
+ with st.spinner(f"Connecting to {CHROMA_HOST}..."):
24
+ client = get_chroma_client()
25
+
26
+ # Check heartbeats to ensure connection is alive
27
+ tenant_heartbeat = client.heartbeat()
28
+ st.success(f"Connected successfully! (Heartbeat: {tenant_heartbeat})")
29
+
30
+ collections = client.list_collections()
31
+
32
+ if not collections:
33
+ st.warning("No collections found in this ChromaDB instance.")
34
+ else:
35
+ # Extract collection names for the dropdown
36
+ collection_names = [c.name for c in collections]
37
+ selected_collection = st.selectbox("Select a Collection to Monitor", collection_names)
38
+
39
+ if selected_collection:
40
+ col = client.get_collection(selected_collection)
41
+ count = col.count()
42
+ st.metric(label="Total Documents in Collection", value=count)
43
+
44
+ if count > 0:
45
+ st.subheader("Sample Data (Top 10 Rows)")
46
+ # Peek grabs a small sample of the data without doing a similarity search
47
+ results = col.peek(limit=10)
48
+
49
+ # Flatten the ChromaDB output into a readable Pandas DataFrame
50
+ if results and results.get("ids"):
51
+ df = pd.DataFrame({
52
+ "ID": results["ids"],
53
+ "Metadata": [str(m) for m in results["metadatas"]],
54
+ "Document": results["documents"]
55
+ })
56
+ st.dataframe(df, use_container_width=True)
57
+ else:
58
+ st.info("Collection data cannot be peeked or is empty.")
59
+
60
+ except Exception as e:
61
+ st.error(f"Failed to connect to ChromaDB.")
62
+ st.error(f"Error Details: {e}")
63
+ st.code(f"Host: {CHROMA_HOST}\nPort: {CHROMA_PORT}\nSSL: {CHROMA_SSL}")