File size: 2,164 Bytes
b81c7ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import os
from flask import Flask, flash, request, redirect, url_for,render_template
from werkzeug.utils import secure_filename
from model import check
from PIL import Image


UPLOAD_FOLDER = 'static'
ALLOWED_EXTENSIONS = {'png', 'jpg', 'jpeg'}

app = Flask(__name__)
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route("/")
def home():
    static_folder = app.static_folder
    for filename in os.listdir(static_folder):
        file_path = os.path.join(static_folder, filename)
        if os.path.isfile(file_path):
            os.remove(file_path)
    return render_template("index.html")

@app.route("/edit", methods = {"GET","POST"})
def edit():
    if request.method == "POST":
        static_folder = app.static_folder
        for filename in os.listdir(static_folder):
            file_path = os.path.join(static_folder, filename)
            if os.path.isfile(file_path):
                os.remove(file_path)
        if 'file' not in request.files:
            flash('No file part')
            return "error"
        file = request.files['file']
        # If the user does not select a file, the browser submits an
        # empty file without a filename.
        if file.filename == '':
            flash('No selected file')
            return render_template("index.html")
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            image = Image.open(file)
            pre = check(image)
            if(pre >= 0.55):
                flash('REAL IMAGE')
            else:
                flash('AI GENERATED IMAGE')
            return render_template("index.html", filename = filename)
        else:
            flash('Allowed file types are: png, jpg, jpeg')

    return render_template("index.html")

@app.route("/display/<filename>")
def display_image(filename):
    return redirect(url_for('static', filename = filename), code = 301)

app.run(debug=True)