Update gradio_demo.py
Browse files- gradio_demo.py +56 -13
gradio_demo.py
CHANGED
@@ -21,16 +21,54 @@ def _resolve_font_path(choice: str, uploaded_file) -> str:
|
|
21 |
if choice == 'auto' or not choice:
|
22 |
return None
|
23 |
|
24 |
-
# known presets mapped to
|
25 |
presets = {
|
26 |
-
'DejaVu Serif':
|
27 |
-
|
28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
}
|
30 |
|
31 |
if choice in presets:
|
32 |
-
p
|
33 |
-
|
|
|
|
|
34 |
|
35 |
# custom uploaded file: gradio returns a local path-like string or dict
|
36 |
if choice == 'Custom' and uploaded_file:
|
@@ -92,7 +130,7 @@ def generate_pdf(
|
|
92 |
font_choice: str = 'auto',
|
93 |
uploaded_font=None,
|
94 |
wrap_on_words: bool = True,
|
95 |
-
) -> Tuple[str, str]:
|
96 |
"""Generate selected PDF and return (pdf_path, extracted_text)
|
97 |
|
98 |
Inputs: text, mode: 'normal'|'attacked'|'targeted', attack_factor, target_text
|
@@ -112,6 +150,10 @@ def generate_pdf(
|
|
112 |
# apply wrap mode
|
113 |
attacker.wrap_on_words = wrap_on_words
|
114 |
|
|
|
|
|
|
|
|
|
115 |
try:
|
116 |
if mode == 'normal':
|
117 |
attacker.create_normal_pdf(text=clean_text, output_path=output_path)
|
@@ -125,12 +167,12 @@ def generate_pdf(
|
|
125 |
|
126 |
except Exception as e:
|
127 |
# Surface errors to the UI
|
128 |
-
return "", f"Error
|
129 |
|
130 |
# Extract text to show how the copied/extracted text looks
|
131 |
extracted = _extract_text_from_pdf(output_path)
|
132 |
|
133 |
-
return output_path, extracted
|
134 |
|
135 |
|
136 |
def build_demo():
|
@@ -153,15 +195,16 @@ def build_demo():
|
|
153 |
|
154 |
download_file = gr.File(label='Download generated PDF')
|
155 |
extracted_preview = gr.Textbox(lines=8, label='Extracted text preview')
|
|
|
156 |
|
157 |
def _on_generate(text, mode, attack_factor, target_text, font_choice, upload_font, wrap_on_words):
|
158 |
-
path, extracted = generate_pdf(text=text, mode=mode, attack_factor=attack_factor, target_text=target_text, font_choice=font_choice, uploaded_font=upload_font, wrap_on_words=wrap_on_words)
|
159 |
if not path:
|
160 |
# Return empty file and error message in preview
|
161 |
-
return None, extracted
|
162 |
-
return path, extracted
|
163 |
|
164 |
-
generate.click(fn=_on_generate, inputs=[txt, mode, attack_factor, target_text, font_choice, upload_font, wrap_on_words], outputs=[download_file, extracted_preview])
|
165 |
|
166 |
return demo
|
167 |
|
|
|
21 |
if choice == 'auto' or not choice:
|
22 |
return None
|
23 |
|
24 |
+
# known presets mapped to candidate system paths (try first existing)
|
25 |
presets = {
|
26 |
+
'DejaVu Serif': [
|
27 |
+
'/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf',
|
28 |
+
],
|
29 |
+
'Liberation Serif': [
|
30 |
+
'/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf',
|
31 |
+
],
|
32 |
+
'FreeSerif': [
|
33 |
+
'/usr/share/fonts/truetype/freefont/FreeSerif.ttf',
|
34 |
+
],
|
35 |
+
'DejaVu Sans': [
|
36 |
+
'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
|
37 |
+
],
|
38 |
+
'Arial': [
|
39 |
+
'/usr/share/fonts/truetype/msttcorefonts/Arial.ttf',
|
40 |
+
'/usr/share/fonts/truetype/msttcorefonts/arial.ttf',
|
41 |
+
'/usr/share/fonts/truetype/arial/arial.ttf',
|
42 |
+
'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
|
43 |
+
],
|
44 |
+
'Helvetica': [
|
45 |
+
'/usr/share/fonts/truetype/urw-base35/Helvetica.ttf',
|
46 |
+
'/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf',
|
47 |
+
'/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf',
|
48 |
+
],
|
49 |
+
'Times New Roman': [
|
50 |
+
'/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf',
|
51 |
+
'/usr/share/fonts/truetype/msttcorefonts/Times_New_Roman.ttf',
|
52 |
+
'/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf',
|
53 |
+
],
|
54 |
+
'Roboto': [
|
55 |
+
'/usr/share/fonts/truetype/roboto/Roboto-Regular.ttf',
|
56 |
+
'/usr/share/fonts/truetype/roboto/Roboto-Regular.ttf',
|
57 |
+
],
|
58 |
+
'Courier': [
|
59 |
+
'/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf',
|
60 |
+
'/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf',
|
61 |
+
],
|
62 |
+
'Times': [
|
63 |
+
'/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf',
|
64 |
+
],
|
65 |
}
|
66 |
|
67 |
if choice in presets:
|
68 |
+
for p in presets[choice]:
|
69 |
+
if os.path.exists(p):
|
70 |
+
return p
|
71 |
+
return None
|
72 |
|
73 |
# custom uploaded file: gradio returns a local path-like string or dict
|
74 |
if choice == 'Custom' and uploaded_file:
|
|
|
130 |
font_choice: str = 'auto',
|
131 |
uploaded_font=None,
|
132 |
wrap_on_words: bool = True,
|
133 |
+
) -> Tuple[str, str, str]:
|
134 |
"""Generate selected PDF and return (pdf_path, extracted_text)
|
135 |
|
136 |
Inputs: text, mode: 'normal'|'attacked'|'targeted', attack_factor, target_text
|
|
|
150 |
# apply wrap mode
|
151 |
attacker.wrap_on_words = wrap_on_words
|
152 |
|
153 |
+
# Build a contextual status string for the UI
|
154 |
+
resolved_font = font_path or "(auto/default)"
|
155 |
+
status_lines = [f"Font resolved to: {resolved_font}", f"Wrap on words: {wrap_on_words}"]
|
156 |
+
|
157 |
try:
|
158 |
if mode == 'normal':
|
159 |
attacker.create_normal_pdf(text=clean_text, output_path=output_path)
|
|
|
167 |
|
168 |
except Exception as e:
|
169 |
# Surface errors to the UI
|
170 |
+
return "", f"Error extracting text: {e}", f"Error: {e}"
|
171 |
|
172 |
# Extract text to show how the copied/extracted text looks
|
173 |
extracted = _extract_text_from_pdf(output_path)
|
174 |
|
175 |
+
return output_path, extracted, "\n".join(status_lines)
|
176 |
|
177 |
|
178 |
def build_demo():
|
|
|
195 |
|
196 |
download_file = gr.File(label='Download generated PDF')
|
197 |
extracted_preview = gr.Textbox(lines=8, label='Extracted text preview')
|
198 |
+
status_box = gr.Textbox(lines=4, label='Status')
|
199 |
|
200 |
def _on_generate(text, mode, attack_factor, target_text, font_choice, upload_font, wrap_on_words):
|
201 |
+
path, extracted, status = generate_pdf(text=text, mode=mode, attack_factor=attack_factor, target_text=target_text, font_choice=font_choice, uploaded_font=upload_font, wrap_on_words=wrap_on_words)
|
202 |
if not path:
|
203 |
# Return empty file and error message in preview
|
204 |
+
return None, extracted, status
|
205 |
+
return path, extracted, status
|
206 |
|
207 |
+
generate.click(fn=_on_generate, inputs=[txt, mode, attack_factor, target_text, font_choice, upload_font, wrap_on_words], outputs=[download_file, extracted_preview, status_box])
|
208 |
|
209 |
return demo
|
210 |
|