Update app.py
Browse files
app.py
CHANGED
|
@@ -9,14 +9,15 @@ rewriter = pipeline(
|
|
| 9 |
"text2text-generation",
|
| 10 |
model="google/flan-t5-large",
|
| 11 |
tokenizer="google/flan-t5-large",
|
| 12 |
-
device_map="auto", # runs on CPU if no GPU;
|
| 13 |
)
|
| 14 |
|
| 15 |
def clean(txt: str) -> str:
|
| 16 |
-
"""Strip non‑Latin‑1 chars
|
| 17 |
txt = (
|
| 18 |
txt.replace("“", '"').replace("”", '"')
|
| 19 |
.replace("’", "'").replace("‘", "'")
|
|
|
|
| 20 |
)
|
| 21 |
return unicodedata.normalize("NFKD", txt).encode("latin1", "ignore").decode("latin1")
|
| 22 |
|
|
@@ -33,6 +34,7 @@ QUESTIONS = [
|
|
| 33 |
]
|
| 34 |
|
| 35 |
# ---------------- 3 | Rule‑based analytics ----------------
|
|
|
|
| 36 |
def analyse(resp):
|
| 37 |
findings, actions = [], []
|
| 38 |
|
|
@@ -70,6 +72,7 @@ def analyse(resp):
|
|
| 70 |
return findings, actions
|
| 71 |
|
| 72 |
# ---------------- 4 | Prompt builder ----------------
|
|
|
|
| 73 |
def prompt_from(resp):
|
| 74 |
findings, actions = analyse(resp)
|
| 75 |
bullets_f = "\n".join(f"- {x}" for x in findings)
|
|
@@ -88,11 +91,13 @@ Use clear, concise, professional language.
|
|
| 88 |
"""
|
| 89 |
|
| 90 |
# ---------------- 5 | PDF helper ----------------
|
|
|
|
| 91 |
def to_pdf(text: str, company: str):
|
| 92 |
pdf = FPDF()
|
| 93 |
pdf.add_page()
|
| 94 |
pdf.set_font("Arial", "B", 14)
|
| 95 |
-
|
|
|
|
| 96 |
pdf.ln(5)
|
| 97 |
pdf.set_font("Arial", "", 11)
|
| 98 |
pdf.multi_cell(0, 8, clean(text))
|
|
@@ -101,8 +106,9 @@ def to_pdf(text: str, company: str):
|
|
| 101 |
return tmp.name
|
| 102 |
|
| 103 |
# ---------------- 6 | Gradio UI ----------------
|
|
|
|
| 104 |
with gr.Blocks(title="AI Compliance & Governance Report") as demo:
|
| 105 |
-
gr.Markdown("## 🛡️
|
| 106 |
|
| 107 |
widgets = {}
|
| 108 |
for q in QUESTIONS:
|
|
@@ -123,6 +129,7 @@ with gr.Blocks(title="AI Compliance & Governance Report") as demo:
|
|
| 123 |
out_pdf = gr.File(label="Download PDF")
|
| 124 |
|
| 125 |
# ---------------- 7 | Callback ----------------
|
|
|
|
| 126 |
def run(*vals):
|
| 127 |
data = dict(zip(widgets.keys(), vals))
|
| 128 |
prompt = prompt_from(data)
|
|
|
|
| 9 |
"text2text-generation",
|
| 10 |
model="google/flan-t5-large",
|
| 11 |
tokenizer="google/flan-t5-large",
|
| 12 |
+
device_map="auto", # runs on CPU if no GPU; auto‑places on GPU if available
|
| 13 |
)
|
| 14 |
|
| 15 |
def clean(txt: str) -> str:
|
| 16 |
+
"""Strip non‑Latin‑1 chars (curly quotes, NB‑hyphens, etc.) so FPDF doesn't choke."""
|
| 17 |
txt = (
|
| 18 |
txt.replace("“", '"').replace("”", '"')
|
| 19 |
.replace("’", "'").replace("‘", "'")
|
| 20 |
+
.replace("–", "-").replace("—", "-").replace("‑", "-") # en/em/non‑break hyphens → ASCII hyphen
|
| 21 |
)
|
| 22 |
return unicodedata.normalize("NFKD", txt).encode("latin1", "ignore").decode("latin1")
|
| 23 |
|
|
|
|
| 34 |
]
|
| 35 |
|
| 36 |
# ---------------- 3 | Rule‑based analytics ----------------
|
| 37 |
+
|
| 38 |
def analyse(resp):
|
| 39 |
findings, actions = [], []
|
| 40 |
|
|
|
|
| 72 |
return findings, actions
|
| 73 |
|
| 74 |
# ---------------- 4 | Prompt builder ----------------
|
| 75 |
+
|
| 76 |
def prompt_from(resp):
|
| 77 |
findings, actions = analyse(resp)
|
| 78 |
bullets_f = "\n".join(f"- {x}" for x in findings)
|
|
|
|
| 91 |
"""
|
| 92 |
|
| 93 |
# ---------------- 5 | PDF helper ----------------
|
| 94 |
+
|
| 95 |
def to_pdf(text: str, company: str):
|
| 96 |
pdf = FPDF()
|
| 97 |
pdf.add_page()
|
| 98 |
pdf.set_font("Arial", "B", 14)
|
| 99 |
+
header = clean(f"Compliance Report - {company}") # clean company string too
|
| 100 |
+
pdf.multi_cell(0, 10, header, align="C")
|
| 101 |
pdf.ln(5)
|
| 102 |
pdf.set_font("Arial", "", 11)
|
| 103 |
pdf.multi_cell(0, 8, clean(text))
|
|
|
|
| 106 |
return tmp.name
|
| 107 |
|
| 108 |
# ---------------- 6 | Gradio UI ----------------
|
| 109 |
+
|
| 110 |
with gr.Blocks(title="AI Compliance & Governance Report") as demo:
|
| 111 |
+
gr.Markdown("## 🛡️ Compliance Survey → AI‑Written Report \nFill in the survey and receive a polished PDF assessment.")
|
| 112 |
|
| 113 |
widgets = {}
|
| 114 |
for q in QUESTIONS:
|
|
|
|
| 129 |
out_pdf = gr.File(label="Download PDF")
|
| 130 |
|
| 131 |
# ---------------- 7 | Callback ----------------
|
| 132 |
+
|
| 133 |
def run(*vals):
|
| 134 |
data = dict(zip(widgets.keys(), vals))
|
| 135 |
prompt = prompt_from(data)
|