Spaces:
Running
Running
ArrcttacsrjksX
commited on
Upload app(43).py
Browse files- Backup/app(43).py +136 -0
Backup/app(43).py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import subprocess
|
3 |
+
import os
|
4 |
+
import tempfile
|
5 |
+
import datetime
|
6 |
+
from huggingface_hub import upload_file
|
7 |
+
|
8 |
+
def convert_image_to_dxf(image_file, output_folder=None, use_lines=False):
|
9 |
+
try:
|
10 |
+
# Validate input image
|
11 |
+
if image_file is None:
|
12 |
+
return "No image provided", None, None
|
13 |
+
|
14 |
+
# Define output folder, using a temp directory if none is specified
|
15 |
+
if not output_folder:
|
16 |
+
output_folder = tempfile.mkdtemp()
|
17 |
+
else:
|
18 |
+
os.makedirs(output_folder, exist_ok=True)
|
19 |
+
|
20 |
+
# Generate the date-based output file name
|
21 |
+
current_date = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
22 |
+
image_path = image_file
|
23 |
+
output_path = os.path.join(output_folder, f"{current_date}_output.dxf")
|
24 |
+
debug_output_path = os.path.join(output_folder, f"{current_date}_debug.png")
|
25 |
+
|
26 |
+
# Prepare the command arguments
|
27 |
+
command_args = ["./SimpleImageToDxfHavePass"]
|
28 |
+
|
29 |
+
# Add --use-lines if the checkbox is checked
|
30 |
+
if use_lines:
|
31 |
+
command_args.append("--use-lines")
|
32 |
+
|
33 |
+
# Add other arguments
|
34 |
+
command_args.extend([f"--imagePath={image_path}", f"--outputPath={output_path}", f"--debug-output={debug_output_path}"])
|
35 |
+
|
36 |
+
# Execute conversion command
|
37 |
+
result = subprocess.run(
|
38 |
+
command_args,
|
39 |
+
check=True,
|
40 |
+
capture_output=True
|
41 |
+
)
|
42 |
+
|
43 |
+
# Log stdout for debugging
|
44 |
+
print(result.stdout.decode('utf-8'))
|
45 |
+
|
46 |
+
# Check if conversion was successful
|
47 |
+
if not os.path.exists(output_path):
|
48 |
+
return "Conversion failed: DXF file was not created.", None, None
|
49 |
+
|
50 |
+
# Prepare folder structure for upload
|
51 |
+
date_folder = f"{current_date}"
|
52 |
+
|
53 |
+
# Hugging Face token
|
54 |
+
hf_token = os.getenv("HF_TOKEN")
|
55 |
+
if not hf_token:
|
56 |
+
return "Hugging Face token not found", None, None
|
57 |
+
|
58 |
+
# Upload input image, output DXF, and optionally debug image to Hugging Face in a date-based folder
|
59 |
+
uploaded_files = []
|
60 |
+
|
61 |
+
# Upload input image
|
62 |
+
uploaded_input = upload_file(
|
63 |
+
path_or_fileobj=image_path,
|
64 |
+
path_in_repo=f"datasets/ArrcttacsrjksX/ImageToAutocadData/{date_folder}/{os.path.basename(image_path)}",
|
65 |
+
repo_id="ArrcttacsrjksX/ImageToAutocadData",
|
66 |
+
token=hf_token
|
67 |
+
)
|
68 |
+
uploaded_files.append(uploaded_input)
|
69 |
+
|
70 |
+
# Upload DXF output
|
71 |
+
uploaded_dxf = upload_file(
|
72 |
+
path_or_fileobj=output_path,
|
73 |
+
path_in_repo=f"datasets/ArrcttacsrjksX/ImageToAutocadData/{date_folder}/{os.path.basename(output_path)}",
|
74 |
+
repo_id="ArrcttacsrjksX/ImageToAutocadData",
|
75 |
+
token=hf_token
|
76 |
+
)
|
77 |
+
uploaded_files.append(uploaded_dxf)
|
78 |
+
|
79 |
+
# If the checkbox is ticked, upload debug image
|
80 |
+
if use_lines:
|
81 |
+
uploaded_debug = upload_file(
|
82 |
+
path_or_fileobj=debug_output_path,
|
83 |
+
path_in_repo=f"datasets/ArrcttacsrjksX/ImageToAutocadData/{date_folder}/{os.path.basename(debug_output_path)}",
|
84 |
+
repo_id="ArrcttacsrjksX/ImageToAutocadData",
|
85 |
+
token=hf_token
|
86 |
+
)
|
87 |
+
uploaded_files.append(uploaded_debug)
|
88 |
+
|
89 |
+
# Return files directly for download in Gradio interface
|
90 |
+
return output_path, debug_output_path if use_lines else None, uploaded_files
|
91 |
+
|
92 |
+
except subprocess.CalledProcessError as e:
|
93 |
+
error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}"
|
94 |
+
return error_msg, None, None
|
95 |
+
|
96 |
+
def main():
|
97 |
+
with gr.Blocks() as demo:
|
98 |
+
with gr.Tabs():
|
99 |
+
# Tab for conversion
|
100 |
+
with gr.TabItem("Image to DXF"):
|
101 |
+
gr.Markdown("# SimpleImageToDxfHavePass")
|
102 |
+
|
103 |
+
# Input row for image and optional output folder
|
104 |
+
with gr.Row():
|
105 |
+
image_input = gr.Image(type="filepath", label="Input Image (PNG/JPG)")
|
106 |
+
output_folder = gr.Textbox(label="Output Folder (optional)", placeholder="Leave blank to use a temporary folder")
|
107 |
+
|
108 |
+
# Checkbox to decide whether to use --use-lines
|
109 |
+
use_lines_checkbox = gr.Checkbox(label="Use --use-lines for conversion", value=False) # Default is False
|
110 |
+
|
111 |
+
# Outputs: Debug image and DXF file download link
|
112 |
+
with gr.Row():
|
113 |
+
debug_output = gr.Image(type="filepath", label="Debug Output Preview")
|
114 |
+
dxf_output = gr.File(label="DXF File Download")
|
115 |
+
|
116 |
+
# Conversion button with event binding
|
117 |
+
convert_btn = gr.Button("Convert to DXF")
|
118 |
+
convert_btn.click(
|
119 |
+
convert_image_to_dxf,
|
120 |
+
inputs=[image_input, output_folder, use_lines_checkbox],
|
121 |
+
outputs=[dxf_output, debug_output, dxf_output]
|
122 |
+
)
|
123 |
+
|
124 |
+
# About tab
|
125 |
+
with gr.TabItem("About"):
|
126 |
+
gr.Markdown("This Gradio app allows users to convert an image to a DXF file using the SimpleImageToDxfHavePass command-line tool.")
|
127 |
+
|
128 |
+
demo.launch(share=True)
|
129 |
+
|
130 |
+
if __name__ == "__main__":
|
131 |
+
try:
|
132 |
+
subprocess.run(['chmod', '+x', './SimpleImageToDxfHavePass'], check=True)
|
133 |
+
except subprocess.CalledProcessError as e:
|
134 |
+
print(f"Error setting permissions: {e}")
|
135 |
+
exit(1)
|
136 |
+
main()
|