ArrcttacsrjksX commited on
Commit
cccfef2
·
verified ·
1 Parent(s): 24e1083

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -6
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  import tempfile
5
  import datetime
6
  import shutil
7
- from huggingface_hub import upload_file
8
 
9
  def convert_image_to_dxf(image_file, output_folder=None, use_lines=False):
10
  try:
@@ -50,10 +50,6 @@ def convert_image_to_dxf(image_file, output_folder=None, use_lines=False):
50
  if not os.path.exists(output_path):
51
  return "Conversion failed: DXF file was not created.", None, None
52
 
53
- # Check if the output DXF file exists
54
- if not os.path.isfile(output_path):
55
- return f"Error: The DXF file was not found at {output_path}.", None, None
56
-
57
  # Clean up temporary folder
58
  if output_folder and os.path.isdir(output_folder):
59
  shutil.rmtree(output_folder)
@@ -107,6 +103,19 @@ def convert_image_to_dxf(image_file, output_folder=None, use_lines=False):
107
  error_msg = f"Error converting image to DXF: {e.stderr.decode('utf-8') if e.stderr else e}"
108
  return error_msg, None, None
109
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
  def main():
111
  with gr.Blocks() as demo:
112
  with gr.Tabs():
@@ -135,9 +144,28 @@ def main():
135
  outputs=[dxf_output, debug_output, dxf_output]
136
  )
137
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  # About tab
139
  with gr.TabItem("About"):
140
- gr.Markdown("This Gradio app allows users to convert an image to a DXF file using the SimpleImageToDxfHavePass command-line tool.")
141
 
142
  demo.launch(share=True)
143
 
 
4
  import tempfile
5
  import datetime
6
  import shutil
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:
 
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)
 
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():
 
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 and download DXF files from Hugging Face.")
169
 
170
  demo.launch(share=True)
171