-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
upload back-end files and organize the front-end files
- Loading branch information
1 parent
a2c0228
commit 3d65506
Showing
101 changed files
with
229 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,29 @@ | ||
# Image-Captioner-Using-Tensoflow | ||
Image-Captioner-Using-Tensoflow | ||
## To add a new page | ||
* Create the html, css, js in the specified folder using the same folder structure. | ||
* Create a new route in the [app.py](./app.py) file with the name you want using only dashes to seperate words. | ||
```PYTHON | ||
@app.route('NEW-ROUTE') | ||
``` | ||
* Define your serving function using a unique name not used before in the whole application. | ||
```PYTHON | ||
def NEW_UNIQUE_NAME(): | ||
``` | ||
* Return your html file path using render_template. | ||
```PYTHON | ||
return render_template('FOLDER_PATH/FILE_PATH.html') | ||
``` | ||
* Your newely created route should look like this. | ||
```PYTHON | ||
@app.route('NEW-ROUTE') | ||
def NEW_UNIQUE_NAME(): | ||
return render_template('FOLDER_PATH/FILE_PATH.html') | ||
``` | ||
|
||
## To run the development server | ||
* Open git bash terminal | ||
```bash | ||
FLASK_APP=app.py | ||
FLASK_ENV=development | ||
flask run --reload | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import os | ||
from flask import Flask, request, render_template, redirect, abort, jsonify, flash, url_for | ||
from flask_cors import CORS | ||
from sqlalchemy import or_ | ||
import numpy as np | ||
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3' | ||
import tensorflow as tf | ||
from werkzeug.utils import secure_filename | ||
from flask import send_from_directory | ||
from keras.preprocessing import image | ||
import matplotlib.pyplot as plt | ||
|
||
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif']) | ||
|
||
def allowed_file(filename): | ||
return '.' in filename and \ | ||
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS | ||
|
||
|
||
def create_app(test_config=None): | ||
# Create and configure the app | ||
app = Flask(__name__) | ||
app.config.from_pyfile('settings.py') | ||
#setup_db(app) | ||
CORS(app, resources={r"/api/*": {"origins": "*"}}) | ||
|
||
@app.after_request | ||
def after_request(response): | ||
response.headers.add('Access-Control-Allow-Headers', 'Content-Type, Authorization,true') | ||
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE, OPTIONS') | ||
return response | ||
|
||
@app.route("/") | ||
def landing_page(): | ||
return render_template("pages/index.html") | ||
|
||
@app.route("/about") | ||
def about_page(): | ||
return render_template("pages/about.html") | ||
|
||
@app.route("/prediction", methods=["POST", "GET"]) | ||
def prediction_page(): | ||
# check if the post request has the file part | ||
if request.method == 'POST': | ||
if 'files' not in request.files: | ||
flash('No file part') | ||
file = request.files['files'] | ||
# if user does not select file, browser also | ||
# submit an empty part without filename | ||
if file.filename == '': | ||
flash('No selected file') | ||
if file and allowed_file(file.filename): | ||
filename = secure_filename(file.filename) | ||
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) | ||
|
||
model = tf.keras.models.load_model('.\\model_weights.h5') | ||
|
||
path = os.path.join(app.config['UPLOAD_FOLDER'], filename) | ||
img = image.load_img(path, target_size=(224, 224)) | ||
x=image.img_to_array(img) | ||
x /= 255 | ||
x=np.expand_dims(x, axis=0) | ||
images = np.vstack([x]) | ||
|
||
prediction = model.predict(images, batch_size=10) | ||
|
||
return jsonify({ | ||
'prediction': prediction, | ||
'success': True, | ||
#'caption': caption | ||
}), 200 | ||
return jsonify({ | ||
'success': False | ||
}), 405 | ||
|
||
@app.route('/uploads/<filename>') | ||
def uploaded_file(filename): | ||
return send_from_directory(app.config['UPLOAD_FOLDER'], | ||
filename) | ||
|
||
@app.errorhandler(400) | ||
def bad_request(error): | ||
return jsonify({ | ||
'success': False, | ||
'error': 400, | ||
'message': 'bad request' | ||
}), 400 | ||
|
||
@app.errorhandler(404) | ||
def not_found(error): | ||
return render_template('/pages/errors/error.html', data={ | ||
'success': False, | ||
'error': 404, | ||
'description': 'Sorry but the page you are looking for does not exist, have been removed, name changed or is temporarily unavailable.', | ||
'message': 'Page Not Be Found' | ||
}), 404 | ||
|
||
@app.errorhandler(405) | ||
def method_not_allowed(error): | ||
return jsonify({ | ||
'success': False, | ||
'error': 405, | ||
'message': 'method not allowed' | ||
}), 405 | ||
|
||
@app.errorhandler(422) | ||
def unprocessable(error): | ||
return jsonify({ | ||
"success": False, | ||
"error": 422, | ||
"message": "unprocessable" | ||
}), 422 | ||
|
||
@app.errorhandler(500) | ||
def internal_server_error(error): | ||
return jsonify({ | ||
'success': False, | ||
'error': 500, | ||
'message': 'internal server errors' | ||
}), 500 | ||
return app | ||
|
||
app = create_app() | ||
|
||
if __name__ == '__main__': | ||
app.run(host='127.0.0.1', port=4040, debug=True) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
alembic==1.4.3 | ||
Babel==2.9.0 | ||
Flask==2.1.0 | ||
gunicorn==20.0.4 | ||
Flask-Cors==3.0.8 | ||
Flask-RESTful==0.3.7 | ||
requests==2.22.0 | ||
psycopg2 | ||
psycopg2-binary==2.8.2 | ||
Flask-Mail==0.9.1 | ||
python-dotenv | ||
Flask-SQLAlchemy==2.1 | ||
SQLAlchemy==1.3.12 | ||
tensorflow==2.6.0 | ||
keras==2.6.0 | ||
Keras-Preprocessing==1.1.2 | ||
Jinja2==3.0 | ||
matplotlib==3.5.2 | ||
mysql-connector-python==8.0.26 | ||
mysqlclient==2.0.3 | ||
itsdangerous==2.0 | ||
six==1.15.0 | ||
Werkzeug==2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python-3.7.8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import os | ||
from os.path import join, dirname | ||
from dotenv import load_dotenv | ||
|
||
dotenv_path = join(dirname(__file__), 'vars.env') | ||
load_dotenv(dotenv_path) | ||
|
||
UPLOAD_FOLDER = os.environ.get('UPLOAD_FOLDER') | ||
DEBUG = True |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="viewport" content="width=h1, initial-scale=1.0"> | ||
<title>Document</title> | ||
</head> | ||
<body> | ||
<h1>About</h1> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#.env | ||
UPLOAD_FOLDER=.\\static\\uploads |