dev-3 commited on
Commit
ff95253
·
1 Parent(s): 93dd654
ai_med_extract/api/routes.py CHANGED
@@ -160,7 +160,7 @@ def register_routes(app, agents):
160
  PHIScrubberAgent = agents["phi_scrubber"]
161
  SummarizerAgent = agents["summarizer"]
162
  MedicalDataExtractorAgent = agents["medical_data_extractor"]
163
- whisper_model = agents["whisper_model"]()
164
 
165
  @app.route("/upload", methods=["POST"])
166
  def upload_file():
@@ -362,19 +362,28 @@ def register_routes(app, agents):
362
 
363
  @app.route("/transcribe", methods=["POST"])
364
  def transcribe_audio():
365
- if "file" not in request.files:
366
- return jsonify({"error": "No file part"}), 400
367
- file = request.files["file"]
368
- if file.filename == "":
369
- return jsonify({"error": "No selected file"}), 400
370
- temp_path = os.path.join("/tmp", file.filename)
371
- file.save(temp_path)
372
  try:
373
- whisper_model = agents["whisper_model"]()
 
 
 
 
 
 
 
 
 
 
 
 
374
  result = whisper_model.transcribe(temp_path)
375
  os.remove(temp_path)
376
  return jsonify({"transcription": result["text"]}), 200
377
  except Exception as e:
 
 
 
378
  return jsonify({"error": str(e)}), 500
379
 
380
  @app.route("/extract_medical_data", methods=["POST"])
 
160
  PHIScrubberAgent = agents["phi_scrubber"]
161
  SummarizerAgent = agents["summarizer"]
162
  MedicalDataExtractorAgent = agents["medical_data_extractor"]
163
+ whisper_model = agents["whisper_model"] # No longer needs to be called as a function
164
 
165
  @app.route("/upload", methods=["POST"])
166
  def upload_file():
 
362
 
363
  @app.route("/transcribe", methods=["POST"])
364
  def transcribe_audio():
365
+ temp_path = None
 
 
 
 
 
 
366
  try:
367
+ if "file" not in request.files:
368
+ return jsonify({"error": "No file part"}), 400
369
+ file = request.files["file"]
370
+ if file.filename == "":
371
+ return jsonify({"error": "No selected file"}), 400
372
+
373
+ # Use secure filename
374
+ from werkzeug.utils import secure_filename
375
+ import uuid
376
+ temp_filename = f"{uuid.uuid4()}_{secure_filename(file.filename)}"
377
+ temp_path = os.path.join("/tmp", temp_filename)
378
+
379
+ file.save(temp_path)
380
  result = whisper_model.transcribe(temp_path)
381
  os.remove(temp_path)
382
  return jsonify({"transcription": result["text"]}), 200
383
  except Exception as e:
384
+ if temp_path and os.path.exists(temp_path):
385
+ os.remove(temp_path)
386
+ logging.error(f"Transcription failed: {str(e)}", exc_info=True)
387
  return jsonify({"error": str(e)}), 500
388
 
389
  @app.route("/extract_medical_data", methods=["POST"])
ai_med_extract/utils/file_utils.py CHANGED
@@ -6,8 +6,9 @@ from werkzeug.utils import secure_filename
6
  from flask import current_app
7
 
8
  ALLOWED_EXTENSIONS = {"pdf", "jpg", "jpeg", "png", "svg", "docx", "doc", "xlsx", "xls", "wav", "mp3", "m4a", "ogg"}
9
- MAX_SIZE_PDF_DOCS = 1 * 1024 * 1024 * 1024 # 1GB
10
- MAX_SIZE_IMAGES = 500 * 1024 * 1024 # 500MB
 
11
 
12
 
13
  def allowed_file(filename):
@@ -31,6 +32,8 @@ def check_file_size(file):
31
  return False, f"File {file.filename} exceeds 1GB size limit"
32
  elif extension in {"jpg", "jpeg", "png"} and size > MAX_SIZE_IMAGES:
33
  return False, f"Image {file.filename} exceeds 500MB size limit"
 
 
34
  return True, None
35
  except Exception as e:
36
  logging.error(f"Error checking file size: {e}", exc_info=True)
 
6
  from flask import current_app
7
 
8
  ALLOWED_EXTENSIONS = {"pdf", "jpg", "jpeg", "png", "svg", "docx", "doc", "xlsx", "xls", "wav", "mp3", "m4a", "ogg"}
9
+ MAX_SIZE_PDF_DOCS = 1 * 1024 * 1024 * 1024 # 1GB
10
+ MAX_SIZE_IMAGES = 500 * 1024 * 1024 # 500MB
11
+ MAX_SIZE_AUDIO = 100 * 1024 * 1024 # 100MB
12
 
13
 
14
  def allowed_file(filename):
 
32
  return False, f"File {file.filename} exceeds 1GB size limit"
33
  elif extension in {"jpg", "jpeg", "png"} and size > MAX_SIZE_IMAGES:
34
  return False, f"Image {file.filename} exceeds 500MB size limit"
35
+ elif extension in {"wav", "mp3", "m4a", "ogg"} and size > MAX_SIZE_AUDIO:
36
+ return False, f"Audio file {file.filename} exceeds 100MB size limit"
37
  return True, None
38
  except Exception as e:
39
  logging.error(f"Error checking file size: {e}", exc_info=True)