Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,14 +17,10 @@ def add_signature_to_document(
|
|
| 17 |
bottom_percent,
|
| 18 |
right_percent
|
| 19 |
):
|
| 20 |
-
"""
|
| 21 |
-
Add signature to document with specified parameters
|
| 22 |
-
"""
|
| 23 |
if document_file is None or signature_file is None:
|
| 24 |
return None, "❌ Please upload both document and signature files!"
|
| 25 |
-
|
| 26 |
try:
|
| 27 |
-
# Determine signature color
|
| 28 |
if color_mode == "Auto-detect from document":
|
| 29 |
signature_color = None
|
| 30 |
elif color_mode == "Black":
|
|
@@ -33,241 +29,74 @@ def add_signature_to_document(
|
|
| 33 |
signature_color = (50, 50, 50)
|
| 34 |
elif color_mode == "Blue":
|
| 35 |
signature_color = (0, 0, 255)
|
| 36 |
-
else:
|
| 37 |
signature_color = (int(custom_r), int(custom_g), int(custom_b))
|
| 38 |
-
|
| 39 |
-
# Create temporary output file
|
| 40 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_output:
|
| 41 |
output_path = tmp_output.name
|
| 42 |
-
|
| 43 |
-
# Process signature
|
| 44 |
result_path = create_sign(
|
| 45 |
document_path=document_file.name,
|
| 46 |
sign_path=signature_file.name,
|
| 47 |
output_path=output_path,
|
| 48 |
signature_color=signature_color,
|
| 49 |
-
scale_factor=scale_factor / 100.0,
|
| 50 |
rotation_angle=rotation_angle,
|
| 51 |
bottom_percent=bottom_percent,
|
| 52 |
right_percent=right_percent
|
| 53 |
)
|
| 54 |
-
|
| 55 |
-
# Load result image
|
| 56 |
result_img = Image.open(result_path)
|
| 57 |
|
| 58 |
-
status_msg = f""
|
| 59 |
-
✅ **Signature added successfully!**
|
| 60 |
-
|
| 61 |
-
📊 **Settings:**
|
| 62 |
-
- Color: {color_mode} {signature_color if signature_color else '(auto-detected)'}
|
| 63 |
-
- Size: {scale_factor}% of document width
|
| 64 |
-
- Rotation: {rotation_angle}°
|
| 65 |
-
- Position: Bottom {bottom_percent}%, Right {right_percent}%
|
| 66 |
-
"""
|
| 67 |
-
|
| 68 |
return result_img, status_msg
|
| 69 |
-
|
| 70 |
except Exception as e:
|
| 71 |
return None, f"❌ **Error:** {str(e)}"
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
custom_css = ""
|
| 75 |
-
.gradio-container {
|
| 76 |
-
font-family: 'IBM Plex Sans', sans-serif;
|
| 77 |
-
}
|
| 78 |
-
.gr-button {
|
| 79 |
-
color: white;
|
| 80 |
-
border-color: #9c27b0;
|
| 81 |
-
background: linear-gradient(45deg, #9c27b0, #673ab7);
|
| 82 |
-
}
|
| 83 |
-
.gr-button:hover {
|
| 84 |
-
border-color: #7b1fa2;
|
| 85 |
-
background: linear-gradient(45deg, #7b1fa2, #512da8);
|
| 86 |
-
}
|
| 87 |
-
#component-0 {
|
| 88 |
-
max-width: 1200px;
|
| 89 |
-
margin: 0 auto;
|
| 90 |
-
}
|
| 91 |
-
"""
|
| 92 |
|
| 93 |
-
# Create Gradio interface
|
| 94 |
with gr.Blocks(css=custom_css, title="SignLib - AI Signature Generator") as demo:
|
| 95 |
-
gr.Markdown(
|
| 96 |
-
"""
|
| 97 |
-
# 🖋️ SignLib - AI Signature Generator
|
| 98 |
-
|
| 99 |
-
**Automatically add signatures to documents for synthetic data generation and AI training.**
|
| 100 |
-
|
| 101 |
-
Upload your document and signature, adjust parameters, and generate signed documents instantly.
|
| 102 |
-
Perfect for creating training datasets for machine learning models.
|
| 103 |
-
|
| 104 |
-
---
|
| 105 |
-
"""
|
| 106 |
-
)
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
-
with gr.Column(
|
| 110 |
-
gr.
|
| 111 |
-
|
| 112 |
-
document_input = gr.File(
|
| 113 |
-
label="Document (PDF, TIFF, PNG, JPEG)",
|
| 114 |
-
file_types=[".pdf", ".tif", ".tiff", ".png", ".jpg", ".jpeg"]
|
| 115 |
-
)
|
| 116 |
-
|
| 117 |
-
signature_input = gr.File(
|
| 118 |
-
label="Signature Image (PNG recommended)",
|
| 119 |
-
file_types=[".png", ".jpg", ".jpeg"]
|
| 120 |
-
)
|
| 121 |
-
|
| 122 |
-
gr.Markdown("---")
|
| 123 |
-
gr.Markdown("### 🎨 Signature Color")
|
| 124 |
|
| 125 |
color_mode = gr.Radio(
|
| 126 |
choices=["Auto-detect from document", "Black", "Dark Gray", "Blue", "Custom RGB"],
|
| 127 |
-
value="Auto-detect from document",
|
| 128 |
-
label="Color Mode"
|
| 129 |
)
|
| 130 |
|
| 131 |
with gr.Row():
|
| 132 |
-
custom_r = gr.Slider(0, 255, 0,
|
| 133 |
-
custom_g = gr.Slider(0, 255, 0,
|
| 134 |
-
custom_b = gr.Slider(0, 255, 0,
|
| 135 |
-
|
| 136 |
-
def update_custom_color_visibility(mode):
|
| 137 |
-
return {
|
| 138 |
-
custom_r: gr.update(visible=(mode == "Custom RGB")),
|
| 139 |
-
custom_g: gr.update(visible=(mode == "Custom RGB")),
|
| 140 |
-
custom_b: gr.update(visible=(mode == "Custom RGB"))
|
| 141 |
-
}
|
| 142 |
|
| 143 |
color_mode.change(
|
| 144 |
-
|
| 145 |
-
inputs=[color_mode],
|
| 146 |
outputs=[custom_r, custom_g, custom_b]
|
| 147 |
)
|
| 148 |
|
| 149 |
-
gr.
|
| 150 |
-
gr.
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
minimum=5,
|
| 154 |
-
maximum=30,
|
| 155 |
-
value=12,
|
| 156 |
-
step=1,
|
| 157 |
-
label="Signature Size (% of document width)",
|
| 158 |
-
info="Recommended: 10-15%"
|
| 159 |
-
)
|
| 160 |
-
|
| 161 |
-
rotation_angle = gr.Slider(
|
| 162 |
-
minimum=-20,
|
| 163 |
-
maximum=20,
|
| 164 |
-
value=0,
|
| 165 |
-
step=0.5,
|
| 166 |
-
label="Rotation Angle (degrees)",
|
| 167 |
-
info="Positive = clockwise, Negative = counter-clockwise"
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
bottom_percent = gr.Slider(
|
| 171 |
-
minimum=10,
|
| 172 |
-
maximum=60,
|
| 173 |
-
value=25,
|
| 174 |
-
step=5,
|
| 175 |
-
label="Search Area from Bottom (%)",
|
| 176 |
-
info="Where to look for signature placement"
|
| 177 |
-
)
|
| 178 |
-
|
| 179 |
-
right_percent = gr.Slider(
|
| 180 |
-
minimum=10,
|
| 181 |
-
maximum=80,
|
| 182 |
-
value=50,
|
| 183 |
-
step=5,
|
| 184 |
-
label="Search Area from Right (%)",
|
| 185 |
-
info="Where to look for signature placement"
|
| 186 |
-
)
|
| 187 |
-
|
| 188 |
-
process_btn = gr.Button("✍️ Add Signature", variant="primary", size="lg")
|
| 189 |
-
|
| 190 |
-
with gr.Column(scale=1):
|
| 191 |
-
gr.Markdown("### 📊 Result")
|
| 192 |
|
| 193 |
-
|
| 194 |
-
label="Signed Document",
|
| 195 |
-
type="pil",
|
| 196 |
-
height=600
|
| 197 |
-
)
|
| 198 |
|
|
|
|
|
|
|
| 199 |
status_output = gr.Markdown("")
|
| 200 |
-
|
| 201 |
-
gr.Markdown(
|
| 202 |
-
"""
|
| 203 |
-
---
|
| 204 |
-
### 💡 Tips
|
| 205 |
-
|
| 206 |
-
- **Auto-detect color**: Best for matching document text color
|
| 207 |
-
- **Size 12%**: Most natural and professional appearance
|
| 208 |
-
- **Bottom 25%, Right 50%**: Standard signature position (bottom-right)
|
| 209 |
-
- **Rotation ±5-10°**: Adds natural variation for AI training
|
| 210 |
-
|
| 211 |
-
### 📦 Installation
|
| 212 |
-
```bash
|
| 213 |
-
pip install signlib
|
| 214 |
-
```
|
| 215 |
-
|
| 216 |
-
### 🐍 Python Usage
|
| 217 |
-
```python
|
| 218 |
-
from signlib import create_sign
|
| 219 |
-
|
| 220 |
-
create_sign(
|
| 221 |
-
'document.pdf',
|
| 222 |
-
'signature.png',
|
| 223 |
-
bottom_percent=25,
|
| 224 |
-
right_percent=50
|
| 225 |
-
)
|
| 226 |
-
```
|
| 227 |
-
|
| 228 |
-
---
|
| 229 |
-
|
| 230 |
-
**[GitHub](https://github.com/cagrigungor/signlib)** |
|
| 231 |
-
**[PyPI](https://pypi.org/project/signlib/)** |
|
| 232 |
-
Made with ❤️ for AI & ML
|
| 233 |
-
"""
|
| 234 |
-
)
|
| 235 |
-
|
| 236 |
-
# Connect process button
|
| 237 |
process_btn.click(
|
| 238 |
fn=add_signature_to_document,
|
| 239 |
-
inputs=[
|
| 240 |
-
document_input,
|
| 241 |
-
signature_input,
|
| 242 |
-
color_mode,
|
| 243 |
-
custom_r,
|
| 244 |
-
custom_g,
|
| 245 |
-
custom_b,
|
| 246 |
-
scale_factor,
|
| 247 |
-
rotation_angle,
|
| 248 |
-
bottom_percent,
|
| 249 |
-
right_percent
|
| 250 |
-
],
|
| 251 |
outputs=[output_image, status_output]
|
| 252 |
)
|
| 253 |
-
|
| 254 |
-
# Examples
|
| 255 |
-
gr.Markdown("### 📚 Example Presets")
|
| 256 |
-
gr.Examples(
|
| 257 |
-
examples=[
|
| 258 |
-
["Auto-detect from document", 12, 0, 25, 50, "Standard signature position"],
|
| 259 |
-
["Black", 10, -5, 30, 40, "Small, tilted signature in bottom-right"],
|
| 260 |
-
["Blue", 15, 3, 20, 60, "Larger blue signature, slightly rotated"],
|
| 261 |
-
["Dark Gray", 12, 0, 40, 40, "Gray signature in lower-right area"],
|
| 262 |
-
],
|
| 263 |
-
inputs=[color_mode, scale_factor, rotation_angle, bottom_percent, right_percent, gr.Textbox(visible=False, label="Description")],
|
| 264 |
-
label="Quick Presets"
|
| 265 |
-
)
|
| 266 |
|
| 267 |
-
# Launch the app
|
| 268 |
if __name__ == "__main__":
|
| 269 |
-
demo.launch(
|
| 270 |
-
share=False,
|
| 271 |
-
server_name="0.0.0.0",
|
| 272 |
-
server_port=7860
|
| 273 |
-
)
|
|
|
|
| 17 |
bottom_percent,
|
| 18 |
right_percent
|
| 19 |
):
|
|
|
|
|
|
|
|
|
|
| 20 |
if document_file is None or signature_file is None:
|
| 21 |
return None, "❌ Please upload both document and signature files!"
|
| 22 |
+
|
| 23 |
try:
|
|
|
|
| 24 |
if color_mode == "Auto-detect from document":
|
| 25 |
signature_color = None
|
| 26 |
elif color_mode == "Black":
|
|
|
|
| 29 |
signature_color = (50, 50, 50)
|
| 30 |
elif color_mode == "Blue":
|
| 31 |
signature_color = (0, 0, 255)
|
| 32 |
+
else:
|
| 33 |
signature_color = (int(custom_r), int(custom_g), int(custom_b))
|
| 34 |
+
|
|
|
|
| 35 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as tmp_output:
|
| 36 |
output_path = tmp_output.name
|
| 37 |
+
|
|
|
|
| 38 |
result_path = create_sign(
|
| 39 |
document_path=document_file.name,
|
| 40 |
sign_path=signature_file.name,
|
| 41 |
output_path=output_path,
|
| 42 |
signature_color=signature_color,
|
| 43 |
+
scale_factor=scale_factor / 100.0,
|
| 44 |
rotation_angle=rotation_angle,
|
| 45 |
bottom_percent=bottom_percent,
|
| 46 |
right_percent=right_percent
|
| 47 |
)
|
| 48 |
+
|
|
|
|
| 49 |
result_img = Image.open(result_path)
|
| 50 |
|
| 51 |
+
status_msg = f"✅ **Signature added successfully!**"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return result_img, status_msg
|
| 53 |
+
|
| 54 |
except Exception as e:
|
| 55 |
return None, f"❌ **Error:** {str(e)}"
|
| 56 |
|
| 57 |
+
# UI Kısmı
|
| 58 |
+
custom_css = ".gradio-container {font-family: 'IBM Plex Sans', sans-serif;}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
|
|
|
| 60 |
with gr.Blocks(css=custom_css, title="SignLib - AI Signature Generator") as demo:
|
| 61 |
+
gr.Markdown("# 🖋️ SignLib - AI Signature Generator")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
+
with gr.Column():
|
| 65 |
+
document_input = gr.File(label="Document", file_types=[".pdf", ".png", ".jpg"])
|
| 66 |
+
signature_input = gr.File(label="Signature", file_types=[".png", ".jpg"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
color_mode = gr.Radio(
|
| 69 |
choices=["Auto-detect from document", "Black", "Dark Gray", "Blue", "Custom RGB"],
|
| 70 |
+
value="Auto-detect from document", label="Color"
|
|
|
|
| 71 |
)
|
| 72 |
|
| 73 |
with gr.Row():
|
| 74 |
+
custom_r = gr.Slider(0, 255, 0, label="R", visible=False)
|
| 75 |
+
custom_g = gr.Slider(0, 255, 0, label="G", visible=False)
|
| 76 |
+
custom_b = gr.Slider(0, 255, 0, label="B", visible=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
color_mode.change(
|
| 79 |
+
lambda m: [gr.update(visible=(m=="Custom RGB"))]*3,
|
| 80 |
+
inputs=[color_mode],
|
| 81 |
outputs=[custom_r, custom_g, custom_b]
|
| 82 |
)
|
| 83 |
|
| 84 |
+
scale_factor = gr.Slider(5, 30, 12, label="Size %")
|
| 85 |
+
rotation_angle = gr.Slider(-20, 20, 0, label="Rotation")
|
| 86 |
+
bottom_percent = gr.Slider(10, 60, 25, label="Bottom %")
|
| 87 |
+
right_percent = gr.Slider(10, 80, 50, label="Right %")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
+
process_btn = gr.Button("✍️ Add Signature", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
with gr.Column():
|
| 92 |
+
output_image = gr.Image(label="Result", type="pil")
|
| 93 |
status_output = gr.Markdown("")
|
| 94 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
process_btn.click(
|
| 96 |
fn=add_signature_to_document,
|
| 97 |
+
inputs=[document_input, signature_input, color_mode, custom_r, custom_g, custom_b, scale_factor, rotation_angle, bottom_percent, right_percent],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
outputs=[output_image, status_output]
|
| 99 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
| 101 |
if __name__ == "__main__":
|
| 102 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|