Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import faiss
|
| 5 |
+
import math
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
|
| 10 |
+
# Load environment variables and configure Gemini
|
| 11 |
+
load_dotenv()
|
| 12 |
+
genai.configure(api_key=os.getenv("API_KEY"))
|
| 13 |
+
gemini_model = genai.GenerativeModel("gemini-2.0-flash")
|
| 14 |
+
|
| 15 |
+
# Load vector index and dataframe
|
| 16 |
+
index = faiss.read_index("shl_vector_index.faiss")
|
| 17 |
+
df = pd.read_csv("shl_combined_assessments.csv")
|
| 18 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 19 |
+
|
| 20 |
+
# Helper to format the result row
|
| 21 |
+
def format_row(row):
|
| 22 |
+
def safe_cast(val, cast_type, default):
|
| 23 |
+
try:
|
| 24 |
+
if val is None or (isinstance(val, float) and math.isnan(val)):
|
| 25 |
+
return default
|
| 26 |
+
return cast_type(val)
|
| 27 |
+
except Exception:
|
| 28 |
+
return default
|
| 29 |
+
|
| 30 |
+
return {
|
| 31 |
+
"Assignment_Name": str(row["Assignment_Name"]),
|
| 32 |
+
"Assignment_Link": str(row["Assignment_Link"]),
|
| 33 |
+
"Test_Type": str(row["Test_Type"]),
|
| 34 |
+
"Approximate_Completion_Time": safe_cast(row["Approximate_Completion_Time"], int, -1),
|
| 35 |
+
"Remote_Testing_Support": bool(row["Remote_Testing_Support"]),
|
| 36 |
+
"Adaptive_IRT_Support": bool(row["Adaptive_IRT_Support"]),
|
| 37 |
+
"Job_Levels": str(row.get("Job_Levels", "N/A")),
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Streamlit UI
|
| 41 |
+
st.set_page_config(page_title="SHL Assessment Recommender", layout="centered")
|
| 42 |
+
st.title("π SHL Assessment Recommender")
|
| 43 |
+
|
| 44 |
+
query = st.text_input("Enter your job role or requirement:", "")
|
| 45 |
+
|
| 46 |
+
if st.button("Search") and query:
|
| 47 |
+
with st.spinner("Finding best assessments..."):
|
| 48 |
+
query_embedding = model.encode([query]).astype("float32")
|
| 49 |
+
D, I = index.search(query_embedding, 10)
|
| 50 |
+
results = [format_row(df.iloc[idx]) for idx in I[0]]
|
| 51 |
+
|
| 52 |
+
st.subheader("π Top Recommendations")
|
| 53 |
+
if results:
|
| 54 |
+
for idx, r in enumerate(results, 1):
|
| 55 |
+
with st.container():
|
| 56 |
+
st.markdown(f"### π Rank {idx}: {r['Assignment_Name']}")
|
| 57 |
+
st.markdown(f"[π Assignment Link]({r['Assignment_Link']})")
|
| 58 |
+
st.markdown(f"- π§ͺ **Test Type**: {r['Test_Type']}")
|
| 59 |
+
st.markdown(f"- β±οΈ **Duration**: {r['Approximate_Completion_Time']} mins")
|
| 60 |
+
st.markdown(f"- π **Remote Testing**: {r['Remote_Testing_Support']}")
|
| 61 |
+
st.markdown(f"- π **Adaptive/IRT**: {r['Adaptive_IRT_Support']}")
|
| 62 |
+
st.markdown(f"- π€ **Job Levels**: {r['Job_Levels']}")
|
| 63 |
+
st.markdown("---")
|
| 64 |
+
else:
|
| 65 |
+
st.warning("No results found.")
|