karbanovich commited on
Commit
97ef6ed
·
verified ·
1 Parent(s): d29ceeb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -31
app.py CHANGED
@@ -11,7 +11,9 @@ AWS_ACCESS_KEY = 'AKIAR7HWYEDXVFCVRKRB'
11
  AWS_SECRET_KEY = 'FEtBc0eNuiDaCeRYzCH0hvC0k9wSe86ApY49IR4/'
12
  BUCKET_NAME = 'flaik-public'
13
 
 
14
  def upload_to_aws(local_file, s3_file):
 
15
  s3 = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)
16
  try:
17
  s3.upload_file(local_file, BUCKET_NAME, s3_file)
@@ -51,45 +53,52 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
51
  path_precision=int(path_precision)
52
  )
53
 
 
 
 
 
54
  # Save the SVG string to a temporary file
55
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
56
- temp_file.write(svg_str.encode('utf-8'))
57
- temp_file.close()
58
 
59
  # Upload the SVG file to AWS S3
60
- s3_file_name = temp_file.name.split('/')[-1] # Use the temp file name as the S3 file name
61
- file_url = upload_to_aws(temp_file.name, s3_file_name)
62
 
 
63
  return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), file_url
64
 
65
  # Gradio interface
66
  iface = gr.Blocks()
67
 
68
  with iface:
69
-
70
  gr.Interface(
71
- fn=convert_image,
72
- inputs=[
73
- gr.Image(type="pil", label="Upload Image"),
74
- gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
75
- gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
76
- gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
77
- gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
78
- gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
79
- gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
80
- gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
81
- gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
82
- gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
83
- gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
84
- gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
85
- ],
86
- outputs=[
87
- gr.HTML(label="SVG Output"),
88
- gr.Textbox(label="SVG File URL")
89
- ],
90
- title="Convert Image to SVG vectors",
91
- description="Upload an image and customize the conversion parameters as needed.<br><h2>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2>",
92
- )
93
-
94
-
95
- iface.launch()
 
 
 
 
11
  AWS_SECRET_KEY = 'FEtBc0eNuiDaCeRYzCH0hvC0k9wSe86ApY49IR4/'
12
  BUCKET_NAME = 'flaik-public'
13
 
14
+
15
  def upload_to_aws(local_file, s3_file):
16
+ """Uploads a file to AWS S3 and returns the URL."""
17
  s3 = boto3.client('s3', aws_access_key_id=AWS_ACCESS_KEY, aws_secret_access_key=AWS_SECRET_KEY)
18
  try:
19
  s3.upload_file(local_file, BUCKET_NAME, s3_file)
 
53
  path_precision=int(path_precision)
54
  )
55
 
56
+ # Debug: Print the SVG content
57
+ print("Generated SVG Content:")
58
+ print(svg_str)
59
+
60
  # Save the SVG string to a temporary file
61
+ with tempfile.NamedTemporaryFile(delete=False, suffix='.svg', mode='w', encoding='utf-8') as temp_file:
62
+ temp_file.write(svg_str)
63
+ temp_file_path = temp_file.name
64
 
65
  # Upload the SVG file to AWS S3
66
+ s3_file_name = temp_file_path.split('/')[-1] # Use the temp file name as the S3 file name
67
+ file_url = upload_to_aws(temp_file_path, s3_file_name)
68
 
69
+ # Return the SVG wrapped in HTML and the file URL
70
  return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), file_url
71
 
72
  # Gradio interface
73
  iface = gr.Blocks()
74
 
75
  with iface:
 
76
  gr.Interface(
77
+ fn=convert_image,
78
+ inputs=[
79
+ gr.Image(type="pil", label="Upload Image"),
80
+ gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
81
+ gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
82
+ gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
83
+ gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
84
+ gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
85
+ gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
86
+ gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
87
+ gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
88
+ gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
89
+ gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
90
+ gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
91
+ ],
92
+ outputs=[
93
+ gr.HTML(label="SVG Output"),
94
+ gr.Textbox(label="SVG File URL")
95
+ ],
96
+ title="Convert Image to SVG vectors",
97
+ description="Upload an image and customize the conversion parameters as needed.<br><h2>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2>",
98
+ )
99
+
100
+ iface.launch()
101
+
102
+
103
+
104
+