Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import tempfile
|
3 |
+
|
4 |
+
# Use DuckDB instead of SQLite for ChromaDB
|
5 |
+
import chromadb
|
6 |
+
from chromadb.config import Settings
|
7 |
+
# Import your RAG components
|
8 |
+
from rag import build_chroma_store, pre_processing_csv, ask_query
|
9 |
+
from sentence_transformers import SentenceTransformer
|
10 |
+
|
11 |
+
# Temporary directory for persistence in Streamlit Cloud
|
12 |
+
temp_dir = tempfile.TemporaryDirectory()
|
13 |
+
|
14 |
+
@st.cache_resource
|
15 |
+
def load_data(csv_path):
|
16 |
+
"""Load and process data, caching the results."""
|
17 |
+
docs, metas = pre_processing_csv(csv_path)
|
18 |
+
|
19 |
+
# Use DuckDB + Parquet instead of SQLite
|
20 |
+
client = chromadb.Client(Settings(
|
21 |
+
persist_directory=temp_dir.name
|
22 |
+
))
|
23 |
+
|
24 |
+
collection, model = build_chroma_store(docs, metas, client=client)
|
25 |
+
return collection, model
|
26 |
+
|
27 |
+
# Load your CSV data
|
28 |
+
csv_path = "shl_products.csv" # Update path if needed
|
29 |
+
collection, model = load_data(csv_path)
|
30 |
+
|
31 |
+
# Streamlit UI
|
32 |
+
st.title("π§ RAG Model Query Interface")
|
33 |
+
st.write("Enter a query to get relevant SHL test assessments.")
|
34 |
+
|
35 |
+
# Query input
|
36 |
+
user_query = st.text_input("Enter your query:")
|
37 |
+
|
38 |
+
if st.button("Submit"):
|
39 |
+
if user_query:
|
40 |
+
results = ask_query(user_query, model, collection)
|
41 |
+
if results:
|
42 |
+
st.write(f"π Results for query: {user_query}")
|
43 |
+
st.write("=" * 80)
|
44 |
+
for i, (doc, meta) in enumerate(results, 1):
|
45 |
+
st.markdown(f"πΉ **Result {i}**")
|
46 |
+
st.markdown(f"π§ͺ **Test Name:** {meta['Test Name']}")
|
47 |
+
st.markdown(f"π **Link:** [https://www.shl.com{meta['Test Link']}]")
|
48 |
+
st.markdown(f"π **Chunk:** {doc}")
|
49 |
+
st.write("-" * 80)
|
50 |
+
else:
|
51 |
+
st.warning("No results found.")
|
52 |
+
else:
|
53 |
+
st.warning("Please enter a query.")
|