File size: 4,519 Bytes
0bd6c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
#!/usr/bin/env python3
"""
Simple API server to serve medical history data and images
Provides endpoints for the frontend to access real history data
"""
import json
import os
from pathlib import Path
from flask import Flask, jsonify, send_file, request
from flask_cors import CORS
app = Flask(__name__)
CORS(app)
# Base paths
UPLOADS_DIR = Path("uploads")
FRONTEND_DIR = Path("frontend")
@app.route('/api/history/<user_id>')
def get_user_history(user_id):
"""Get medical history for a specific user"""
try:
user_folder = UPLOADS_DIR / user_id
history_file = user_folder / 'medical_history.json'
if not history_file.exists():
return jsonify({
"pregnancyRisk": [],
"fetalClassification": [],
"total": 0
})
with open(history_file, 'r') as f:
history = json.load(f)
# Filter by type
pregnancy_risk = [entry for entry in history if entry.get('type') == 'pregnancy_risk']
fetal_classification = [entry for entry in history if entry.get('type') == 'fetal_classification']
return jsonify({
"pregnancyRisk": pregnancy_risk,
"fetalClassification": fetal_classification,
"total": len(history)
})
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/uploads/<user_id>/<filename>')
def serve_user_image(user_id, filename):
"""Serve images from user-specific folders"""
try:
file_path = UPLOADS_DIR / user_id / filename
if file_path.exists():
return send_file(file_path)
else:
return "File not found", 404
except Exception as e:
return str(e), 500
@app.route('/api/users')
def list_users():
"""List all users who have history data"""
try:
users = []
for user_folder in UPLOADS_DIR.glob("user_*"):
if user_folder.is_dir():
history_file = user_folder / 'medical_history.json'
if history_file.exists():
users.append(user_folder.name)
return jsonify(users)
except Exception as e:
return jsonify({"error": str(e)}), 500
@app.route('/api/cleanup/<user_id>')
def cleanup_user_data(user_id):
"""Clean up old files and remove duplicates for a user"""
try:
user_folder = UPLOADS_DIR / user_id
if not user_folder.exists():
return jsonify({"message": "User folder not found"})
# Remove duplicate images (same size and name pattern)
image_files = list(user_folder.glob("*.png")) + list(user_folder.glob("*.jpg")) + list(user_folder.glob("*.jpeg"))
duplicates_removed = 0
file_groups = {}
# Group files by their base name (without timestamp)
for file_path in image_files:
# Extract base name after timestamp
name_parts = file_path.name.split('_', 2)
if len(name_parts) >= 3:
base_name = name_parts[2] # Everything after timestamp
else:
base_name = file_path.name
if base_name not in file_groups:
file_groups[base_name] = []
file_groups[base_name].append(file_path)
# Keep only the newest file for each group
for base_name, files in file_groups.items():
if len(files) > 1:
# Sort by modification time, keep newest
files.sort(key=lambda f: f.stat().st_mtime, reverse=True)
for old_file in files[1:]:
old_file.unlink()
duplicates_removed += 1
return jsonify({
"message": f"Cleanup completed. Removed {duplicates_removed} duplicate files.",
"duplicates_removed": duplicates_removed
})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
# Ensure uploads directory exists
UPLOADS_DIR.mkdir(exist_ok=True)
print("Starting Medical History API Server...")
print("Endpoints:")
print(" GET /api/history/<user_id> - Get user's medical history")
print(" GET /uploads/<user_id>/<filename> - Serve user images")
print(" GET /api/users - List users with history data")
print(" GET /api/cleanup/<user_id> - Clean up duplicate files")
app.run(host='0.0.0.0', port=8503, debug=True) |