Yash goyal commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, render_template, request, jsonify
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
@@ -14,9 +14,9 @@ logger = logging.getLogger(__name__)
|
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
| 17 |
-
# Use relative paths for deployment
|
| 18 |
MODEL_PATH = "skin_lesion_model.h5"
|
| 19 |
HISTORY_PATH = "training_history.pkl"
|
|
|
|
| 20 |
|
| 21 |
# Load model
|
| 22 |
try:
|
|
@@ -42,9 +42,7 @@ else:
|
|
| 42 |
# Plot accuracy history
|
| 43 |
if "accuracy" in history_dict and "val_accuracy" in history_dict:
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
os.makedirs(plot_dir, exist_ok=True) # ✅ ensure directory is writable
|
| 47 |
-
|
| 48 |
plt.plot(history_dict['accuracy'], label='Train Accuracy')
|
| 49 |
plt.plot(history_dict['val_accuracy'], label='Val Accuracy')
|
| 50 |
plt.xlabel('Epochs')
|
|
@@ -52,10 +50,9 @@ if "accuracy" in history_dict and "val_accuracy" in history_dict:
|
|
| 52 |
plt.title('Training History')
|
| 53 |
plt.legend()
|
| 54 |
plt.grid(True)
|
| 55 |
-
|
| 56 |
-
plt.savefig(plot_path) # ✅ safe full path
|
| 57 |
plt.close()
|
| 58 |
-
logger.info("Generated training history plot at %s",
|
| 59 |
except Exception as e:
|
| 60 |
logger.error("Failed to generate training plot: %s", str(e))
|
| 61 |
|
|
@@ -87,7 +84,11 @@ def preprocess_image(image_bytes):
|
|
| 87 |
@app.route("/form", methods=["GET"])
|
| 88 |
def form():
|
| 89 |
logger.info("Serving form page at /form")
|
| 90 |
-
return render_template("form.html", history_plot="/
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
@app.route("/predict", methods=["POST"])
|
| 93 |
def predict():
|
|
@@ -124,6 +125,6 @@ def predict():
|
|
| 124 |
logger.error("Error processing image: %s", str(e))
|
| 125 |
return jsonify({"error": f"Error processing image: {str(e)}"}), 500
|
| 126 |
|
| 127 |
-
# Required
|
| 128 |
if __name__ == "__main__":
|
| 129 |
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, send_file
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
|
|
|
| 14 |
|
| 15 |
app = Flask(__name__)
|
| 16 |
|
|
|
|
| 17 |
MODEL_PATH = "skin_lesion_model.h5"
|
| 18 |
HISTORY_PATH = "training_history.pkl"
|
| 19 |
+
PLOT_PATH = "/tmp/static/training_plot.png"
|
| 20 |
|
| 21 |
# Load model
|
| 22 |
try:
|
|
|
|
| 42 |
# Plot accuracy history
|
| 43 |
if "accuracy" in history_dict and "val_accuracy" in history_dict:
|
| 44 |
try:
|
| 45 |
+
os.makedirs("/tmp/static", exist_ok=True)
|
|
|
|
|
|
|
| 46 |
plt.plot(history_dict['accuracy'], label='Train Accuracy')
|
| 47 |
plt.plot(history_dict['val_accuracy'], label='Val Accuracy')
|
| 48 |
plt.xlabel('Epochs')
|
|
|
|
| 50 |
plt.title('Training History')
|
| 51 |
plt.legend()
|
| 52 |
plt.grid(True)
|
| 53 |
+
plt.savefig(PLOT_PATH)
|
|
|
|
| 54 |
plt.close()
|
| 55 |
+
logger.info("Generated training history plot at %s", PLOT_PATH)
|
| 56 |
except Exception as e:
|
| 57 |
logger.error("Failed to generate training plot: %s", str(e))
|
| 58 |
|
|
|
|
| 84 |
@app.route("/form", methods=["GET"])
|
| 85 |
def form():
|
| 86 |
logger.info("Serving form page at /form")
|
| 87 |
+
return render_template("form.html", history_plot="/training_plot.png")
|
| 88 |
+
|
| 89 |
+
@app.route("/training_plot.png")
|
| 90 |
+
def training_plot():
|
| 91 |
+
return send_file(PLOT_PATH, mimetype='image/png')
|
| 92 |
|
| 93 |
@app.route("/predict", methods=["POST"])
|
| 94 |
def predict():
|
|
|
|
| 125 |
logger.error("Error processing image: %s", str(e))
|
| 126 |
return jsonify({"error": f"Error processing image: {str(e)}"}), 500
|
| 127 |
|
| 128 |
+
# Required for Hugging Face Spaces
|
| 129 |
if __name__ == "__main__":
|
| 130 |
app.run(host="0.0.0.0", port=7860)
|