Spaces:
Paused
Paused
dev-3 commited on
Commit ·
7125064
1
Parent(s): 55c56a0
issue fixes
Browse files
.env
CHANGED
|
@@ -3,4 +3,4 @@ TRANSFORMERS_CACHE=/tmp/huggingface
|
|
| 3 |
XDG_CACHE_HOME=/tmp
|
| 4 |
TORCH_HOME=/tmp/torch
|
| 5 |
WHISPER_CACHE=/tmp/whisper
|
| 6 |
-
UPLOAD_DIR=/tmp/uploads
|
|
|
|
| 3 |
XDG_CACHE_HOME=/tmp
|
| 4 |
TORCH_HOME=/tmp/torch
|
| 5 |
WHISPER_CACHE=/tmp/whisper
|
| 6 |
+
UPLOAD_DIR=/tmp/uploads
|
README_SPACES.md
ADDED
|
File without changes
|
ai_med_extract/api/routes.py
CHANGED
|
@@ -505,10 +505,11 @@ def register_routes(app, agents):
|
|
| 505 |
except Exception as e:
|
| 506 |
return jsonify({"error": f"Summarizer model load failed: {str(e)}"}), 500
|
| 507 |
|
| 508 |
-
# Use
|
| 509 |
import uuid
|
| 510 |
from werkzeug.utils import secure_filename
|
| 511 |
-
|
|
|
|
| 512 |
os.makedirs(temp_dir, exist_ok=True)
|
| 513 |
temp_filename = f"{uuid.uuid4()}_{secure_filename(audio_file.filename)}"
|
| 514 |
temp_path = os.path.join(temp_dir, temp_filename)
|
|
@@ -736,158 +737,3 @@ def register_routes(app, agents):
|
|
| 736 |
@app.route("/")
|
| 737 |
def home():
|
| 738 |
return "Medical Data Extraction API is running!", 200
|
| 739 |
-
|
| 740 |
-
@app.route("/extract_medical_data_questions", methods=["POST"])
|
| 741 |
-
def extract_medical_data_questions():
|
| 742 |
-
data = request.json
|
| 743 |
-
qa_model_name = data.get("qa_model_name")
|
| 744 |
-
qa_model_type = data.get("qa_model_type")
|
| 745 |
-
if "extracted_data" not in data:
|
| 746 |
-
return jsonify({"error": "Missing 'extracted_data' in request"}), 400
|
| 747 |
-
if not qa_model_name or not qa_model_type:
|
| 748 |
-
return jsonify({"error": "Missing 'qa_model_name' or 'qa_model_type'"}), 400
|
| 749 |
-
try:
|
| 750 |
-
qa_pipeline = get_qa_pipeline(qa_model_type, qa_model_name)
|
| 751 |
-
except Exception as e:
|
| 752 |
-
return jsonify({"error": f"Could not load model: {str(e)}"}), 500
|
| 753 |
-
questions = {
|
| 754 |
-
"Patient Name": "What is the patient's name?",
|
| 755 |
-
"Age": "What is the patient's age?",
|
| 756 |
-
"Gender": "What is the patient's gender?",
|
| 757 |
-
"Date of Birth": "What is the patient's date of birth?",
|
| 758 |
-
"Patient ID": "What is the patient ID?",
|
| 759 |
-
"Reason for Visit": "What is the reason for the patient's visit?",
|
| 760 |
-
"Physician": "Who is the physician in charge of the patient?",
|
| 761 |
-
"Test Date": "What is the test date?",
|
| 762 |
-
"Hemoglobin": "What is the patient's hemoglobin level?",
|
| 763 |
-
"Blood Glucose (Fasting)": "What is the patient's fasting blood glucose level?",
|
| 764 |
-
"Total Cholesterol": "What is the total cholesterol level?",
|
| 765 |
-
"LDL Cholesterol": "What is the LDL cholesterol level?",
|
| 766 |
-
"HDL Cholesterol": "What is the HDL cholesterol level?",
|
| 767 |
-
"Serum Creatinine": "What is the serum creatinine level?",
|
| 768 |
-
"Vitamin D (25-OH)": "What is the patient's Vitamin D level?",
|
| 769 |
-
"Height": "What is the patient's height?",
|
| 770 |
-
"Weight": "What is the patient's weight?",
|
| 771 |
-
"Blood Pressure (Systolic)": "What is the patient's systolic blood pressure?",
|
| 772 |
-
"Blood Pressure (Diastolic)": "What is the patient's diastolic blood pressure?",
|
| 773 |
-
"Recommendations": "What are the recommendations based on the test results?",
|
| 774 |
-
}
|
| 775 |
-
structured_response = {"extracted_data": []}
|
| 776 |
-
for file_data in data["extracted_data"]:
|
| 777 |
-
filename = file_data.get("file", "unknown_file")
|
| 778 |
-
context = file_data.get("extracted_text", "").strip()
|
| 779 |
-
if not context:
|
| 780 |
-
structured_response["extracted_data"].append(
|
| 781 |
-
{
|
| 782 |
-
"file": filename,
|
| 783 |
-
"medical_terms": "No data extracted",
|
| 784 |
-
"categorized_data": [],
|
| 785 |
-
}
|
| 786 |
-
)
|
| 787 |
-
continue
|
| 788 |
-
extracted_info = {}
|
| 789 |
-
for key, question in questions.items():
|
| 790 |
-
try:
|
| 791 |
-
result = run_qa_pipeline(qa_pipeline, question, context)
|
| 792 |
-
extracted_info[key] = clean_result(
|
| 793 |
-
result.get("answer", "Not Available")
|
| 794 |
-
)
|
| 795 |
-
except Exception:
|
| 796 |
-
extracted_info[key] = "Error extracting"
|
| 797 |
-
categorized_data = [
|
| 798 |
-
{
|
| 799 |
-
"name": "Patient Information",
|
| 800 |
-
"fields": [
|
| 801 |
-
{
|
| 802 |
-
"label": "Patient Name",
|
| 803 |
-
"value": extracted_info.get("Patient Name", ""),
|
| 804 |
-
},
|
| 805 |
-
{
|
| 806 |
-
"label": "Date of Birth",
|
| 807 |
-
"value": extracted_info.get("Date of Birth", ""),
|
| 808 |
-
},
|
| 809 |
-
{"label": "Gender", "value": extracted_info.get("Gender", "")},
|
| 810 |
-
{
|
| 811 |
-
"label": "Patient ID",
|
| 812 |
-
"value": extracted_info.get("Patient ID", ""),
|
| 813 |
-
},
|
| 814 |
-
],
|
| 815 |
-
},
|
| 816 |
-
{
|
| 817 |
-
"name": "Vitals",
|
| 818 |
-
"fields": [
|
| 819 |
-
{"label": "Height", "value": extracted_info.get("Height", "")},
|
| 820 |
-
{"label": "Weight", "value": extracted_info.get("Weight", "")},
|
| 821 |
-
{
|
| 822 |
-
"label": "Blood Pressure",
|
| 823 |
-
"value": f"{extracted_info.get('Blood Pressure (Systolic)', '')}/{extracted_info.get('Blood Pressure (Diastolic)', '')} mmHg",
|
| 824 |
-
},
|
| 825 |
-
{
|
| 826 |
-
"label": "Hemoglobin",
|
| 827 |
-
"value": extracted_info.get("Hemoglobin", ""),
|
| 828 |
-
},
|
| 829 |
-
{
|
| 830 |
-
"label": "Serum Creatinine",
|
| 831 |
-
"value": extracted_info.get("Serum Creatinine", ""),
|
| 832 |
-
},
|
| 833 |
-
],
|
| 834 |
-
},
|
| 835 |
-
{
|
| 836 |
-
"name": "Lab Results",
|
| 837 |
-
"fields": [
|
| 838 |
-
{
|
| 839 |
-
"label": "Blood Glucose (Fasting)",
|
| 840 |
-
"value": extracted_info.get("Blood Glucose (Fasting)", ""),
|
| 841 |
-
},
|
| 842 |
-
{
|
| 843 |
-
"label": "Total Cholesterol",
|
| 844 |
-
"value": extracted_info.get("Total Cholesterol", ""),
|
| 845 |
-
},
|
| 846 |
-
{
|
| 847 |
-
"label": "LDL Cholesterol",
|
| 848 |
-
"value": extracted_info.get("LDL Cholesterol", ""),
|
| 849 |
-
},
|
| 850 |
-
{
|
| 851 |
-
"label": "HDL Cholesterol",
|
| 852 |
-
"value": extracted_info.get("HDL Cholesterol", ""),
|
| 853 |
-
},
|
| 854 |
-
{
|
| 855 |
-
"label": "Vitamin D (25-OH)",
|
| 856 |
-
"value": extracted_info.get("Vitamin D (25-OH)", ""),
|
| 857 |
-
},
|
| 858 |
-
],
|
| 859 |
-
},
|
| 860 |
-
{
|
| 861 |
-
"name": "Medical Notes",
|
| 862 |
-
"fields": [
|
| 863 |
-
{
|
| 864 |
-
"label": "Reason for Visit",
|
| 865 |
-
"value": extracted_info.get("Reason for Visit", ""),
|
| 866 |
-
},
|
| 867 |
-
{
|
| 868 |
-
"label": "Physician",
|
| 869 |
-
"value": extracted_info.get("Physician", ""),
|
| 870 |
-
},
|
| 871 |
-
{
|
| 872 |
-
"label": "Test Date",
|
| 873 |
-
"value": extracted_info.get("Test Date", ""),
|
| 874 |
-
},
|
| 875 |
-
{
|
| 876 |
-
"label": "Recommendations",
|
| 877 |
-
"value": extracted_info.get("Recommendations", ""),
|
| 878 |
-
},
|
| 879 |
-
],
|
| 880 |
-
},
|
| 881 |
-
]
|
| 882 |
-
structured_response["extracted_data"].append(
|
| 883 |
-
{
|
| 884 |
-
"file": filename,
|
| 885 |
-
"medical_terms": extracted_info,
|
| 886 |
-
"categorized_data": categorized_data,
|
| 887 |
-
}
|
| 888 |
-
)
|
| 889 |
-
return jsonify(structured_response), 200
|
| 890 |
-
|
| 891 |
-
@app.route("/")
|
| 892 |
-
def home():
|
| 893 |
-
return "Medical Data Extraction API is running!", 200
|
|
|
|
| 505 |
except Exception as e:
|
| 506 |
return jsonify({"error": f"Summarizer model load failed: {str(e)}"}), 500
|
| 507 |
|
| 508 |
+
# Use platform-agnostic temp directory
|
| 509 |
import uuid
|
| 510 |
from werkzeug.utils import secure_filename
|
| 511 |
+
import tempfile
|
| 512 |
+
temp_dir = os.path.join(tempfile.gettempdir(), 'audio_uploads')
|
| 513 |
os.makedirs(temp_dir, exist_ok=True)
|
| 514 |
temp_filename = f"{uuid.uuid4()}_{secure_filename(audio_file.filename)}"
|
| 515 |
temp_path = os.path.join(temp_dir, temp_filename)
|
|
|
|
| 737 |
@app.route("/")
|
| 738 |
def home():
|
| 739 |
return "Medical Data Extraction API is running!", 200
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ai_med_extract/utils/model_config.py
ADDED
|
File without changes
|
ai_med_extract/utils/model_loader.py
ADDED
|
File without changes
|
ai_med_extract/utils/model_loader_spaces.py
ADDED
|
File without changes
|
ai_med_extract/utils/text_processing.py
ADDED
|
File without changes
|