Namanj46 commited on
Commit
05950f9
·
verified ·
1 Parent(s): 0343bee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -6
app.py CHANGED
@@ -9,6 +9,7 @@ from llama_index.embeddings.openai import OpenAIEmbedding
9
  from llama_index.core.postprocessor import MetadataReplacementPostProcessor
10
  from llama_index.core.node_parser import SentenceWindowNodeParser
11
  from dotenv import load_dotenv
 
12
 
13
  # Load environment variables
14
  load_dotenv("config.env")
@@ -77,6 +78,18 @@ Settings.node_parser = sentence_node_parser
77
  # Create index
78
  index = VectorStoreIndex.from_documents(documents)
79
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  # Custom reranking function
81
  def custom_rerank(nodes, query):
82
  rerank_prompt = (
@@ -108,7 +121,7 @@ query_engine = index.as_query_engine(
108
  ],
109
  )
110
 
111
- # Chatbot function
112
  def chatbot(message, history):
113
  history_text = "\n".join([f"Human: {h[0]}\nAI: {h[1]}" for h in history])
114
  full_query = f"Given the following chat history:\n{history_text}\n\nHuman: {message}\nAI:"
@@ -121,20 +134,29 @@ def chatbot(message, history):
121
 
122
  # Synthesize answer from reranked nodes
123
  context = "\n".join([node.get_content() for node in reranked_nodes])
 
 
124
  response = llm.complete(
125
  f"Using the following context, answer the query:\n\nContext: {context}\n\nQuery: {full_query}"
126
  )
127
 
128
- return response.text
 
 
 
129
 
130
- # Create Gradio interface
131
  iface = gr.ChatInterface(
132
  chatbot,
133
- title="Resume Chatbot",
134
- description="Ask questions about resumes in the database.",
 
 
 
 
135
  theme="soft",
136
  examples=[
137
- "Out of all the resumes tell me three of them who have experience in SQL?",
138
  "Give me key summary takeaways of the resumes who have experience in Project Management?",
139
  "Give me the names of 10 candidates who have more than two years of experience in general?",
140
  ],
 
9
  from llama_index.core.postprocessor import MetadataReplacementPostProcessor
10
  from llama_index.core.node_parser import SentenceWindowNodeParser
11
  from dotenv import load_dotenv
12
+ import re
13
 
14
  # Load environment variables
15
  load_dotenv("config.env")
 
78
  # Create index
79
  index = VectorStoreIndex.from_documents(documents)
80
 
81
+ # Function to filter PII from text (removes emails, phone numbers, etc.)
82
+ def filter_pii(text):
83
+ # Regular expressions for email, phone numbers, and sensitive patterns
84
+ email_pattern = r"\S+@\S+\.\S+"
85
+ phone_pattern = r"\+?\d[\d\s()-]{8,}\d"
86
+
87
+ # Replace found patterns with [REDACTED]
88
+ text = re.sub(email_pattern, "[REDACTED]", text)
89
+ text = re.sub(phone_pattern, "[REDACTED]", text)
90
+
91
+ return text
92
+
93
  # Custom reranking function
94
  def custom_rerank(nodes, query):
95
  rerank_prompt = (
 
121
  ],
122
  )
123
 
124
+ # Chatbot function with PII filter
125
  def chatbot(message, history):
126
  history_text = "\n".join([f"Human: {h[0]}\nAI: {h[1]}" for h in history])
127
  full_query = f"Given the following chat history:\n{history_text}\n\nHuman: {message}\nAI:"
 
134
 
135
  # Synthesize answer from reranked nodes
136
  context = "\n".join([node.get_content() for node in reranked_nodes])
137
+
138
+ # Get the response from LLM
139
  response = llm.complete(
140
  f"Using the following context, answer the query:\n\nContext: {context}\n\nQuery: {full_query}"
141
  )
142
 
143
+ # Filter PII from the response
144
+ filtered_response = filter_pii(response.text)
145
+
146
+ return filtered_response
147
 
148
+ # Create Gradio interface with comprehensive instructions
149
  iface = gr.ChatInterface(
150
  chatbot,
151
+ title="Resume Chatbot - Secure Candidate Query",
152
+ description=(
153
+ "This is a Resume Chatbot that answers questions about candidate experience and qualifications. "
154
+ "It will not reveal any private information beyond names of candidates. Please ask questions about skills, "
155
+ "experience, or qualifications without requesting sensitive personal information."
156
+ ),
157
  theme="soft",
158
  examples=[
159
+ "Out of all the resumes, tell me three of them who have experience in SQL?",
160
  "Give me key summary takeaways of the resumes who have experience in Project Management?",
161
  "Give me the names of 10 candidates who have more than two years of experience in general?",
162
  ],