ArrcttacsrjksX commited on
Commit
67629ed
·
verified ·
1 Parent(s): 38ed12e

Upload app(42).py

Browse files
Files changed (1) hide show
  1. Backup/app(42).py +122 -0
Backup/app(42).py ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
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
+ # Execute conversion command
27
+ result = subprocess.run(
28
+ ["./SimpleImageToDxfHavePass", "--use-lines", f"--imagePath={image_path}", f"--outputPath={output_path}", f"--debug-output={debug_output_path}"],
29
+ check=True,
30
+ capture_output=True
31
+ )
32
+
33
+ # Log stdout for debugging
34
+ print(result.stdout.decode('utf-8'))
35
+
36
+ # Check if conversion was successful
37
+ if not os.path.exists(output_path):
38
+ return "Conversion failed: DXF file was not created.", None, None
39
+
40
+ # Prepare folder structure for upload
41
+ date_folder = f"{current_date}"
42
+
43
+ # Hugging Face token
44
+ hf_token = os.getenv("HF_TOKEN")
45
+ if not hf_token:
46
+ return "Hugging Face token not found", None, None
47
+
48
+ # Upload input image, output DXF, and debug image to Hugging Face in a date-based folder
49
+ uploaded_files = []
50
+
51
+ # Upload input image
52
+ uploaded_input = upload_file(
53
+ path_or_fileobj=image_path,
54
+ path_in_repo=f"datasets/ArrcttacsrjksX/ImageToAutocadData/{date_folder}/{os.path.basename(image_path)}",
55
+ repo_id="ArrcttacsrjksX/ImageToAutocadData",
56
+ token=hf_token
57
+ )
58
+ uploaded_files.append(uploaded_input)
59
+
60
+ # Upload DXF output
61
+ uploaded_dxf = upload_file(
62
+ path_or_fileobj=output_path,
63
+ path_in_repo=f"datasets/ArrcttacsrjksX/ImageToAutocadData/{date_folder}/{os.path.basename(output_path)}",
64
+ repo_id="ArrcttacsrjksX/ImageToAutocadData",
65
+ token=hf_token
66
+ )
67
+ uploaded_files.append(uploaded_dxf)
68
+
69
+ # Upload debug image
70
+ uploaded_debug = upload_file(
71
+ path_or_fileobj=debug_output_path,
72
+ path_in_repo=f"datasets/ArrcttacsrjksX/ImageToAutocadData/{date_folder}/{os.path.basename(debug_output_path)}",
73
+ repo_id="ArrcttacsrjksX/ImageToAutocadData",
74
+ token=hf_token
75
+ )
76
+ uploaded_files.append(uploaded_debug)
77
+
78
+ # Return files directly for download in Gradio interface
79
+ return output_path, debug_output_path, uploaded_files
80
+
81
+ except subprocess.CalledProcessError as e:
82
+ error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}"
83
+ return error_msg, None, None
84
+
85
+ def main():
86
+ with gr.Blocks() as demo:
87
+ with gr.Tabs():
88
+ # Tab for conversion
89
+ with gr.TabItem("Image to DXF"):
90
+ gr.Markdown("# SimpleImageToDxfHavePass")
91
+
92
+ # Input row for image and optional output folder
93
+ with gr.Row():
94
+ image_input = gr.Image(type="filepath", label="Input Image (PNG/JPG)")
95
+ output_folder = gr.Textbox(label="Output Folder (optional)", placeholder="Leave blank to use a temporary folder")
96
+
97
+ # Outputs: Debug image and DXF file download link
98
+ with gr.Row():
99
+ debug_output = gr.Image(type="filepath", label="Debug Output Preview")
100
+ dxf_output = gr.File(label="DXF File Download")
101
+
102
+ # Conversion button with event binding
103
+ convert_btn = gr.Button("Convert to DXF")
104
+ convert_btn.click(
105
+ convert_image_to_dxf,
106
+ inputs=[image_input, output_folder],
107
+ outputs=[dxf_output, debug_output, dxf_output]
108
+ )
109
+
110
+ # About tab
111
+ with gr.TabItem("About"):
112
+ gr.Markdown("This Gradio app allows users to convert an image to a DXF file using the SimpleImageToDxfHavePass command-line tool.")
113
+
114
+ demo.launch(share=True)
115
+
116
+ if __name__ == "__main__":
117
+ try:
118
+ subprocess.run(['chmod', '+x', './SimpleImageToDxfHavePass'], check=True)
119
+ except subprocess.CalledProcessError as e:
120
+ print(f"Error setting permissions: {e}")
121
+ exit(1)
122
+ main()