Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +23 -0
- app.py +93 -0
- requirements.txt +9 -0
Dockerfile
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use a lightweight Python base image
|
2 |
+
FROM python:3.10-slim-buster
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy requirements.txt and install Python dependencies
|
8 |
+
COPY requirements.txt .
|
9 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
10 |
+
|
11 |
+
# Copy the rest of your application code
|
12 |
+
COPY . .
|
13 |
+
|
14 |
+
# Ensure the static/uploads directory exists (if not created by your app)
|
15 |
+
RUN mkdir -p static/uploads
|
16 |
+
|
17 |
+
# Expose the port that Flask will run on (Hugging Face Spaces uses PORT env var)
|
18 |
+
EXPOSE 7860
|
19 |
+
|
20 |
+
# Command to run your Flask application
|
21 |
+
# Hugging Face Spaces will set the PORT environment variable.
|
22 |
+
# Your app.py is already set up to use it (os.environ.get('PORT', 7860)).
|
23 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import numpy as np
|
3 |
+
import tensorflow as tf
|
4 |
+
from flask import Flask, request, render_template, jsonify
|
5 |
+
from tensorflow.keras.utils import load_img, img_to_array
|
6 |
+
from werkzeug.utils import secure_filename
|
7 |
+
from datetime import datetime
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
# Load the trained model
|
12 |
+
MODEL_PATH = r"model.keras" # Update to correct path
|
13 |
+
model = tf.keras.models.load_model(MODEL_PATH)
|
14 |
+
|
15 |
+
# Configurations
|
16 |
+
UPLOAD_FOLDER = os.path.join('static', 'uploads')
|
17 |
+
ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png'}
|
18 |
+
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
19 |
+
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
|
20 |
+
|
21 |
+
def allowed_file(filename):
|
22 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
23 |
+
|
24 |
+
def preprocess_image(image_path):
|
25 |
+
img = load_img(image_path, target_size=(224, 224))
|
26 |
+
img_array = img_to_array(img) / 255.0
|
27 |
+
return np.expand_dims(img_array, axis=0)
|
28 |
+
|
29 |
+
@app.route('/')
|
30 |
+
def index():
|
31 |
+
return render_template('home.html')
|
32 |
+
|
33 |
+
@app.route('/tool')
|
34 |
+
def tool():
|
35 |
+
return render_template('tool.html')
|
36 |
+
|
37 |
+
@app.route('/about')
|
38 |
+
def about():
|
39 |
+
return render_template('about.html')
|
40 |
+
|
41 |
+
@app.route('/contact')
|
42 |
+
def contact():
|
43 |
+
return render_template('contact.html')
|
44 |
+
|
45 |
+
@app.route('/predict', methods=['POST'])
|
46 |
+
def predict():
|
47 |
+
if 'file' not in request.files:
|
48 |
+
return jsonify({'error': 'No files uploaded'}), 400
|
49 |
+
|
50 |
+
files = request.files.getlist('file')
|
51 |
+
if not files or all(f.filename == '' for f in files):
|
52 |
+
return jsonify({'error': 'No files selected'}), 400
|
53 |
+
|
54 |
+
results = []
|
55 |
+
for file in files:
|
56 |
+
if file and allowed_file(file.filename):
|
57 |
+
filename = secure_filename(file.filename)
|
58 |
+
timestamp = datetime.now().strftime("%Y%m%d%H%M%S%f")
|
59 |
+
unique_filename = f"{timestamp}_{filename}"
|
60 |
+
file_path = os.path.join(app.config['UPLOAD_FOLDER'], unique_filename)
|
61 |
+
file.save(file_path)
|
62 |
+
|
63 |
+
try:
|
64 |
+
img_array = preprocess_image(file_path)
|
65 |
+
prediction = model.predict(img_array)[0][0]
|
66 |
+
label = "Dirty" if prediction > 0.5 else "Clean"
|
67 |
+
confidence = prediction if label == "Dirty" else 1 - prediction
|
68 |
+
|
69 |
+
results.append({
|
70 |
+
'label': label,
|
71 |
+
'confidence': f"{confidence:.2%}",
|
72 |
+
'image_url': f"/static/uploads/{unique_filename}"
|
73 |
+
})
|
74 |
+
except Exception as e:
|
75 |
+
results.append({
|
76 |
+
'label': 'Error',
|
77 |
+
'confidence': 'N/A',
|
78 |
+
'image_url': None,
|
79 |
+
'error': str(e)
|
80 |
+
})
|
81 |
+
else:
|
82 |
+
results.append({
|
83 |
+
'label': 'Error',
|
84 |
+
'confidence': 'N/A',
|
85 |
+
'image_url': None,
|
86 |
+
'error': f"Invalid file type: {file.filename}"
|
87 |
+
})
|
88 |
+
|
89 |
+
# Render a results page and pass results into it
|
90 |
+
return render_template('results.html', results=results)
|
91 |
+
|
92 |
+
if __name__ == '__main__':
|
93 |
+
app.run(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Flask
|
2 |
+
tensorflow
|
3 |
+
numpy
|
4 |
+
Pillow
|
5 |
+
requests
|
6 |
+
scikit-learn
|
7 |
+
matplotlib
|
8 |
+
seaborn
|
9 |
+
huggingface_hub
|