shl_assignment / app.py
Nikhillmahesh701's picture
Create app.py
5f73052 verified
import pandas as pd
import numpy as np
import faiss
from sentence_transformers import SentenceTransformer
import gradio as gr
# ---------- Load CSV ----------
df = pd.read_csv("shl_products.csv")
# ---------- Description Helper ----------
def create_description(row):
return f"{row['Pre-packaged Job Solutions']} is a {row['Test Type']} test. " \
f"Remote Testing support: {row['Remote Testing']}. Adaptive/IRT: {row['Adaptive/IRT']}."
df["description"] = df.apply(create_description, axis=1)
# ---------- Sentence Embeddings ----------
model = SentenceTransformer("all-MiniLM-L6-v2")
df["embedding"] = df["description"].apply(lambda x: model.encode(str(x)))
# ---------- Build FAISS Index ----------
embedding_matrix = np.stack(df["embedding"].values)
dimension = embedding_matrix.shape[1]
index = faiss.IndexFlatL2(dimension)
index.add(embedding_matrix)
# ---------- Retrieval Function ----------
def recommend_assessments(query, k=10):
query_embedding = model.encode(query)
D, I = index.search(np.array([query_embedding]), k)
results = df.iloc[I[0]][[
"Pre-packaged Job Solutions",
"Remote Testing",
"Adaptive/IRT",
"Test Type"
]]
return results.reset_index(drop=True)
# ---------- Gradio UI ----------
interface = gr.Interface(
fn=recommend_assessments,
inputs=gr.Textbox(lines=3, label="Job Description / Hiring Need"),
outputs=gr.Dataframe(type="pandas"),
title="SHL Assessment Recommender",
description="Enter a natural language hiring query and get relevant SHL assessments.",
examples=[
["Looking for a cognitive test for engineers that supports remote testing."],
["Assessment for sales roles with adaptive questions and remote option."],
["Test for developers that evaluates collaboration and reasoning in under 40 minutes."]
]
)
if __name__ == "__main__":
interface.launch()