github-actions commited on
Commit
0e3c6bf
·
1 Parent(s): f1b408d

Auto deploy from GitHub Actions

Browse files
Files changed (24) hide show
  1. app.py +23 -12
  2. hf-space/app.py +19 -10
  3. hf-space/hf-space/hf-space/app.py +9 -18
  4. hf-space/hf-space/hf-space/hf-space/hf-space/app.py +24 -15
  5. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/config.py +1 -1
  6. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/.github/workflows/deploy.yml +43 -0
  7. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/.gitignore +5 -0
  8. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/app.py +45 -0
  9. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/config.py +12 -0
  10. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/.gitattributes +35 -0
  11. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/README.md +14 -0
  12. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/ingest.py +34 -0
  13. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/prompts.py +60 -0
  14. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py +75 -0
  15. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt +10 -0
  16. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/vectorstore/faiss_index/index.faiss +0 -0
  17. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/vectorstore/faiss_index/index.pkl +3 -0
  18. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py +28 -43
  19. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt +9 -9
  20. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt +0 -1
  21. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py +57 -18
  22. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt +1 -0
  23. hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py +71 -36
  24. hf-space/hf-space/rag_pipeline.py +23 -43
app.py CHANGED
@@ -7,36 +7,47 @@ def respond(message, history):
7
  if history is None:
8
  history = []
9
 
10
- response = ask_question(message)
 
 
 
 
11
 
12
- history.append(
13
- (message, response)
14
- )
 
 
 
 
 
15
 
16
- return "", history
17
 
18
  with gr.Blocks() as demo:
19
 
20
  gr.Markdown("# Airlines HR Assistant")
21
 
22
- chatbot = gr.Chatbot(height=500)
 
 
 
23
 
24
  msg = gr.Textbox(
25
- placeholder="Ask a question..."
26
  )
27
 
28
  clear = gr.Button("Clear Chat")
29
 
30
  msg.submit(
31
  respond,
32
- [msg, chatbot],
33
- [msg, chatbot]
34
  )
35
 
36
  clear.click(
37
- lambda: None,
38
- None,
39
- chatbot,
40
  queue=False
41
  )
42
 
 
7
  if history is None:
8
  history = []
9
 
10
+ # Add user message
11
+ history.append({
12
+ "role": "user",
13
+ "content": message
14
+ })
15
 
16
+ # Generate response
17
+ bot_response = ask_question(message)
18
+
19
+ # Add assistant response
20
+ history.append({
21
+ "role": "assistant",
22
+ "content": bot_response
23
+ })
24
 
25
+ return history
26
 
27
  with gr.Blocks() as demo:
28
 
29
  gr.Markdown("# Airlines HR Assistant")
30
 
31
+ chatbot = gr.Chatbot(
32
+ label="HR Chat",
33
+ height=500
34
+ )
35
 
36
  msg = gr.Textbox(
37
+ placeholder="Ask HR policy questions..."
38
  )
39
 
40
  clear = gr.Button("Clear Chat")
41
 
42
  msg.submit(
43
  respond,
44
+ inputs=[msg, chatbot],
45
+ outputs=[chatbot]
46
  )
47
 
48
  clear.click(
49
+ lambda: [],
50
+ outputs=[chatbot],
 
51
  queue=False
52
  )
53
 
hf-space/app.py CHANGED
@@ -7,19 +7,29 @@ def respond(message, history):
7
  if history is None:
8
  history = []
9
 
10
- bot_message = ask_question(message)
 
 
 
 
11
 
12
- history.append(
13
- (message, bot_message)
14
- )
 
 
 
 
 
15
 
16
- return "", history
17
 
18
  with gr.Blocks() as demo:
19
 
20
  gr.Markdown("# Airlines HR Assistant")
21
 
22
  chatbot = gr.Chatbot(
 
23
  height=500
24
  )
25
 
@@ -31,14 +41,13 @@ with gr.Blocks() as demo:
31
 
32
  msg.submit(
33
  respond,
34
- [msg, chatbot],
35
- [msg, chatbot]
36
  )
37
 
38
  clear.click(
39
- lambda: None,
40
- None,
41
- chatbot,
42
  queue=False
43
  )
44
 
 
7
  if history is None:
8
  history = []
9
 
10
+ # Add user message
11
+ history.append({
12
+ "role": "user",
13
+ "content": message
14
+ })
15
 
16
+ # Generate response
17
+ bot_response = ask_question(message)
18
+
19
+ # Add assistant response
20
+ history.append({
21
+ "role": "assistant",
22
+ "content": bot_response
23
+ })
24
 
25
+ return history
26
 
27
  with gr.Blocks() as demo:
28
 
29
  gr.Markdown("# Airlines HR Assistant")
30
 
31
  chatbot = gr.Chatbot(
32
+ label="HR Chat",
33
  height=500
34
  )
35
 
 
41
 
42
  msg.submit(
43
  respond,
44
+ inputs=[msg, chatbot],
45
+ outputs=[chatbot]
46
  )
47
 
48
  clear.click(
49
+ lambda: [],
50
+ outputs=[chatbot],
 
51
  queue=False
52
  )
53
 
hf-space/hf-space/hf-space/app.py CHANGED
@@ -7,29 +7,19 @@ def respond(message, history):
7
  if history is None:
8
  history = []
9
 
10
- # Add user message
11
- history.append({
12
- "role": "user",
13
- "content": message
14
- })
15
-
16
- # Generate bot response
17
  bot_message = ask_question(message)
18
 
19
- # Add assistant response
20
- history.append({
21
- "role": "assistant",
22
- "content": bot_message
23
- })
24
 
25
- return history, history
26
 
27
  with gr.Blocks() as demo:
28
 
29
  gr.Markdown("# Airlines HR Assistant")
30
 
31
  chatbot = gr.Chatbot(
32
- type="messages",
33
  height=500
34
  )
35
 
@@ -41,13 +31,14 @@ with gr.Blocks() as demo:
41
 
42
  msg.submit(
43
  respond,
44
- inputs=[msg, chatbot],
45
- outputs=[chatbot, chatbot]
46
  )
47
 
48
  clear.click(
49
- lambda: [],
50
- outputs=chatbot,
 
51
  queue=False
52
  )
53
 
 
7
  if history is None:
8
  history = []
9
 
 
 
 
 
 
 
 
10
  bot_message = ask_question(message)
11
 
12
+ history.append(
13
+ (message, bot_message)
14
+ )
 
 
15
 
16
+ return "", history
17
 
18
  with gr.Blocks() as demo:
19
 
20
  gr.Markdown("# Airlines HR Assistant")
21
 
22
  chatbot = gr.Chatbot(
 
23
  height=500
24
  )
25
 
 
31
 
32
  msg.submit(
33
  respond,
34
+ [msg, chatbot],
35
+ [msg, chatbot]
36
  )
37
 
38
  clear.click(
39
+ lambda: None,
40
+ None,
41
+ chatbot,
42
  queue=False
43
  )
44
 
hf-space/hf-space/hf-space/hf-space/hf-space/app.py CHANGED
@@ -1,25 +1,35 @@
1
-
2
  import gradio as gr
3
 
4
  from rag_pipeline import ask_question
5
 
6
- def respond(message, chat_history):
 
 
 
 
 
 
 
 
 
7
 
 
8
  bot_message = ask_question(message)
9
 
10
- chat_history.append(
11
- (message, bot_message)
12
- )
 
 
13
 
14
- return "", chat_history
15
 
16
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
17
 
18
- gr.Markdown(
19
- "# Airlines HR Assistant"
20
- )
21
 
22
  chatbot = gr.Chatbot(
 
23
  height=500
24
  )
25
 
@@ -31,14 +41,13 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
31
 
32
  msg.submit(
33
  respond,
34
- [msg, chatbot],
35
- [msg, chatbot]
36
  )
37
 
38
  clear.click(
39
- lambda: None,
40
- None,
41
- chatbot,
42
  queue=False
43
  )
44
 
 
 
1
  import gradio as gr
2
 
3
  from rag_pipeline import ask_question
4
 
5
+ def respond(message, history):
6
+
7
+ if history is None:
8
+ history = []
9
+
10
+ # Add user message
11
+ history.append({
12
+ "role": "user",
13
+ "content": message
14
+ })
15
 
16
+ # Generate bot response
17
  bot_message = ask_question(message)
18
 
19
+ # Add assistant response
20
+ history.append({
21
+ "role": "assistant",
22
+ "content": bot_message
23
+ })
24
 
25
+ return history, history
26
 
27
+ with gr.Blocks() as demo:
28
 
29
+ gr.Markdown("# Airlines HR Assistant")
 
 
30
 
31
  chatbot = gr.Chatbot(
32
+ type="messages",
33
  height=500
34
  )
35
 
 
41
 
42
  msg.submit(
43
  respond,
44
+ inputs=[msg, chatbot],
45
+ outputs=[chatbot, chatbot]
46
  )
47
 
48
  clear.click(
49
+ lambda: [],
50
+ outputs=chatbot,
 
51
  queue=False
52
  )
53
 
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/config.py CHANGED
@@ -1,7 +1,7 @@
1
 
2
  MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
3
 
4
- LLM_MODEL = "google/flan-t5-base"
5
 
6
  PDF_PATH = "data/manual.pdf"
7
 
 
1
 
2
  MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
3
 
4
+ LLM_MODEL = "google/flan-t5-small"
5
 
6
  PDF_PATH = "data/manual.pdf"
7
 
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/.github/workflows/deploy.yml ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: Deploy to Hugging Face Spaces
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - main
7
+
8
+ jobs:
9
+ deploy:
10
+ runs-on: ubuntu-latest
11
+
12
+ env:
13
+ FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
14
+
15
+ steps:
16
+
17
+ - name: Checkout Repository
18
+ uses: actions/checkout@v5
19
+
20
+ - name: Deploy to Hugging Face Space
21
+
22
+ env:
23
+ HF_TOKEN: ${{ secrets.HF_TOKEN }}
24
+
25
+ run: |
26
+
27
+ git config --global user.email "github-actions@github.com"
28
+ git config --global user.name "github-actions"
29
+
30
+ git clone https://mdsalmon159:$HF_TOKEN@huggingface.co/spaces/mdsalmon159/hrchatbot hf-space
31
+
32
+ rsync -av \
33
+ --exclude='.git' \
34
+ --exclude='data/manual.pdf' \
35
+ ./ hf-space/
36
+
37
+ cd hf-space
38
+
39
+ git add .
40
+
41
+ git commit -m "Auto deploy from GitHub Actions" || echo "No changes to commit"
42
+
43
+ git push
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/.gitignore ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+
2
+ __pycache__/
3
+ .ipynb_checkpoints/
4
+ *.pyc
5
+ data/manual.pdf
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+
4
+ from rag_pipeline import ask_question
5
+
6
+ def respond(message, chat_history):
7
+
8
+ bot_message = ask_question(message)
9
+
10
+ chat_history.append(
11
+ (message, bot_message)
12
+ )
13
+
14
+ return "", chat_history
15
+
16
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
17
+
18
+ gr.Markdown(
19
+ "# Airlines HR Assistant"
20
+ )
21
+
22
+ chatbot = gr.Chatbot(
23
+ height=500
24
+ )
25
+
26
+ msg = gr.Textbox(
27
+ placeholder="Ask HR policy questions..."
28
+ )
29
+
30
+ clear = gr.Button("Clear Chat")
31
+
32
+ msg.submit(
33
+ respond,
34
+ [msg, chatbot],
35
+ [msg, chatbot]
36
+ )
37
+
38
+ clear.click(
39
+ lambda: None,
40
+ None,
41
+ chatbot,
42
+ queue=False
43
+ )
44
+
45
+ demo.launch()
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/config.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ MODEL_NAME = "sentence-transformers/all-MiniLM-L6-v2"
3
+
4
+ LLM_MODEL = "google/flan-t5-base"
5
+
6
+ PDF_PATH = "data/manual.pdf"
7
+
8
+ VECTOR_DB_PATH = "vectorstore/faiss_index"
9
+
10
+ CHUNK_SIZE = 500
11
+
12
+ CHUNK_OVERLAP = 50
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/.gitattributes ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/README.md ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Hrchatbot
3
+ emoji: 📈
4
+ colorFrom: blue
5
+ colorTo: purple
6
+ sdk: gradio
7
+ sdk_version: 6.14.0
8
+ python_version: '3.13'
9
+ app_file: app.py
10
+ pinned: false
11
+ license: apache-2.0
12
+ ---
13
+
14
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/ingest.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from langchain_community.document_loaders import PyPDFLoader
3
+
4
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
5
+
6
+ from langchain_community.embeddings import HuggingFaceEmbeddings
7
+
8
+ from langchain_community.vectorstores import FAISS
9
+
10
+ from config import *
11
+
12
+ loader = PyPDFLoader(PDF_PATH)
13
+
14
+ documents = loader.load()
15
+
16
+ splitter = RecursiveCharacterTextSplitter(
17
+ chunk_size=500,
18
+ chunk_overlap=50
19
+ )
20
+
21
+ docs = splitter.split_documents(documents)
22
+
23
+ embeddings = HuggingFaceEmbeddings(
24
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
25
+ )
26
+
27
+ vector_db = FAISS.from_documents(
28
+ docs,
29
+ embeddings
30
+ )
31
+
32
+ vector_db.save_local(VECTOR_DB_PATH)
33
+
34
+ print("Vector DB created successfully")
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/prompts.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ SYSTEM_PROMPT = """
3
+ You are an Airlines HR Assistant AI responsible for answering employee questions based strictly on the official HR handbook and company policy documents.
4
+
5
+ The user input will contain contextual information retrieved from the HR handbook.
6
+
7
+ The context section begins with:
8
+ ###Context
9
+
10
+ The employee question begins with:
11
+ ###Question
12
+
13
+ Your role is to provide accurate HR support using ONLY the provided handbook information.
14
+
15
+ Instructions:
16
+
17
+ - Answer ONLY using the information present in the provided context.
18
+ - Do NOT use external knowledge.
19
+ - Do NOT make assumptions or generate information not explicitly stated.
20
+ - Do NOT mention the context, document, handbook excerpts, or retrieval process in your response.
21
+ - Keep responses professional, concise, and employee-friendly.
22
+ - Use bullet points when appropriate for clarity.
23
+ - Maintain the tone of an HR representative.
24
+ - Focus strictly on HR and company policy topics including:
25
+ - Leave policies
26
+ - Attendance
27
+ - Payroll
28
+ - Employee conduct
29
+ - Benefits
30
+ - Working hours
31
+ - Travel policy
32
+ - Reimbursements
33
+ - Escalation procedures
34
+ - Employee responsibilities
35
+ - Company procedures
36
+
37
+ Rules for answering:
38
+
39
+ 1. If the answer exists in the context:
40
+ - Provide a direct and accurate answer.
41
+ - Keep formatting clean and readable.
42
+ - Avoid unnecessary elaboration.
43
+
44
+ 2. If the answer is partially available:
45
+ - Answer only the portion supported by the context.
46
+
47
+ 3. If the answer is NOT found in the context:
48
+ Respond EXACTLY with:
49
+ "I couldn't find this information in the handbook."
50
+
51
+ 4. Never fabricate policy details.
52
+
53
+ 5. Never answer questions unrelated to the HR handbook.
54
+
55
+ 6. If the user asks ethical, legal, medical, or safety-sensitive questions not covered in the handbook:
56
+ Respond EXACTLY with:
57
+ "I couldn't find this information in the handbook."
58
+
59
+ Answer carefully and ensure policy accuracy.
60
+ """
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import pipeline
3
+
4
+ from langchain_community.llms import HuggingFacePipeline
5
+
6
+ from langchain.chains import RetrievalQA
7
+
8
+ from langchain.prompts import PromptTemplate
9
+
10
+ from langchain_community.embeddings import HuggingFaceEmbeddings
11
+
12
+ from langchain_community.vectorstores import FAISS
13
+
14
+ from config import *
15
+
16
+ from prompts import SYSTEM_PROMPT
17
+
18
+ template = """
19
+ {system_prompt}
20
+
21
+ ###Context:
22
+ {context}
23
+
24
+ ###Question:
25
+ {question}
26
+
27
+ Answer:
28
+ """
29
+
30
+ PROMPT = PromptTemplate(
31
+ template=template,
32
+ input_variables=["context", "question"],
33
+ partial_variables={
34
+ "system_prompt": SYSTEM_PROMPT
35
+ }
36
+ )
37
+
38
+ embeddings = HuggingFaceEmbeddings(
39
+ model_name="sentence-transformers/all-MiniLM-L6-v2"
40
+ )
41
+
42
+ vector_db = FAISS.load_local(
43
+ VECTOR_DB_PATH,
44
+ embeddings,
45
+ allow_dangerous_deserialization=True
46
+ )
47
+
48
+ retriever = vector_db.as_retriever(
49
+ search_kwargs={"k": 3}
50
+ )
51
+
52
+ pipe = pipeline(
53
+ "text2text-generation",
54
+ model="google/flan-t5-base",
55
+ max_new_tokens=256
56
+ )
57
+
58
+ llm = HuggingFacePipeline(
59
+ pipeline=pipe
60
+ )
61
+
62
+ qa_chain = RetrievalQA.from_chain_type(
63
+ llm=llm,
64
+ retriever=retriever,
65
+ chain_type="stuff",
66
+ chain_type_kwargs={
67
+ "prompt": PROMPT
68
+ }
69
+ )
70
+
71
+ def ask_question(query):
72
+
73
+ response = qa_chain.run(query)
74
+
75
+ return response
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ gradio
3
+ langchain
4
+ langchain-community
5
+ sentence-transformers
6
+ transformers
7
+ accelerate
8
+ faiss-cpu
9
+ pypdf
10
+ torch
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/vectorstore/faiss_index/index.faiss ADDED
Binary file (73.8 kB). View file
 
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/vectorstore/faiss_index/index.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:5873301a1f6aab0cae850445e83a16e71f895000af15ff65d6dd28ed8d9873f4
3
+ size 26589
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py CHANGED
@@ -1,12 +1,5 @@
1
-
2
  from transformers import pipeline
3
 
4
- from langchain_community.llms import HuggingFacePipeline
5
-
6
- from langchain.chains import RetrievalQA
7
-
8
- from langchain.prompts import PromptTemplate
9
-
10
  from langchain_community.embeddings import HuggingFaceEmbeddings
11
 
12
  from langchain_community.vectorstores import FAISS
@@ -15,61 +8,53 @@ from config import *
15
 
16
  from prompts import SYSTEM_PROMPT
17
 
18
- template = """
19
- {system_prompt}
20
-
21
- ###Context:
22
- {context}
23
-
24
- ###Question:
25
- {question}
26
-
27
- Answer:
28
- """
29
-
30
- PROMPT = PromptTemplate(
31
- template=template,
32
- input_variables=["context", "question"],
33
- partial_variables={
34
- "system_prompt": SYSTEM_PROMPT
35
- }
36
- )
37
-
38
  embeddings = HuggingFaceEmbeddings(
39
- model_name="sentence-transformers/all-MiniLM-L6-v2"
40
  )
41
 
 
42
  vector_db = FAISS.load_local(
43
  VECTOR_DB_PATH,
44
  embeddings,
45
  allow_dangerous_deserialization=True
46
  )
47
 
 
48
  retriever = vector_db.as_retriever(
49
  search_kwargs={"k": 3}
50
  )
51
 
 
52
  pipe = pipeline(
53
  "text2text-generation",
54
- model="google/flan-t5-base",
55
  max_new_tokens=256
56
  )
57
 
58
- llm = HuggingFacePipeline(
59
- pipeline=pipe
60
- )
61
 
62
- qa_chain = RetrievalQA.from_chain_type(
63
- llm=llm,
64
- retriever=retriever,
65
- chain_type="stuff",
66
- chain_type_kwargs={
67
- "prompt": PROMPT
68
- }
69
- )
70
 
71
- def ask_question(query):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- response = qa_chain.run(query)
74
 
75
- return response
 
 
1
  from transformers import pipeline
2
 
 
 
 
 
 
 
3
  from langchain_community.embeddings import HuggingFaceEmbeddings
4
 
5
  from langchain_community.vectorstores import FAISS
 
8
 
9
  from prompts import SYSTEM_PROMPT
10
 
11
+ # Load embedding model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  embeddings = HuggingFaceEmbeddings(
13
+ model_name=MODEL_NAME
14
  )
15
 
16
+ # Load vector database
17
  vector_db = FAISS.load_local(
18
  VECTOR_DB_PATH,
19
  embeddings,
20
  allow_dangerous_deserialization=True
21
  )
22
 
23
+ # Create retriever
24
  retriever = vector_db.as_retriever(
25
  search_kwargs={"k": 3}
26
  )
27
 
28
+ # Load LLM
29
  pipe = pipeline(
30
  "text2text-generation",
31
+ model=LLM_MODEL,
32
  max_new_tokens=256
33
  )
34
 
35
+ def ask_question(query):
 
 
36
 
37
+ # Retrieve relevant docs
38
+ docs = retriever.get_relevant_documents(query)
 
 
 
 
 
 
39
 
40
+ context = "\n\n".join(
41
+ [doc.page_content for doc in docs]
42
+ )
43
+
44
+ prompt = f"""
45
+ {SYSTEM_PROMPT}
46
+
47
+ ###Context:
48
+ {context}
49
+
50
+ ###Question:
51
+ {query}
52
+
53
+ Answer:
54
+ """
55
+
56
+ result = pipe(prompt)
57
 
58
+ answer = result[0]["generated_text"]
59
 
60
+ return answer
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt CHANGED
@@ -1,10 +1,10 @@
1
-
2
- gradio
3
- langchain
4
- langchain-community
5
- sentence-transformers
6
- transformers
7
- accelerate
8
- faiss-cpu
9
- pypdf
10
  torch
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  torch
2
+ torchvision
3
+ torchaudio
4
+ transformers==4.39.3
5
+ sentence-transformers==2.7.0
6
+ langchain==0.1.14
7
+ langchain-community==0.0.31
8
+ faiss-cpu==1.9.0.post1
9
+ pypdf==4.2.0
10
+ gradio==4.24.0
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt CHANGED
@@ -7,4 +7,3 @@ langchain==0.1.14
7
  langchain-community==0.0.31
8
  faiss-cpu==1.9.0.post1
9
  pypdf==4.2.0
10
- gradio==4.24.0
 
7
  langchain-community==0.0.31
8
  faiss-cpu==1.9.0.post1
9
  pypdf==4.2.0
 
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py CHANGED
@@ -1,43 +1,84 @@
1
- from transformers import pipeline
 
2
 
3
  from langchain_community.embeddings import HuggingFaceEmbeddings
4
-
5
  from langchain_community.vectorstores import FAISS
6
 
7
  from config import *
8
-
9
  from prompts import SYSTEM_PROMPT
10
 
11
- # Load embedding model
 
 
 
 
 
 
 
 
12
  embeddings = HuggingFaceEmbeddings(
13
  model_name=MODEL_NAME
14
  )
15
 
16
- # Load vector database
17
  vector_db = FAISS.load_local(
18
  VECTOR_DB_PATH,
19
  embeddings,
20
  allow_dangerous_deserialization=True
21
  )
22
 
23
- # Create retriever
24
  retriever = vector_db.as_retriever(
25
- search_kwargs={"k": 3}
26
  )
27
 
28
- # Load LLM
29
- pipe = pipeline(
30
- "text2text-generation",
31
- model=LLM_MODEL,
32
- max_new_tokens=256
33
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def ask_question(query):
36
 
37
- # Retrieve relevant docs
38
  docs = retriever.get_relevant_documents(query)
39
 
40
- context = "\n\n".join(
41
  [doc.page_content for doc in docs]
42
  )
43
 
@@ -53,8 +94,6 @@ def ask_question(query):
53
  Answer:
54
  """
55
 
56
- result = pipe(prompt)
57
-
58
- answer = result[0]["generated_text"]
59
 
60
  return answer
 
1
+ import requests
2
+ import os
3
 
4
  from langchain_community.embeddings import HuggingFaceEmbeddings
 
5
  from langchain_community.vectorstores import FAISS
6
 
7
  from config import *
 
8
  from prompts import SYSTEM_PROMPT
9
 
10
+ HF_TOKEN = os.getenv("HF_API_TOKEN")
11
+
12
+ API_URL = f"https://api-inference.huggingface.co/models/{LLM_MODEL}"
13
+
14
+ headers = {
15
+ "Authorization": f"Bearer {HF_TOKEN}"
16
+ }
17
+
18
+ # Load embeddings
19
  embeddings = HuggingFaceEmbeddings(
20
  model_name=MODEL_NAME
21
  )
22
 
23
+ # Load vector DB
24
  vector_db = FAISS.load_local(
25
  VECTOR_DB_PATH,
26
  embeddings,
27
  allow_dangerous_deserialization=True
28
  )
29
 
 
30
  retriever = vector_db.as_retriever(
31
+ search_kwargs={"k": 2}
32
  )
33
 
34
+ def query_llm(prompt):
35
+
36
+ payload = {
37
+ "inputs": prompt,
38
+ "parameters": {
39
+ "max_new_tokens": 128,
40
+ "temperature": 0.1
41
+ }
42
+ }
43
+
44
+ response = requests.post(
45
+ API_URL,
46
+ headers=headers,
47
+ json=payload,
48
+ timeout=60
49
+ )
50
+
51
+ result = response.json()
52
+
53
+ print(result)
54
+
55
+ # Handle model loading / API errors
56
+ if isinstance(result, dict):
57
+
58
+ if "error" in result:
59
+
60
+ return f"Model Error: {result['error']}"
61
+
62
+ if "estimated_time" in result:
63
+
64
+ return "Model is loading. Please try again in a few seconds."
65
+
66
+ # Handle successful response
67
+ if isinstance(result, list):
68
+
69
+ if len(result) > 0:
70
+
71
+ if "generated_text" in result[0]:
72
+
73
+ return result[0]["generated_text"]
74
+
75
+ return "I couldn't generate a response."
76
 
77
  def ask_question(query):
78
 
 
79
  docs = retriever.get_relevant_documents(query)
80
 
81
+ context = "\n".join(
82
  [doc.page_content for doc in docs]
83
  )
84
 
 
94
  Answer:
95
  """
96
 
97
+ answer = query_llm(prompt)
 
 
98
 
99
  return answer
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/requirements.txt CHANGED
@@ -7,3 +7,4 @@ langchain==0.1.14
7
  langchain-community==0.0.31
8
  faiss-cpu==1.9.0.post1
9
  pypdf==4.2.0
 
 
7
  langchain-community==0.0.31
8
  faiss-cpu==1.9.0.post1
9
  pypdf==4.2.0
10
+ requests
hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/hf-space/rag_pipeline.py CHANGED
@@ -15,21 +15,36 @@ headers = {
15
  "Authorization": f"Bearer {HF_TOKEN}"
16
  }
17
 
18
- # Load embeddings
19
- embeddings = HuggingFaceEmbeddings(
20
- model_name=MODEL_NAME
21
- )
22
-
23
- # Load vector DB
24
- vector_db = FAISS.load_local(
25
- VECTOR_DB_PATH,
26
- embeddings,
27
- allow_dangerous_deserialization=True
28
- )
29
-
30
- retriever = vector_db.as_retriever(
31
- search_kwargs={"k": 2}
32
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  def query_llm(prompt):
35
 
@@ -41,42 +56,62 @@ def query_llm(prompt):
41
  }
42
  }
43
 
44
- response = requests.post(
45
- API_URL,
46
- headers=headers,
47
- json=payload,
48
- timeout=60
49
- )
50
 
51
- result = response.json()
 
 
 
 
 
52
 
53
- print(result)
54
 
55
- # Handle model loading / API errors
56
- if isinstance(result, dict):
57
 
58
- if "error" in result:
59
 
60
- return f"Model Error: {result['error']}"
61
 
62
- if "estimated_time" in result:
63
 
64
- return "Model is loading. Please try again in a few seconds."
65
 
66
- # Handle successful response
67
- if isinstance(result, list):
68
 
69
- if len(result) > 0:
70
 
71
- if "generated_text" in result[0]:
72
 
73
- return result[0]["generated_text"]
74
 
75
- return "I couldn't generate a response."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
 
77
  def ask_question(query):
78
 
79
- docs = retriever.get_relevant_documents(query)
 
 
80
 
81
  context = "\n".join(
82
  [doc.page_content for doc in docs]
 
15
  "Authorization": f"Bearer {HF_TOKEN}"
16
  }
17
 
18
+ # Global cache
19
+ retriever = None
20
+
21
+ def load_vectorstore():
22
+
23
+ global retriever
24
+
25
+ if retriever is None:
26
+
27
+ print("Loading embeddings...")
28
+
29
+ embeddings = HuggingFaceEmbeddings(
30
+ model_name=MODEL_NAME
31
+ )
32
+
33
+ print("Loading vector database...")
34
+
35
+ vector_db = FAISS.load_local(
36
+ VECTOR_DB_PATH,
37
+ embeddings,
38
+ allow_dangerous_deserialization=True
39
+ )
40
+
41
+ retriever = vector_db.as_retriever(
42
+ search_kwargs={"k": 2}
43
+ )
44
+
45
+ print("Retriever ready.")
46
+
47
+ return retriever
48
 
49
  def query_llm(prompt):
50
 
 
56
  }
57
  }
58
 
59
+ try:
 
 
 
 
 
60
 
61
+ response = requests.post(
62
+ API_URL,
63
+ headers=headers,
64
+ json=payload,
65
+ timeout=60
66
+ )
67
 
68
+ if response.status_code != 200:
69
 
70
+ return f"API Error: {response.status_code}"
 
71
 
72
+ try:
73
 
74
+ result = response.json()
75
 
76
+ except Exception:
77
 
78
+ return "The model service is temporarily unavailable. Please try again."
79
 
80
+ print(result)
 
81
 
82
+ if isinstance(result, dict):
83
 
84
+ if "error" in result:
85
 
86
+ return f"Model Error: {result['error']}"
87
 
88
+ if "estimated_time" in result:
89
+
90
+ return "Model is loading. Please try again in a few seconds."
91
+
92
+ if isinstance(result, list):
93
+
94
+ if len(result) > 0:
95
+
96
+ if "generated_text" in result[0]:
97
+
98
+ return result[0]["generated_text"]
99
+
100
+ return "I couldn't generate a response."
101
+
102
+ except requests.exceptions.Timeout:
103
+
104
+ return "The request timed out. Please try again."
105
+
106
+ except Exception as e:
107
+
108
+ return f"Unexpected Error: {str(e)}"
109
 
110
  def ask_question(query):
111
 
112
+ retriever_instance = load_vectorstore()
113
+
114
+ docs = retriever_instance.invoke(query)
115
 
116
  context = "\n".join(
117
  [doc.page_content for doc in docs]
hf-space/hf-space/rag_pipeline.py CHANGED
@@ -5,7 +5,6 @@ from langchain_community.embeddings import HuggingFaceEmbeddings
5
  from langchain_community.vectorstores import FAISS
6
 
7
  from config import *
8
- from prompts import SYSTEM_PROMPT
9
 
10
  HF_TOKEN = os.getenv("HF_API_TOKEN")
11
 
@@ -15,10 +14,9 @@ headers = {
15
  "Authorization": f"Bearer {HF_TOKEN}"
16
  }
17
 
18
- # Global cache
19
  retriever = None
20
 
21
- def load_vectorstore():
22
 
23
  global retriever
24
 
@@ -30,7 +28,7 @@ def load_vectorstore():
30
  model_name=MODEL_NAME
31
  )
32
 
33
- print("Loading vector database...")
34
 
35
  vector_db = FAISS.load_local(
36
  VECTOR_DB_PATH,
@@ -42,7 +40,7 @@ def load_vectorstore():
42
  search_kwargs={"k": 2}
43
  )
44
 
45
- print("Retriever ready.")
46
 
47
  return retriever
48
 
@@ -51,8 +49,7 @@ def query_llm(prompt):
51
  payload = {
52
  "inputs": prompt,
53
  "parameters": {
54
- "max_new_tokens": 128,
55
- "temperature": 0.1
56
  }
57
  }
58
 
@@ -62,54 +59,41 @@ def query_llm(prompt):
62
  API_URL,
63
  headers=headers,
64
  json=payload,
65
- timeout=60
66
  )
67
 
68
- if response.status_code != 200:
69
-
70
- return f"API Error: {response.status_code}"
71
 
72
- try:
73
-
74
- result = response.json()
75
 
76
- except Exception:
77
 
78
- return "The model service is temporarily unavailable. Please try again."
79
 
80
  print(result)
81
 
82
- if isinstance(result, dict):
83
-
84
- if "error" in result:
85
-
86
- return f"Model Error: {result['error']}"
87
-
88
- if "estimated_time" in result:
89
-
90
- return "Model is loading. Please try again in a few seconds."
91
-
92
  if isinstance(result, list):
93
 
94
- if len(result) > 0:
95
-
96
- if "generated_text" in result[0]:
 
97
 
98
- return result[0]["generated_text"]
99
 
100
- return "I couldn't generate a response."
101
 
102
- except requests.exceptions.Timeout:
103
 
104
- return "The request timed out. Please try again."
105
 
106
  except Exception as e:
107
 
108
- return f"Unexpected Error: {str(e)}"
109
 
110
  def ask_question(query):
111
 
112
- retriever_instance = load_vectorstore()
113
 
114
  docs = retriever_instance.invoke(query)
115
 
@@ -118,17 +102,13 @@ def ask_question(query):
118
  )
119
 
120
  prompt = f"""
121
- {SYSTEM_PROMPT}
122
 
123
- ###Context:
124
  {context}
125
 
126
- ###Question:
127
  {query}
128
-
129
- Answer:
130
  """
131
 
132
- answer = query_llm(prompt)
133
-
134
- return answer
 
5
  from langchain_community.vectorstores import FAISS
6
 
7
  from config import *
 
8
 
9
  HF_TOKEN = os.getenv("HF_API_TOKEN")
10
 
 
14
  "Authorization": f"Bearer {HF_TOKEN}"
15
  }
16
 
 
17
  retriever = None
18
 
19
+ def load_retriever():
20
 
21
  global retriever
22
 
 
28
  model_name=MODEL_NAME
29
  )
30
 
31
+ print("Loading FAISS index...")
32
 
33
  vector_db = FAISS.load_local(
34
  VECTOR_DB_PATH,
 
40
  search_kwargs={"k": 2}
41
  )
42
 
43
+ print("Retriever loaded.")
44
 
45
  return retriever
46
 
 
49
  payload = {
50
  "inputs": prompt,
51
  "parameters": {
52
+ "max_new_tokens": 64
 
53
  }
54
  }
55
 
 
59
  API_URL,
60
  headers=headers,
61
  json=payload,
62
+ timeout=20
63
  )
64
 
65
+ print("STATUS:", response.status_code)
 
 
66
 
67
+ if response.status_code != 200:
 
 
68
 
69
+ return f"HF API Error: {response.status_code}"
70
 
71
+ result = response.json()
72
 
73
  print(result)
74
 
 
 
 
 
 
 
 
 
 
 
75
  if isinstance(result, list):
76
 
77
+ return result[0].get(
78
+ "generated_text",
79
+ "No response generated."
80
+ )
81
 
82
+ if isinstance(result, dict):
83
 
84
+ if "error" in result:
85
 
86
+ return result["error"]
87
 
88
+ return str(result)
89
 
90
  except Exception as e:
91
 
92
+ return f"ERROR: {str(e)}"
93
 
94
  def ask_question(query):
95
 
96
+ retriever_instance = load_retriever()
97
 
98
  docs = retriever_instance.invoke(query)
99
 
 
102
  )
103
 
104
  prompt = f"""
105
+ Answer the question using ONLY the context below.
106
 
107
+ Context:
108
  {context}
109
 
110
+ Question:
111
  {query}
 
 
112
  """
113
 
114
+ return query_llm(prompt)