anmol11p commited on
Commit
4118ef0
·
verified ·
1 Parent(s): 788163d

fix input handling

Browse files

Enhancements to Input Handling and Report Generation in Anupalan Karta Compliance Checker This contribution improves the usability, robustness, and performance of the app by: ✅ Fixing PDF Upload Bugs Ensures multi-page PDFs are parsed correctly and avoids issues with NoneType returns from empty pages. 🌐 Improved Error Handling for URL Input Adds user-facing warnings for unreachable or invalid public policy URLs, improving clarity. 🧠 Prevents Duplicate AI Report Generation Adds a session-based state (ai_report_generated) to avoid unnecessary re-calls to the API, saving resources. 🧹 Cleaner UI & Sidebar Organization Streamlines the input mode options using st.sidebar and fixes misleading behaviors (e.g., fallback to warnings). 🧪 Ensures Accurate Framework Selection Connects selectedFw properly to run_check, allowing accurate filtering of results.

Files changed (1) hide show
  1. src/app.py +101 -62
src/app.py CHANGED
@@ -1,17 +1,12 @@
1
  import streamlit as st
2
- from compliance_lib import fetch_text, run_check, generate_report, RULES
3
- import textwrap, json, datetime, os
4
-
5
- st.set_page_config(page_title="Anupalan Karta – Compliance Checker",
6
  layout="wide")
7
-
8
- st.title("🛡️ Anupalan Karta (अनुपालंकर्ता)")
9
-
10
- st.subheader("Unified compliance self-check Tool")
11
-
12
- # --- introduction and how-to guide ------------------------------------------
13
  st.markdown("""
14
- ## Introduction
15
 
16
  **Anupalan Karta** (अनुपालंकर्ता) is a simple tool to help business owners, data architects, and data owners quickly check if their policies and procedures meet important regulations like GDPR, EU AI Act, and ISO 27001.
17
 
@@ -45,58 +40,102 @@ Visit [anktechsol.com](https://anktechsol.com) for professional compliance consu
45
 
46
  ---
47
  """)
48
- # --- sidebar ---------------------------------------------------------------
 
 
 
 
 
 
49
  with st.sidebar:
50
- st.header("📑 Input options")
51
- mode = st.radio("Choose data source:",
52
- ("Paste text", "URL of public policy", "Upload file"))
53
- if mode == "Paste text":
54
- raw_text = st.text_area("Paste your policy / procedures here")
55
- elif mode == "URL of public policy":
56
- url = st.text_input("Public URL (HTTPS)")
57
- raw_text = fetch_text(url) if url else ""
58
- else:
59
- up = st.file_uploader("Upload .txt, .md or .pdf", type=["txt", "md", "pdf"])
60
- raw_text = up.read().decode("utf-8", errors="ignore") if up else ""
61
-
62
- st.markdown("---")
63
- selected_fw = st.multiselect(
64
- "Frameworks to check",
65
- list(RULES.keys()),
66
- default=list(RULES.keys())
67
- )
68
- run_btn = st.button("Run compliance check")
69
-
70
- # --- main body --------------------------------------------------------------
71
- if run_btn and raw_text.strip():
72
- with st.spinner("Running rule-based checks…"):
73
- results = run_check(raw_text)
74
- st.subheader("📊 Checklist results")
75
- for fw in selected_fw:
76
- passed = sum(1 for _, ok in results[fw] if ok)
77
- total = len(results[fw])
78
- st.write(f"**{fw}: {passed}/{total} items passed**")
79
- st.progress(passed / total)
80
- for label, ok in results[fw]:
81
- st.write(("✅" if ok else "❌") + " " + label)
 
 
 
 
 
 
82
  st.markdown("---")
83
 
84
- # --- AI report section --------------------------------------------------
85
- st.subheader("📝 Generate narrative report")
86
- if st.button("Generate AI report"):
87
- with st.spinner("Calling model… this may take ~30 s"):
88
- bullet = "\n".join(
89
- f"- {fw}: {sum(ok for _, ok in results[fw])}/{len(results[fw])} passed"
90
- for fw in selected_fw
91
- )
92
- from config import AI_REPORT_PROMPT # <-- import the prompt template
93
- prompt = AI_REPORT_PROMPT.format(bullet=bullet)
94
- report = generate_report(prompt)
95
- st.markdown("#### Draft report")
96
- st.code(report, language="markdown")
97
- st.download_button("⬇️ Download .md",
98
- report.encode("utf-8"),
99
- file_name="anupalan_karta_report.md",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  mime="text/markdown")
101
- else:
102
- st.info("Awaiting input…")
 
 
1
  import streamlit as st
2
+ from compliance_lib import *
3
+ import PyPDF2 #
4
+ st.set_page_config(page_title="AP - Anupalan Karta – Compliance Checker",
 
5
  layout="wide")
6
+ st.title('🛡️ Anupalan Karta (अनुपालंकर्ता)')
7
+ st.subheader('Unified compliance self-check Tool')
 
 
 
 
8
  st.markdown("""
9
+ ## Introduction
10
 
11
  **Anupalan Karta** (अनुपालंकर्ता) is a simple tool to help business owners, data architects, and data owners quickly check if their policies and procedures meet important regulations like GDPR, EU AI Act, and ISO 27001.
12
 
 
40
 
41
  ---
42
  """)
43
+
44
+ # default state
45
+ if "check_run" not in st.session_state:
46
+ st.session_state.check_run = False
47
+ st.session_state.raw_text = ""
48
+
49
+ # ------ side bar ----
50
  with st.sidebar:
51
+ st.subheader('📥 Input options')
52
+ option = st.radio('Choose data source:',['Paste text','URL of public policy','Upload file'])
53
+ raw_text=""
54
+ if(option=='Paste text'):
55
+ raw_text= st.text_area('Paste your policy / procedures here')
56
+
57
+
58
+ elif option == 'URL of public policy':
59
+ Url = st.text_area("Public URL (HTTPS)", key="input_text")
60
+ raw_text = fetchText(Url) if Url else st.warning
61
+ if Url:
62
+ raw_text, error = fetchText(Url)
63
+ if error:
64
+ st.warning(error)
65
+ else:
66
+ raw_text = ""
67
+ # print(raw_text)
68
+ else:
69
+ fileUpload = st.file_uploader("Upload .txt, .md or .pdf", ['txt', 'md', 'pdf'])
70
+ if fileUpload:
71
+ file_type = fileUpload.name.split('.')[-1].lower()
72
+
73
+ if file_type in ["txt", "md"]:
74
+ raw_text = fileUpload.read().decode("utf-8", errors="ignore")
75
+
76
+ elif file_type=="pdf":
77
+ try:
78
+ pdf_reader = PyPDF2.PdfReader(fileUpload)
79
+ for page in pdf_reader.pages:
80
+ raw_text += page.extract_text() or ""
81
+ except Exception as e:
82
+ st.error(f"Error reading PDF: {e}")
83
+ if raw_text and st.session_state.get("last_input") != raw_text:
84
+ st.session_state.check_run = False
85
+ st.session_state.last_input = raw_text
86
+
87
+ # "Framework to Check", ["GDPR", "EU_AI_ACT", "ISO_27001"]
88
+ selectedFw=st.multiselect('Framework to Check',list(RULES.keys()),default=list(RULES.keys()))
89
  st.markdown("---")
90
 
91
+
92
+
93
+ # ------- side bar end -----
94
+
95
+ # <====== main code =====>
96
+ # strip removes all the leading and trailing whitespace characters from a stringif run_btn and raw_text.strip():
97
+ if "results" not in st.session_state:
98
+ st.session_state.results = None
99
+ st.session_state.selectedFw=[]
100
+
101
+ if st.sidebar.button("Run Compliance Check") and raw_text.strip():
102
+ # reset_results()
103
+ with st.spinner("Running rule-based checks…."):
104
+ st.session_state.results=run_check(raw_text, selectedFw)
105
+ st.session_state.selectedFw=selectedFw
106
+ st.session_state.check_run=True
107
+ if st.session_state.get("check_run"):
108
+ results = st.session_state.results
109
+ selectedFw = st.session_state.selectedFw
110
+ # 📊 Checklist results
111
+ st.subheader('📊 Checklist results')
112
+ for fw in selectedFw:
113
+ # Count how many rules passed for that framework
114
+ passed = sum(1 for _, ok in results[fw] if ok) # _ is unused, ok is True/False
115
+ total = len(results[fw])
116
+
117
+ st.write(f"**{fw}: {passed}/{total} items passed**") # Example: EU_AI_ACT: 0/2 items passed
118
+ st.progress(passed / total)
119
+
120
+ for label, ok in results[fw]:
121
+ st.write(("✅" if ok else "❌") + " " + label) # Example: ❌ Lawful basis documented
122
+ st.markdown("---")
123
+
124
+ # 📝 Generate narrative report
125
+
126
+ st.subheader('📝 Generate narrative report')
127
+ if st.button('Generate AI report'):
128
+ with st.spinner("Calling model… this may take ~30 s"):
129
+ bullet = "\n".join(
130
+ f"- {fw}: {sum(ok for _, ok in results[fw])}/{len(results[fw])} passed"
131
+ for fw in selectedFw
132
+ )
133
+ prompt=AI_REPORT_PROMPT.format(bullet=bullet)
134
+ report=generate_report(prompt)
135
+ st.markdown("#### Draft report")
136
+ st.code(report, language="markdown")
137
+ st.download_button("⬇️ Download .md",report.encode('utf-8'),file_name="anupalan_karta_report.md",
138
  mime="text/markdown")
139
+ else:
140
+ st.info("Awaiting input…")
141
+