WebashalarForML commited on
Commit
d81481c
·
verified ·
1 Parent(s): 5c24c28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -24
app.py CHANGED
@@ -1,4 +1,4 @@
1
- from flask import Flask, request, jsonify, render_template, send_from_directory
2
  import cv2, json,base64,io,os,tempfile,logging, re
3
  import numpy as np
4
  from unstructured.partition.pdf import partition_pdf
@@ -2180,29 +2180,15 @@ def index():
2180
 
2181
  @app.route("/download_sb3/<project_id>", methods=["GET"])
2182
  def download_sb3(project_id):
2183
- """
2184
- Allows users to download the generated .sb3 Scratch project file.
2185
- """
2186
- sb3_filename = f"{project_id}.sb3"
2187
- #sb3_filepath = os.path.join("generated_projects", sb3_filename)
2188
- sb3_filepath = GEN_PROJECT_DIR / sb3_filename
2189
-
2190
- try:
2191
- if os.path.exists(sb3_filepath):
2192
- logger.info(f"Serving SB3 file for project ID: {project_id}")
2193
- # send_from_directory serves the file and handles content-disposition for download
2194
- return send_from_directory(
2195
- directory="generated_projects",
2196
- path=sb3_filename,
2197
- as_attachment=True, # This makes the browser download the file
2198
- download_name=sb3_filename # This sets the filename for the download
2199
- )
2200
- else:
2201
- logger.warning(f"SB3 file not found for ID: {project_id}")
2202
- return jsonify({"error": "Scratch project file not found"}), 404
2203
- except Exception as e:
2204
- logger.error(f"Error serving SB3 file for ID {project_id}: {e}")
2205
- return jsonify({"error": "Failed to retrieve Scratch project file"}), 500
2206
 
2207
  # API endpoint
2208
  @app.route('/process_pdf', methods=['POST'])
 
1
+ from flask import Flask, request, jsonify, render_template, send_from_directory, send_file
2
  import cv2, json,base64,io,os,tempfile,logging, re
3
  import numpy as np
4
  from unstructured.partition.pdf import partition_pdf
 
2180
 
2181
  @app.route("/download_sb3/<project_id>", methods=["GET"])
2182
  def download_sb3(project_id):
2183
+ sb3_path = GEN_PROJECT_DIR / f"{project_id}.sb3"
2184
+ if not sb3_path.exists():
2185
+ return jsonify({"error": "Scratch project file not found"}), 404
2186
+
2187
+ return send_file(
2188
+ sb3_path,
2189
+ as_attachment=True,
2190
+ download_name=sb3_path.name
2191
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2192
 
2193
  # API endpoint
2194
  @app.route('/process_pdf', methods=['POST'])