-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
203 lines (163 loc) · 6.16 KB
/
app.py
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
import os
from flask import Flask, flash, request, jsonify, render_template, redirect, session, url_for
from forms import RegistrationForm, LoginForm
from flask_bcrypt import Bcrypt
import base64
import requests
from PIL import Image
import io
import numpy as np
from datetime import datetime
import sqlite3
from dotenv import load_dotenv
load_dotenv()
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key' # Replace with a secure key
bcrypt = Bcrypt(app)
DATABASE = 'users.db'
def create_user_table():
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL
)
''')
conn.commit()
conn.close()
create_user_table()
def encode_image_to_base64(image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
img_str = base64.b64encode(buffered.getvalue()).decode()
return f"data:image/jpeg;base64,{img_str}"
def is_skin_image(image):
if image.mode != 'RGB':
image = image.convert('RGB')
img_array = np.array(image)
avg_color = np.mean(img_array, axis=(0, 1))
skin_lower = np.array([120, 80, 60])
skin_upper = np.array([240, 200, 180])
return np.all(avg_color >= skin_lower) and np.all(avg_color <= skin_upper)
@app.route('/')
def index():
#return render_template('index.html')
username = session.get('username')
if not username:
flash('You need to log in first.', 'danger')
return redirect(url_for('login'))
return render_template('index.html', username=username)
@app.route('/signup', methods=['GET', 'POST'])
def signup():
form = RegistrationForm()
if form.validate_on_submit():
username = form.username.data
email = form.email.data
password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
try:
cursor.execute('INSERT INTO users (username, email, password) VALUES (?, ?, ?)', (username, email, password))
conn.commit()
flash('Account created successfully! You can now log in.', 'success')
return redirect(url_for('login'))
except sqlite3.IntegrityError:
flash('Email already exists. Please use a different email.', 'danger')
finally:
conn.close()
return render_template('signup.html', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
email = form.email.data
password = form.password.data
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute('SELECT * FROM users WHERE email = ?', (email,))
user = cursor.fetchone()
conn.close()
if user and bcrypt.check_password_hash(user[3], password):
session['user_id'] = user[0]
session['username'] = user[1]
flash('You have successfully logged in!', 'success')
return redirect(url_for('index'))
else:
flash('Login unsuccessful. Check email and password.', 'danger')
return render_template('login.html', form=form)
@app.route('/logout')
def logout():
session.clear()
flash('You have been logged out.', 'info')
return redirect(url_for('login'))
@app.route('/results')
def result():
return render_template('results.html')
@app.route('/history')
def history():
conn = sqlite3.connect('skin_kare.db')
cursor = conn.cursor()
cursor.execute('SELECT diagnosis, treatment, date FROM analyses ORDER BY date DESC')
analyses = cursor.fetchall()
conn.close()
return render_template('historyPage.html', analyses=analyses)
@app.route('/clear_history', methods=['POST'])
def clear_history():
conn = sqlite3.connect('skin_kare.db')
cursor = conn.cursor()
cursor.execute('DELETE FROM analyses')
conn.commit()
conn.close()
return redirect(url_for('history'))
@app.route('/analyze', methods=['POST'])
def analyze_image():
if 'image' in request.files:
file = request.files['image']
image = Image.open(file)
else:
img_data = request.form.get('image_base64').split(",")[1]
image = Image.open(io.BytesIO(base64.b64decode(img_data)))
if not is_skin_image(image):
return jsonify({"error": "The uploaded image is not a skin image."}), 400
image_base64 = encode_image_to_base64(image)
prompt = """
You are a professional dermatologist. Analyze this skin image and provide:
1. The name of the skin condition
2. A description
3. Suggested remedies or treatments
"""
result = call_openrouter_api(image_base64, prompt)
if "error" in result:
return jsonify({"error": result["error"]}), 500
analysis = result['choices'][0]['message']['content']
condition_name = analysis.split('.')[0]
treatment = "Moisturizers and corticosteroid creams"
date = datetime.now().strftime('%m/%d/%Y')
save_analysis(condition_name, treatment, date)
response_data = {
"condition": condition_name,
"analysis": analysis,
"imageBase64": image_base64
}
return jsonify(response_data)
def call_openrouter_api(image_base64, prompt):
url = "https://openrouter.ai/api/v1/chat/completions"
headers = {"Authorization": f"Bearer {os.getenv('OPENROUTER_API_KEY')}", "Content-Type": "application/json"}
payload = {
"model": "meta-llama/llama-3.2-11b-vision-instruct:free",
"messages": [{"role": "user", "content": [{"type": "text", "text": prompt}, {"type": "image_url", "image_url": {"url": image_base64}}]}]
}
response = requests.post(url, headers=headers, json=payload)
response.raise_for_status()
return response.json()
def save_analysis(diagnosis, treatment, date):
conn = sqlite3.connect('skin_kare.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO analyses (diagnosis, treatment, date) VALUES (?, ?, ?)', (diagnosis, treatment, date))
conn.commit()
conn.close()
if __name__ == "__main__":
app.run(debug=True)