Download app.py from helviz/swin-api: direct link, hf CLI and curl.
- Browser
- Download file 3.4 kB
-
https://huggingface.co/spaces/helviz/swin-api/resolve/main/app.py
- Command line
-
hf download hf://spaces/helviz/swin-api/app.py
-
curl -L -o app.py https://huggingface.co/spaces/helviz/swin-api/resolve/main/app.py
3.4 kB
| from flask import Flask, request, jsonify | |
| from flask.views import MethodView | |
| from predictions import predict | |
| import os | |
| import logging | |
| import sys | |
| # Configure logging with a safe file path | |
| log_file_path = os.getenv('LOG_FILE_PATH', '/tmp/app.log') # Allow configuration via env | |
| try: | |
| log_dir = os.path.dirname(log_file_path) | |
| if log_dir: | |
| os.makedirs(log_dir, exist_ok=True) | |
| logging.basicConfig( | |
| level=logging.DEBUG, | |
| format='%(asctime)s %(levelname)s [%(module)s] %(message)s', | |
| handlers=[ | |
| logging.FileHandler(log_file_path), | |
| logging.StreamHandler(sys.stdout) | |
| ] | |
| ) | |
| except PermissionError as e: | |
| logging.basicConfig( | |
| level=logging.DEBUG, | |
| format='%(asctime)s %(levelname)s [%(module)s] %(message)s', | |
| handlers=[ | |
| logging.StreamHandler(sys.stdout) | |
| ] | |
| ) | |
| logging.warning(f"Failed to initialize file logging at {log_file_path}: {str(e)}. Using console logging only.") | |
| logger = logging.getLogger(__name__) | |
| app = Flask(__name__) | |
| # Add a root endpoint for debugging | |
| def home(): | |
| logger.info("Root endpoint accessed") | |
| return jsonify({"message": "Diabetic Retinopathy Diagnosis API", "endpoints": ["/predict", "/health"]}), 200 | |
| # Health check endpoint | |
| def health_check(): | |
| logger.debug("Health check requested") | |
| return jsonify({"status": "ok"}), 200 | |
| class PredictAPI(MethodView): | |
| def post(self): | |
| logger.info("Received POST request to /predict") | |
| if 'image' not in request.files: | |
| logger.error("No image provided in request") | |
| return jsonify({"error": "No image provided"}), 400 | |
| image = request.files['image'] | |
| if image.filename == '': | |
| logger.error("No selected image in request") | |
| return jsonify({"error": "No selected image"}), 400 | |
| logger.debug(f"Received image: {image.filename}") | |
| # Save the image temporarily | |
| image_path = os.path.join("/tmp", "temp_image.jpg") | |
| try: | |
| image.save(image_path) | |
| logger.info(f"Saved temporary image to {image_path}") | |
| except Exception as e: | |
| logger.error(f"Failed to save image: {str(e)}") | |
| return jsonify({"error": "Failed to save image"}), 500 | |
| # Perform prediction | |
| try: | |
| result = predict(image_path) | |
| logger.info(f"Prediction successful: {result['class']}") | |
| except Exception as e: | |
| logger.error(f"Prediction failed: {str(e)}") | |
| return jsonify({"error": "Prediction failed"}), 500 | |
| finally: | |
| try: | |
| if os.path.exists(image_path): | |
| os.remove(image_path) | |
| logger.debug(f"Removed temporary image: {image_path}") | |
| except Exception as e: | |
| logger.warning(f"Failed to remove temporary image: {str(e)}") | |
| return jsonify(result) | |
| # Register the endpoint | |
| app.add_url_rule('/predict', view_func=PredictAPI.as_view('predict_api')) | |
| # Optional: Keep for local debugging, but not used in Hugging Face | |
| if __name__ == '__main__': | |
| logger.info("Starting Flask application for local debugging") | |
| port = int(os.getenv('PORT', 7860)) # Default to 7860 for consistency | |
| app.run(host='0.0.0.0', port=port, debug=True) |