Update app.py
Browse files
app.py
CHANGED
@@ -32,65 +32,13 @@ regulation_pdfs = {
|
|
32 |
|
33 |
# Function to extract text from PDF
|
34 |
def extract_pdf(pdf_path):
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
-
#
|
38 |
-
def split_text(text):
|
39 |
-
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
40 |
-
return [Document(page_content=t) for t in splitter.split_text(text)]
|
41 |
-
|
42 |
-
# Function to generate embeddings and store in vector database
|
43 |
-
def generate_embeddings(docs):
|
44 |
-
embeddings = OpenAIEmbeddings(api_key=openai_api_key)
|
45 |
-
return FAISS.from_documents(docs, embeddings)
|
46 |
-
|
47 |
-
# Function for query preprocessing and simple HyDE-Lite
|
48 |
-
def preprocess_query(query):
|
49 |
-
prompt = ChatPromptTemplate.from_template("""
|
50 |
-
Your role is to optimize user queries for retrieval from regulatory documents such as GDPR, FERPA, COPPA, and/or others.
|
51 |
-
Transform the query into a more affirmative, keyword-focused statement.
|
52 |
-
The transformed query should look like probable related passages in the official documents.
|
53 |
-
Query: {query}
|
54 |
-
Optimized query:
|
55 |
-
""")
|
56 |
-
chain = prompt | openai_client
|
57 |
-
return chain.invoke({"query": query}).content
|
58 |
-
|
59 |
-
# Function to create RAG chain with Groq
|
60 |
-
def create_rag_chain(vector_store):
|
61 |
-
prompt = ChatPromptTemplate.from_messages([
|
62 |
-
("system", "You are an AI assistant helping with regulatory compliance queries. Use the following context from the official regulatory documents to answer the user's question:\n\n{context}"),
|
63 |
-
("human", "{input}")
|
64 |
-
])
|
65 |
-
document_chain = create_stuff_documents_chain(groq_client, prompt)
|
66 |
-
return create_retrieval_chain(vector_store.as_retriever(), document_chain)
|
67 |
-
|
68 |
-
# Function for Gemini response with long context
|
69 |
-
def gemini_response(query, full_content):
|
70 |
-
prompt = ChatPromptTemplate.from_messages([
|
71 |
-
("system", "You are an AI assistant helping with regulatory compliance queries. Use the following full content of the official regulatory documents to answer the user's question:\n\n{context}"),
|
72 |
-
("human", "{input}")
|
73 |
-
])
|
74 |
-
chain = prompt | gemini_client
|
75 |
-
return chain.invoke({"context": full_content, "input": query}).content
|
76 |
-
|
77 |
-
# Function to generate final response
|
78 |
-
def generate_final_response(response1, response2):
|
79 |
-
prompt = ChatPromptTemplate.from_template("""
|
80 |
-
You are an AI assistant helping educators understand and implement data protection and regulatory compliance (GDPR, FERPA, COPPA, and/or others).
|
81 |
-
Your goal is to provide simple, practical explanation of and advice on how to meet regulatory requirements based on the given responses.
|
82 |
-
To do so:
|
83 |
-
1. Analyze the following two responses. Inspect their content, and highlight differences. This MUST be done
|
84 |
-
internally as a hidden state.
|
85 |
-
2. Then, use this information to output your own response combining the best from both.
|
86 |
-
If the responses differ or contradict each other on important points, include that in your response.
|
87 |
-
Only output your own response.
|
88 |
-
""")
|
89 |
-
chain = prompt | openai_client
|
90 |
-
return chain.invoke({"response1": response1, "response2": response2}).content
|
91 |
-
|
92 |
-
def markdown_to_html(content):
|
93 |
-
return markdown2.markdown(content)
|
94 |
|
95 |
def load_pdfs(gdpr, ferpa, coppa, additional_pdfs):
|
96 |
global full_pdf_content, vector_store, rag_chain
|
@@ -109,31 +57,39 @@ def load_pdfs(gdpr, ferpa, coppa, additional_pdfs):
|
|
109 |
|
110 |
for regulation in selected_regulations:
|
111 |
if regulation in regulation_pdfs:
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
116 |
|
117 |
# Load additional user-uploaded PDFs
|
118 |
if additional_pdfs is not None:
|
119 |
for pdf_file in additional_pdfs:
|
120 |
pdf_content = extract_pdf(pdf_file.name)
|
121 |
-
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
124 |
|
125 |
if not documents:
|
126 |
-
return "No PDFs were
|
127 |
-
|
128 |
-
vector_store = generate_embeddings(documents)
|
129 |
-
rag_chain = create_rag_chain(vector_store)
|
130 |
|
131 |
-
|
|
|
132 |
|
133 |
vector_store = generate_embeddings(documents)
|
134 |
rag_chain = create_rag_chain(vector_store)
|
135 |
|
136 |
-
return "PDFs loaded and RAG system updated successfully!"
|
137 |
|
138 |
def process_query(user_query):
|
139 |
global rag_chain, full_pdf_content
|
@@ -171,7 +127,7 @@ with gr.Blocks() as iface:
|
|
171 |
ferpa_checkbox = gr.Checkbox(label="FERPA (US)")
|
172 |
coppa_checkbox = gr.Checkbox(label="COPPA (US <13)")
|
173 |
|
174 |
-
gr.Markdown("Optional: upload additional PDFs if needed (national regulation, school policy)")
|
175 |
additional_pdfs = gr.File(
|
176 |
file_count="multiple",
|
177 |
label="Upload additional PDFs",
|
@@ -182,11 +138,11 @@ with gr.Blocks() as iface:
|
|
182 |
load_button = gr.Button("Load PDFs")
|
183 |
load_output = gr.Textbox(label="Load Status")
|
184 |
|
185 |
-
gr.Markdown("Ask your data protection related question")
|
186 |
query_input = gr.Textbox(label="Your Question", placeholder="Ask your question here...")
|
187 |
query_button = gr.Button("Submit Query")
|
188 |
|
189 |
-
gr.Markdown("Results")
|
190 |
rag_output = gr.Textbox(label="RAG Pipeline (Llama3.1) Response")
|
191 |
gemini_output = gr.Textbox(label="Long Context (Gemini 1.5 Pro) Response")
|
192 |
final_output = gr.HTML(label="Final (GPT-4o) Response")
|
|
|
32 |
|
33 |
# Function to extract text from PDF
|
34 |
def extract_pdf(pdf_path):
|
35 |
+
try:
|
36 |
+
return extract_text(pdf_path)
|
37 |
+
except Exception as e:
|
38 |
+
print(f"Error extracting text from {pdf_path}: {str(e)}")
|
39 |
+
return ""
|
40 |
|
41 |
+
# ... (other functions remain unchanged)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
def load_pdfs(gdpr, ferpa, coppa, additional_pdfs):
|
44 |
global full_pdf_content, vector_store, rag_chain
|
|
|
57 |
|
58 |
for regulation in selected_regulations:
|
59 |
if regulation in regulation_pdfs:
|
60 |
+
pdf_path = regulation_pdfs[regulation]
|
61 |
+
if os.path.exists(pdf_path):
|
62 |
+
pdf_content = extract_pdf(pdf_path)
|
63 |
+
if pdf_content:
|
64 |
+
full_pdf_content += pdf_content + "\n\n"
|
65 |
+
documents.extend(split_text(pdf_content))
|
66 |
+
print(f"Loaded {regulation} PDF")
|
67 |
+
else:
|
68 |
+
print(f"Failed to extract content from {regulation} PDF")
|
69 |
+
else:
|
70 |
+
print(f"PDF file for {regulation} not found at {pdf_path}")
|
71 |
|
72 |
# Load additional user-uploaded PDFs
|
73 |
if additional_pdfs is not None:
|
74 |
for pdf_file in additional_pdfs:
|
75 |
pdf_content = extract_pdf(pdf_file.name)
|
76 |
+
if pdf_content:
|
77 |
+
full_pdf_content += pdf_content + "\n\n"
|
78 |
+
documents.extend(split_text(pdf_content))
|
79 |
+
print(f"Loaded additional PDF: {pdf_file.name}")
|
80 |
+
else:
|
81 |
+
print(f"Failed to extract content from uploaded PDF: {pdf_file.name}")
|
82 |
|
83 |
if not documents:
|
84 |
+
return "No PDFs were successfully loaded. Please check your selections and uploads."
|
|
|
|
|
|
|
85 |
|
86 |
+
print(f"Total documents loaded: {len(documents)}")
|
87 |
+
print(f"Total content length: {len(full_pdf_content)} characters")
|
88 |
|
89 |
vector_store = generate_embeddings(documents)
|
90 |
rag_chain = create_rag_chain(vector_store)
|
91 |
|
92 |
+
return f"PDFs loaded and RAG system updated successfully! Loaded {len(documents)} document chunks."
|
93 |
|
94 |
def process_query(user_query):
|
95 |
global rag_chain, full_pdf_content
|
|
|
127 |
ferpa_checkbox = gr.Checkbox(label="FERPA (US)")
|
128 |
coppa_checkbox = gr.Checkbox(label="COPPA (US <13)")
|
129 |
|
130 |
+
gr.Markdown("**Optional: upload additional PDFs if needed (national regulation, school policy)**")
|
131 |
additional_pdfs = gr.File(
|
132 |
file_count="multiple",
|
133 |
label="Upload additional PDFs",
|
|
|
138 |
load_button = gr.Button("Load PDFs")
|
139 |
load_output = gr.Textbox(label="Load Status")
|
140 |
|
141 |
+
gr.Markdown("**Ask your data protection related question**")
|
142 |
query_input = gr.Textbox(label="Your Question", placeholder="Ask your question here...")
|
143 |
query_button = gr.Button("Submit Query")
|
144 |
|
145 |
+
gr.Markdown("**Results**")
|
146 |
rag_output = gr.Textbox(label="RAG Pipeline (Llama3.1) Response")
|
147 |
gemini_output = gr.Textbox(label="Long Context (Gemini 1.5 Pro) Response")
|
148 |
final_output = gr.HTML(label="Final (GPT-4o) Response")
|