Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from sentence_transformers import SentenceTransformer
|
| 3 |
+
import faiss
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Knowledge base
|
| 7 |
+
documents = [
|
| 8 |
+
"Leave policy: Employees are entitled to 20 annual leaves per year.",
|
| 9 |
+
"Health insurance: The company provides full coverage for employees and partial coverage for dependents.",
|
| 10 |
+
"Working hours: Standard office hours are 9 AM to 6 PM, Monday to Friday.",
|
| 11 |
+
"Remote work: Employees may work from home 2 days per week with manager approval.",
|
| 12 |
+
"Promotions: Promotions are based on yearly performance reviews."
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
# Load embedding model
|
| 16 |
+
embedder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 17 |
+
doc_embeddings = embedder.encode(documents, convert_to_numpy=True)
|
| 18 |
+
|
| 19 |
+
# Create FAISS index
|
| 20 |
+
dimension = doc_embeddings.shape[1]
|
| 21 |
+
index = faiss.IndexFlatL2(dimension)
|
| 22 |
+
index.add(doc_embeddings)
|
| 23 |
+
|
| 24 |
+
# RAG function
|
| 25 |
+
def rag_chat(message, history):
|
| 26 |
+
query_embedding = embedder.encode([message], convert_to_numpy=True)
|
| 27 |
+
D, I = index.search(query_embedding, k=1)
|
| 28 |
+
best_doc = documents[I[0][0]]
|
| 29 |
+
response = f"📘 Policy Reference: {best_doc}"
|
| 30 |
+
return response
|
| 31 |
+
|
| 32 |
+
# Gradio UI
|
| 33 |
+
demo = gr.ChatInterface(
|
| 34 |
+
fn=rag_chat,
|
| 35 |
+
title="💡 WellCare AI - HR Assistant",
|
| 36 |
+
description="Ask me anything about leave, health insurance, or company policy.",
|
| 37 |
+
theme="soft"
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
demo.launch()
|