|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
""" |
|
|
app.py |
|
|
============================================================================= |
|
|
This is the main entry point for the Flask web application. |
|
|
It acts as a server that: |
|
|
1. Serves the webpage (`index.html`) to the user's browser. |
|
|
2. Receives images uploaded by the user via the `/cartoonize` route. |
|
|
3. Sends those images to the `backend.py` module for processing. |
|
|
4. Returns the cartoonized result back to the user. |
|
|
============================================================================= |
|
|
""" |
|
|
import os |
|
|
import io |
|
|
from flask import Flask, request, send_file |
|
|
from backend import Cartoonizer |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app = Flask(__name__, static_folder='static', template_folder='.') |
|
|
|
|
|
|
|
|
|
|
|
MODEL_PATH = os.path.join(os.path.dirname(__file__), 'src', 'saved_models') |
|
|
|
|
|
print(f"Loading model from {MODEL_PATH}...") |
|
|
print("Please wait, this may take a few seconds...") |
|
|
|
|
|
|
|
|
|
|
|
cartoonizer = Cartoonizer(MODEL_PATH) |
|
|
|
|
|
@app.route('/') |
|
|
def index(): |
|
|
""" |
|
|
Route: Homepage (/) |
|
|
Purpose: Serves the main website (index.html). |
|
|
""" |
|
|
return send_file('index.html') |
|
|
|
|
|
@app.errorhandler(404) |
|
|
def page_not_found(e): |
|
|
""" |
|
|
Route: 404 Error |
|
|
Purpose: Serves the custom cinematic 404 page. |
|
|
""" |
|
|
return send_file('404.html'), 404 |
|
|
|
|
|
@app.route('/cartoonize', methods=['POST']) |
|
|
def cartoonize(): |
|
|
""" |
|
|
Route: /cartoonize |
|
|
Method: POST (Sending data to server) |
|
|
Purpose: Accepts an uploaded image, runs the AI model, and returns the result. |
|
|
""" |
|
|
|
|
|
if 'file' not in request.files: |
|
|
return "No file part", 400 |
|
|
|
|
|
file = request.files['file'] |
|
|
|
|
|
|
|
|
if file.filename == '': |
|
|
return "No selected file", 400 |
|
|
|
|
|
try: |
|
|
|
|
|
image_bytes = file.read() |
|
|
|
|
|
|
|
|
result_bytes = cartoonizer.predict(image_bytes) |
|
|
|
|
|
|
|
|
return send_file( |
|
|
io.BytesIO(result_bytes), |
|
|
mimetype='image/jpeg' |
|
|
) |
|
|
except Exception as e: |
|
|
print(f"Error processing image: {e}") |
|
|
return str(e), 500 |
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
|
|
|
print("Server starting on http://localhost:7860") |
|
|
app.run(host='0.0.0.0', debug=False, port=7860) |
|
|
|