acmc commited on
Commit
52f401c
·
verified ·
1 Parent(s): 0d51a33

Update gradio_demo.py

Browse files
Files changed (1) hide show
  1. gradio_demo.py +40 -4
gradio_demo.py CHANGED
@@ -13,7 +13,33 @@ import gradio as gr
13
  from pdf_attacker import PDFAttacker
14
 
15
 
16
- attacker = PDFAttacker()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  # Theme customization per request
19
  theme = gr.themes.Soft(
@@ -63,6 +89,8 @@ def generate_pdf(
63
  mode: str,
64
  attack_factor: float = 0.7,
65
  target_text: str = "",
 
 
66
  ) -> Tuple[str, str]:
67
  """Generate selected PDF and return (pdf_path, extracted_text)
68
 
@@ -77,6 +105,10 @@ def generate_pdf(
77
  # Clean input text
78
  clean_text = " ".join(text.split())
79
 
 
 
 
 
80
  try:
81
  if mode == 'normal':
82
  attacker.create_normal_pdf(text=clean_text, output_path=output_path)
@@ -110,18 +142,22 @@ def build_demo():
110
  attack_factor = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.7, label='Attack factor (attacked mode)')
111
  target_text = gr.Textbox(lines=2, label='Target text (targeted mode)')
112
  generate = gr.Button('Generate PDF')
 
 
 
 
113
 
114
  download_file = gr.File(label='Download generated PDF')
115
  extracted_preview = gr.Textbox(lines=8, label='Extracted text preview')
116
 
117
- def _on_generate(text, mode, attack_factor, target_text):
118
- path, extracted = generate_pdf(text=text, mode=mode, attack_factor=attack_factor, target_text=target_text)
119
  if not path:
120
  # Return empty file and error message in preview
121
  return None, extracted
122
  return path, extracted
123
 
124
- generate.click(fn=_on_generate, inputs=[txt, mode, attack_factor, target_text], outputs=[download_file, extracted_preview])
125
 
126
  return demo
127
 
 
13
  from pdf_attacker import PDFAttacker
14
 
15
 
16
+ def _resolve_font_path(choice: str, uploaded_file) -> str:
17
+ """Return a font path given a dropdown choice or uploaded file.
18
+
19
+ If choice is 'auto' return None so PDFAttacker will pick a reasonable default.
20
+ """
21
+ if choice == 'auto' or not choice:
22
+ return None
23
+
24
+ # known presets mapped to common system paths
25
+ presets = {
26
+ 'DejaVu Serif': '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf',
27
+ 'Liberation Serif': '/usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf',
28
+ 'FreeSerif': '/usr/share/fonts/truetype/freefont/FreeSerif.ttf',
29
+ }
30
+
31
+ if choice in presets:
32
+ p = presets[choice]
33
+ return p if os.path.exists(p) else None
34
+
35
+ # custom uploaded file: gradio returns a local path-like string or dict
36
+ if choice == 'Custom' and uploaded_file:
37
+ # uploaded_file may be a dict-like object or a str path
38
+ if isinstance(uploaded_file, dict) and 'name' in uploaded_file:
39
+ return uploaded_file['name']
40
+ return uploaded_file
41
+
42
+ return None
43
 
44
  # Theme customization per request
45
  theme = gr.themes.Soft(
 
89
  mode: str,
90
  attack_factor: float = 0.7,
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
 
 
105
  # Clean input text
106
  clean_text = " ".join(text.split())
107
 
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':
114
  attacker.create_normal_pdf(text=clean_text, output_path=output_path)
 
142
  attack_factor = gr.Slider(minimum=0.0, maximum=1.0, step=0.05, value=0.7, label='Attack factor (attacked mode)')
143
  target_text = gr.Textbox(lines=2, label='Target text (targeted mode)')
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