Spaces:
Running
Running
ArrcttacsrjksX
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -3,8 +3,7 @@ import subprocess
|
|
3 |
import os
|
4 |
import tempfile
|
5 |
import datetime
|
6 |
-
import
|
7 |
-
from huggingface_hub import upload_file, hf_hub_download
|
8 |
|
9 |
def convert_image_to_dxf(image_file, output_folder=None, use_lines=False):
|
10 |
try:
|
@@ -41,19 +40,13 @@ def convert_image_to_dxf(image_file, output_folder=None, use_lines=False):
|
|
41 |
capture_output=True
|
42 |
)
|
43 |
|
44 |
-
#
|
45 |
print(result.stdout.decode('utf-8'))
|
46 |
-
if result.stderr:
|
47 |
-
print(result.stderr.decode('utf-8'))
|
48 |
|
49 |
# Check if conversion was successful
|
50 |
if not os.path.exists(output_path):
|
51 |
return "Conversion failed: DXF file was not created.", None, None
|
52 |
|
53 |
-
# Clean up temporary folder
|
54 |
-
if output_folder and os.path.isdir(output_folder):
|
55 |
-
shutil.rmtree(output_folder)
|
56 |
-
|
57 |
# Prepare folder structure for upload
|
58 |
date_folder = f"{current_date}"
|
59 |
|
@@ -93,29 +86,13 @@ def convert_image_to_dxf(image_file, output_folder=None, use_lines=False):
|
|
93 |
)
|
94 |
uploaded_files.append(uploaded_debug)
|
95 |
|
96 |
-
# Return
|
97 |
-
if use_lines
|
98 |
-
return output_path, debug_output_path, [output_path, debug_output_path]
|
99 |
-
else:
|
100 |
-
return output_path, None, [output_path]
|
101 |
|
102 |
except subprocess.CalledProcessError as e:
|
103 |
error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}"
|
104 |
return error_msg, None, None
|
105 |
|
106 |
-
# Function to download the DXF file from Hugging Face
|
107 |
-
def download_from_huggingface(file_name, folder="datasets/ArrcttacsrjksX/ImageToAutocadData"):
|
108 |
-
try:
|
109 |
-
# Path on Hugging Face Repo
|
110 |
-
file_path = hf_hub_download(
|
111 |
-
repo_id="ArrcttacsrjksX/ImageToAutocadData",
|
112 |
-
filename=f"{folder}/{file_name}",
|
113 |
-
use_auth_token=os.getenv("HF_TOKEN") # Ensure token is available in the environment
|
114 |
-
)
|
115 |
-
return file_path
|
116 |
-
except Exception as e:
|
117 |
-
return str(e)
|
118 |
-
|
119 |
def main():
|
120 |
with gr.Blocks() as demo:
|
121 |
with gr.Tabs():
|
@@ -133,8 +110,8 @@ def main():
|
|
133 |
|
134 |
# Outputs: Debug image and DXF file download link
|
135 |
with gr.Row():
|
136 |
-
debug_output = gr.
|
137 |
-
dxf_output = gr.File(label="DXF File Download")
|
138 |
|
139 |
# Conversion button with event binding
|
140 |
convert_btn = gr.Button("Convert to DXF")
|
@@ -144,28 +121,9 @@ def main():
|
|
144 |
outputs=[dxf_output, debug_output, dxf_output]
|
145 |
)
|
146 |
|
147 |
-
# Download tab for downloading from Hugging Face
|
148 |
-
with gr.TabItem("Download DXF"):
|
149 |
-
gr.Markdown("# Download DXF File from Hugging Face")
|
150 |
-
|
151 |
-
# Input for filename
|
152 |
-
file_name_input = gr.Textbox(label="Enter DXF file name to download", placeholder="e.g., 2024-11-12_15-10-30_output.dxf")
|
153 |
-
|
154 |
-
# Button to trigger download
|
155 |
-
download_btn = gr.Button("Download DXF")
|
156 |
-
|
157 |
-
# Output for download link
|
158 |
-
download_link = gr.File(label="Download DXF")
|
159 |
-
|
160 |
-
download_btn.click(
|
161 |
-
download_from_huggingface,
|
162 |
-
inputs=[file_name_input],
|
163 |
-
outputs=[download_link]
|
164 |
-
)
|
165 |
-
|
166 |
# About tab
|
167 |
with gr.TabItem("About"):
|
168 |
-
gr.Markdown("This Gradio app allows users to convert an image to a DXF file using the SimpleImageToDxfHavePass command-line tool
|
169 |
|
170 |
demo.launch(share=True)
|
171 |
|
|
|
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:
|
|
|
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 |
|
|
|
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():
|
|
|
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")
|
|
|
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 |
|