-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
229 lines (173 loc) · 7.52 KB
/
app.py
File metadata and controls
229 lines (173 loc) · 7.52 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import email,os
from unittest import result
from pymongo import MongoClient
import random
import math
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from flask import Flask,flash, render_template, request,redirect, url_for
import requests as r
from werkzeug.utils import secure_filename
from lobe import ImageModel
import wikipediaapi
def generate_otp():
digits = [i for i in range(0, 10)]
global otp_code
random_str = ""
for i in range(6):
index = math.floor(random.random()*10)
random_str += str(digits[index])
otp_code = random_str
return random_str
def send_mail(receiver_mail, message):
sender_email = "Your gmail"
password = "Your password"
msg = MIMEMultipart()
msg["Subject"] = "Email Authentication"
msg["From"] = "Your gmail"
msg["To"] = "Your gmail"
message1 = '''Dear User,
Thank you for visting our website.Use the following OTP to complete your Sign Up procedures. OTP is valid for 5 minutes '''
body = message1 + str(message)
msg.attach(MIMEText(body, 'plain'))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(sender_email, password)
text = msg.as_string()
server.sendmail(sender_email, receiver_mail, text)
server.quit()
app = Flask(__name__)
UPLOAD_FOLDER = 'static/images/uploads/'
app.secret_key = "secret key"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def show():
return render_template("login_logout.html")
@app.route('/homepage', methods=['GET', 'POST'])
def homepage():
if request.method == 'POST':
if request.form['signin'] == 'signin':
send_usermail = request.form['signemail']
send_password = request.form['signpassword']
cluster = MongoClient(
"mongodb+srv://sahilshukla:[email protected]/myFirstDatabase?retryWrites=true&w=majority")
db = cluster["detailsdb"]
collection = db["loginDetails"]
post = {"name": send_usermail, "password": send_password}
collection.insert_one(post)
results = collection.find({"name": send_usermail})
return render_template("homepage.html")
if request.form['signin'] == 'signup':
send_username = request.form['username']
send_email = request.form['email']
send_signpass = request.form['password']
cluster =MongoClient(
"mongodb+srv://sahilshukla:[email protected]/myFirstDatabase?retryWrites=true&w=majority")
db = cluster["detailsdb"]
collection = db["loginDetails"]
post = {"username": send_username,"email":send_email,"password":send_signpass}
collection.insert_one(post)
return render_template("index.html")
return render_template("homepage.html")
@app.route('/email_otp', methods=['GET', 'POST'])
def email_authentication():
global email
email = request.form['email']
send_mail(email, generate_otp())
return render_template("email_otp.html", user_email=email)
@app.route('/resend_otp', methods=['GET', 'POST'])
def otp_resend():
send_mail(email, generate_otp())
return render_template("email_otp.html", user_email=email, resend_notice="Sending the otp Again")
@app.route('/success', methods=['GET', 'POST'])
def success():
return render_template("homepage.html")
@app.route('/validate', methods=['POST'])
def validate():
otp_got_from_website = request.form['number1']
if int(otp_code) == int(otp_got_from_website):
return render_template("homepage.html")
return render_template("email_otp.html", msg="Not verified try Again:(",user_email=email)
@app.route('/index')
def login():
return render_template("index.html")
@app.route('/search')
def search():
return render_template("search.html")
# @app.route('/search',methods= ['POST'])
# def predict_default():
# return render_template("search.html")
@app.route('/predict',methods= ['POST'])
def predict():
if request.form['submit'] == 'file':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No image selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
else:
flash('Allowed image types are - png, jpg, jpeg, gif')
return redirect(request.url)
model = ImageModel.load("model")
given_path = r"static\images\uploads"
file_path_from_file = os.path.join(given_path,filename)
result = model.predict_from_file(file_path_from_file)
for label, confidence in result.labels:
# added condition such that only some contents are visible
if int(confidence*100) > 50:
# print(f"{label}: {confidence*100}%")
global name,name_confidence
name = label
name_confidence = confidence*100
if request.form['submit'] =='url':
got_url = request.form['get_url']
get_response=r.get(got_url)
global image_name
image_name=got_url.split("/")[-1]
os.chdir('static/images/uploads/')
with open(image_name,"wb") as file_output:
file_output.write(get_response.content)
file_output.close()
for i in range(1,4):
os.chdir('..')
model = ImageModel.load("model")
result = model.predict_from_url(got_url)
for label, confidence in result.labels:
# added condition such that only some contents are visible
if int(confidence*100) > 50:
# print(f"{label}: {confidence*100}%")
name = label
name_confidence = confidence*100
wiki_wiki = wikipediaapi.Wikipedia('en')
page_py = wiki_wiki.page(name)
global summary
summary = page_py.summary[0:680]
return render_template("content.html",name = name,image_name = image_name, confidence = "{:.2f}".format(name_confidence))
wiki_wiki = wikipediaapi.Wikipedia('en')
page_py = wiki_wiki.page(name)
summary = page_py.summary[0:680]
return render_template("content.html",name = name,filename = filename,confidence = "{:.2f}".format(name_confidence))
@app.route('/display/<filename>')
def display_image(filename):
return redirect(url_for('static', filename='images/uploads/' + filename), code=301)
@app.route('/display/<image_name>')
def display_image_url(image_name):
return redirect(url_for('static', filename='images/uploads/' + image_name), code=301)
@app.route('/summary')
def summary():
return render_template("summary.html",summary = summary,name = name.capitalize())
@app.route('/aboutus')
def aboutus():
return render_template("aboutus.html")
if __name__ == "__main__":
app.run(debug=True)