Upload 3 files
Browse files- app.py +54 -0
- gdpr_logic.py +51 -0
- requirements.txt +9 -0
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gdpr_app import ask_question, clear_chat, create_quick_question_handler
|
| 3 |
+
|
| 4 |
+
custom_css = """
|
| 5 |
+
.gradio-container {
|
| 6 |
+
max-width: 1600px !important;
|
| 7 |
+
margin: auto !important;
|
| 8 |
+
padding-left: 30px !important;
|
| 9 |
+
padding-right: 30px !important;
|
| 10 |
+
}
|
| 11 |
+
#quick-buttons {
|
| 12 |
+
font-size: 18px !important;
|
| 13 |
+
margin-top: 10px;
|
| 14 |
+
}
|
| 15 |
+
"""
|
| 16 |
+
|
| 17 |
+
# Gradio application
|
| 18 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 19 |
+
gr.Markdown(
|
| 20 |
+
"""
|
| 21 |
+
<h1 style="text-align: center;">GDPR Compliance Assistant</h1>
|
| 22 |
+
<p style="text-align: center;">
|
| 23 |
+
This AI-based assistant helps you navigate GDPR articles with ease.
|
| 24 |
+
Simply ask a question or choose from the quick questions below.
|
| 25 |
+
</p>
|
| 26 |
+
"""
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
with gr.Row():
|
| 30 |
+
chatbot = gr.Chatbot(label="GDPR Assistant")
|
| 31 |
+
quick_buttons = gr.Buttons(["What is GDPR?", "What rights do I have?"])
|
| 32 |
+
|
| 33 |
+
prompt_text = gr.Textbox(lines=2, placeholder="Ask a GDPR-related question...")
|
| 34 |
+
send_button = gr.Button(value="Send", elem_id="send-button")
|
| 35 |
+
clear_button = gr.Button(value="Clear Chat", elem_id="clear-button")
|
| 36 |
+
|
| 37 |
+
send_button.click(
|
| 38 |
+
fn=ask_question,
|
| 39 |
+
inputs=[prompt_text, chatbot],
|
| 40 |
+
outputs=[chatbot]
|
| 41 |
+
)
|
| 42 |
+
clear_button.click(
|
| 43 |
+
fn=clear_chat,
|
| 44 |
+
inputs=[chatbot],
|
| 45 |
+
outputs=[chatbot]
|
| 46 |
+
)
|
| 47 |
+
quick_buttons.click(
|
| 48 |
+
fn=create_quick_question_handler,
|
| 49 |
+
inputs=[chatbot],
|
| 50 |
+
outputs=[prompt_text]
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
gdpr_logic.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 2 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 3 |
+
import tempfile
|
| 4 |
+
import requests
|
| 5 |
+
from typing import List, Dict, Optional
|
| 6 |
+
|
| 7 |
+
# Add the following functions and the class from GDPR_chat.py
|
| 8 |
+
# ...
|
| 9 |
+
# Existing Functions:
|
| 10 |
+
# - create_session
|
| 11 |
+
# - download_page
|
| 12 |
+
# - get_article_links
|
| 13 |
+
# - clean_text
|
| 14 |
+
# - find_article_heading
|
| 15 |
+
# - is_stop_heading
|
| 16 |
+
# - extract_article_content
|
| 17 |
+
# - extract_single_article
|
| 18 |
+
# - save_cache
|
| 19 |
+
# - load_cache
|
| 20 |
+
# - scrape_all_articles
|
| 21 |
+
# - load_gdpr_data
|
| 22 |
+
# - initialize_data
|
| 23 |
+
# - refresh_data
|
| 24 |
+
# - build_context
|
| 25 |
+
# - generate_answer
|
| 26 |
+
# - format_sources
|
| 27 |
+
# - ask_question
|
| 28 |
+
# - clear_chat
|
| 29 |
+
# - create_quick_question_handler
|
| 30 |
+
|
| 31 |
+
# Existing Class:
|
| 32 |
+
# - GDPRSearchIndex (with its methods and attributes)
|
| 33 |
+
|
| 34 |
+
# Example for clarity:
|
| 35 |
+
def load_gdpr_data():
|
| 36 |
+
"""Load GDPR data from the cache or regenerate if needed."""
|
| 37 |
+
# Implementation here
|
| 38 |
+
pass
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class GDPRSearchIndex:
|
| 42 |
+
"""Class representing the GDPR search index."""
|
| 43 |
+
def __init__(self, articles: List[Dict[str, str]]):
|
| 44 |
+
self.articles = articles
|
| 45 |
+
# Implementation here
|
| 46 |
+
def search(self, query: str) -> List[Dict[str, str]]:
|
| 47 |
+
# Implementation here
|
| 48 |
+
pass
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# Add all the other relevant backend logic functions.
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=5.0
|
| 2 |
+
torch>=2.3
|
| 3 |
+
transformers>=4.45
|
| 4 |
+
accelerate>=0.33
|
| 5 |
+
sentencepiece
|
| 6 |
+
requests>=2.32
|
| 7 |
+
beautifulsoup4>=4.12
|
| 8 |
+
scikit-learn>=1.5
|
| 9 |
+
lxml
|