Update gradio_demo.py
Browse files- gradio_demo.py +8 -4
gradio_demo.py
CHANGED
|
@@ -91,6 +91,7 @@ def generate_pdf(
|
|
| 91 |
target_text: str = "",
|
| 92 |
font_choice: str = 'auto',
|
| 93 |
uploaded_font=None,
|
|
|
|
| 94 |
) -> Tuple[str, str]:
|
| 95 |
"""Generate selected PDF and return (pdf_path, extracted_text)
|
| 96 |
|
|
@@ -108,6 +109,8 @@ def generate_pdf(
|
|
| 108 |
# resolve font path and create an attacker instance for this request
|
| 109 |
font_path = _resolve_font_path(choice=font_choice, uploaded_file=uploaded_font)
|
| 110 |
attacker = PDFAttacker(font_path=font_path)
|
|
|
|
|
|
|
| 111 |
|
| 112 |
try:
|
| 113 |
if mode == 'normal':
|
|
@@ -144,20 +147,21 @@ def build_demo():
|
|
| 144 |
generate = gr.Button('Generate PDF')
|
| 145 |
|
| 146 |
# Font selection: presets + custom upload
|
| 147 |
-
font_choice = gr.Dropdown(choices=['auto', 'DejaVu Serif', 'Liberation Serif', 'FreeSerif', 'Custom'], value='auto', label='Font')
|
| 148 |
upload_font = gr.File(label='Upload TTF/OTF (optional)', file_count='single')
|
|
|
|
| 149 |
|
| 150 |
download_file = gr.File(label='Download generated PDF')
|
| 151 |
extracted_preview = gr.Textbox(lines=8, label='Extracted text preview')
|
| 152 |
|
| 153 |
-
def _on_generate(text, mode, attack_factor, target_text, font_choice, upload_font):
|
| 154 |
-
path, extracted = generate_pdf(text=text, mode=mode, attack_factor=attack_factor, target_text=target_text, font_choice=font_choice, uploaded_font=upload_font)
|
| 155 |
if not path:
|
| 156 |
# Return empty file and error message in preview
|
| 157 |
return None, extracted
|
| 158 |
return path, extracted
|
| 159 |
|
| 160 |
-
generate.click(fn=_on_generate, inputs=[txt, mode, attack_factor, target_text, font_choice, upload_font], outputs=[download_file, extracted_preview])
|
| 161 |
|
| 162 |
return demo
|
| 163 |
|
|
|
|
| 91 |
target_text: str = "",
|
| 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 |
|
|
|
|
| 109 |
# resolve font path and create an attacker instance for this request
|
| 110 |
font_path = _resolve_font_path(choice=font_choice, uploaded_file=uploaded_font)
|
| 111 |
attacker = PDFAttacker(font_path=font_path)
|
| 112 |
+
# apply wrap mode
|
| 113 |
+
attacker.wrap_on_words = wrap_on_words
|
| 114 |
|
| 115 |
try:
|
| 116 |
if mode == 'normal':
|
|
|
|
| 147 |
generate = gr.Button('Generate PDF')
|
| 148 |
|
| 149 |
# Font selection: presets + custom upload
|
| 150 |
+
font_choice = gr.Dropdown(choices=['auto', 'DejaVu Serif', 'Liberation Serif', 'FreeSerif', 'Arial', 'Helvetica', 'Times New Roman', 'Roboto', 'Courier', 'Custom'], value='auto', label='Font')
|
| 151 |
upload_font = gr.File(label='Upload TTF/OTF (optional)', file_count='single')
|
| 152 |
+
wrap_on_words = gr.Checkbox(label='Wrap on words', value=True)
|
| 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 |
|