ArrcttacsrjksX commited on
Commit
66d3400
·
verified ·
1 Parent(s): d00e361

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -5
app.py CHANGED
@@ -2,6 +2,8 @@ import gradio as gr
2
  import subprocess
3
  import os
4
  import tempfile
 
 
5
 
6
  def convert_image_to_dxf(image_file, output_folder=None):
7
  try:
@@ -12,11 +14,14 @@ def convert_image_to_dxf(image_file, output_folder=None):
12
  # Define output folder, using a temp directory if none is specified
13
  if not output_folder:
14
  output_folder = tempfile.mkdtemp()
 
 
15
 
16
- # Prepare paths for DXF and debug output
 
17
  image_path = image_file
18
- output_path = os.path.join(output_folder, os.path.splitext(os.path.basename(image_path))[0] + ".dxf")
19
- debug_output_path = os.path.join(output_folder, os.path.splitext(os.path.basename(image_path))[0] + "_debug.png")
20
 
21
  # Execute conversion command
22
  result = subprocess.run(
@@ -25,11 +30,54 @@ def convert_image_to_dxf(image_file, output_folder=None):
25
  capture_output=True
26
  )
27
 
 
 
 
28
  # Check if conversion was successful
29
  if not os.path.exists(output_path):
30
  return "Conversion failed: DXF file was not created.", None, None
31
 
32
- return f"DXF file saved to: {output_path}", debug_output_path, output_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  except subprocess.CalledProcessError as e:
35
  error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}"
@@ -50,7 +98,7 @@ def main():
50
  # Outputs: Debug image and DXF file download link
51
  with gr.Row():
52
  debug_output = gr.Image(type="filepath", label="Debug Output Preview")
53
- dxf_output = gr.File(label="DXF File Download")
54
 
55
  # Conversion button with event binding
56
  convert_btn = gr.Button("Convert to DXF")
 
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:
 
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(
 
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
+ hf_token = os.getenv("HF_TOKEN")
44
+ if not hf_token:
45
+ return "Hugging Face token not found", None, None
46
+
47
+ # Upload input image, output DXF, and debug image to Hugging Face in a date-based folder
48
+ uploaded_files = []
49
+
50
+ # Upload input image
51
+ uploaded_input = upload_file(
52
+ path_or_fileobj=image_path,
53
+ path_in_repo=f"ImageToAutocadData/{date_folder}/{os.path.basename(image_path)}",
54
+ repo_id="ArrcttacsrjksX/ImageToAutocadData",
55
+ token=hf_token
56
+ )
57
+ uploaded_files.append(uploaded_input)
58
+
59
+ # Upload DXF output
60
+ uploaded_dxf = upload_file(
61
+ path_or_fileobj=output_path,
62
+ path_in_repo=f"ImageToAutocadData/{date_folder}/{os.path.basename(output_path)}",
63
+ repo_id="ArrcttacsrjksX/ImageToAutocadData",
64
+ token=hf_token
65
+ )
66
+ uploaded_files.append(uploaded_dxf)
67
+
68
+ # Upload debug image
69
+ uploaded_debug = upload_file(
70
+ path_or_fileobj=debug_output_path,
71
+ path_in_repo=f"ImageToAutocadData/{date_folder}/{os.path.basename(debug_output_path)}",
72
+ repo_id="ArrcttacsrjksX/ImageToAutocadData",
73
+ token=hf_token
74
+ )
75
+ uploaded_files.append(uploaded_debug)
76
+
77
+ # Generate download links for each uploaded file
78
+ links = [f"https://huggingface.co/{uploaded_file.repo_id}/blob/main/{uploaded_file.path_in_repo}" for uploaded_file in uploaded_files]
79
+
80
+ return f"Files uploaded successfully. Download links:\n" + "\n".join(links), debug_output_path, output_path
81
 
82
  except subprocess.CalledProcessError as e:
83
  error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}"
 
98
  # Outputs: Debug image and DXF file download link
99
  with gr.Row():
100
  debug_output = gr.Image(type="filepath", label="Debug Output Preview")
101
+ dxf_output = gr.Textbox(label="DXF File Download Link", placeholder="The link will appear here")
102
 
103
  # Conversion button with event binding
104
  convert_btn = gr.Button("Convert to DXF")