-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
178 lines (152 loc) · 6.67 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
from flask import Flask, render_template, jsonify, request
from extensions import db, login_manager, limiter, get_rate_limit_string
from flask_migrate import Migrate
import os
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
def create_app():
app = Flask(__name__)
# Configuration
app.config['SECRET_KEY'] = os.getenv('SECRET_KEY')
app.config['MAX_CONTENT_LENGTH'] = 25 * 1024 * 1024 # 25MB max file size (matching nginx)
app.config['SQLALCHEMY_DATABASE_URI'] = os.getenv('DB_PATH', 'sqlite:///sketchmaker.db')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['UPLOAD_FOLDER'] = os.path.join(app.root_path, 'static', 'uploads')
app.config['TRAINING_FILES_FOLDER'] = os.path.join(app.root_path, 'static', 'training_files')
# Ensure required environment variables are set
required_vars = ['SECRET_KEY']
missing_vars = [var for var in required_vars if not os.getenv(var)]
if missing_vars:
raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
# Create required directories
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
os.makedirs(app.config['TRAINING_FILES_FOLDER'], exist_ok=True)
os.makedirs(os.path.join(app.root_path, 'static', 'images'), exist_ok=True)
# Initialize extensions
db.init_app(app)
limiter.init_app(app)
login_manager.init_app(app)
migrate = Migrate(app, db)
with app.app_context():
# Import models here to avoid circular imports
from models import User, APIProvider, AIModel, AuthSettings
# Import blueprints
from blueprints.auth import auth_bp
from blueprints.core import core_bp
from blueprints.generate import generate_bp
from blueprints.gallery import gallery_bp
from blueprints.download import download_bp
from blueprints.admin import admin
from blueprints.training import training_bp
from blueprints.banner import banner
from blueprints.image_generator import image_generator_bp
from blueprints.magix import magix_bp
# Register blueprints
app.register_blueprint(auth_bp)
app.register_blueprint(core_bp)
app.register_blueprint(generate_bp)
app.register_blueprint(gallery_bp)
app.register_blueprint(download_bp)
app.register_blueprint(admin)
app.register_blueprint(training_bp)
app.register_blueprint(banner)
app.register_blueprint(image_generator_bp)
app.register_blueprint(magix_bp)
# Error handlers
@app.errorhandler(404)
def page_not_found(e):
return render_template('errors/404.html'), 404
@app.errorhandler(413)
def request_entity_too_large(e):
if request.path.startswith('/api/'):
return jsonify({
"error": "File too large",
"message": "The uploaded file exceeds the maximum allowed size.",
}), 413
return render_template('errors/413.html'), 413
@app.errorhandler(429)
def ratelimit_handler(e):
# Default retry time of 60 seconds if not provided
retry_after = getattr(e, 'retry_after', 60)
if retry_after is None:
retry_after = 60
# For API endpoints, return JSON response
if request.path.startswith('/api/'):
return jsonify({
"error": "Rate limit exceeded",
"message": "Too many requests. Please try again later.",
"retry_after": retry_after
}), 429
# For web pages, render template
return render_template('errors/429.html',
retry_after=retry_after
), 429
@app.errorhandler(500)
def internal_server_error(e):
return render_template('errors/500.html'), 500
# User loader
@login_manager.user_loader
def load_user(user_id):
return db.session.get(User, int(user_id))
def init_api_providers():
"""Initialize default API providers and models"""
# Only initialize if no providers exist
if APIProvider.query.first() is None:
# Create providers
openai = APIProvider(name="OpenAI")
anthropic = APIProvider(name="Anthropic")
gemini = APIProvider(name="Google Gemini")
groq = APIProvider(name="Groq")
db.session.add_all([openai, anthropic, gemini, groq])
db.session.commit()
# OpenAI models
openai_models = [
"gpt-4o",
"gpt-4o-mini",
"o1-preview",
"o1-mini"
]
for model in openai_models:
db.session.add(AIModel(name=model, provider_id=openai.id))
# Anthropic models
anthropic_models = [
"claude-3-5-sonnet-20241022",
"claude-3-5-haiku-20241022",
"claude-3-opus-20240229",
"claude-3-haiku-20240307"
]
for model in anthropic_models:
db.session.add(AIModel(name=model, provider_id=anthropic.id))
# Google Gemini models
gemini_models = [
"gemini-1.5-flash-002",
"gemini-1.5-flash-exp-0827",
"gemini-1.5-flash-8b-exp-0827",
"gemini-1.5-pro-002",
"gemini-1.5-pro-exp-0827"
]
for model in gemini_models:
db.session.add(AIModel(name=model, provider_id=gemini.id))
# Groq models
groq_models = [
"llama-3.1-70b-versatile",
"llama-3.1-8b-instant",
"llama-3.2-11b-text-preview",
"llama-3.2-11b-vision-preview",
"llama-3.2-1b-preview",
"llama-3.2-3b-preview",
"llama-3.2-90b-text-preview",
"llama-3.2-90b-vision-preview"
]
for model in groq_models:
db.session.add(AIModel(name=model, provider_id=groq.id))
db.session.commit()
# Create database tables and initialize providers
db.create_all()
init_api_providers()
return app
# Create the application instance for gunicorn
app = create_app()
if __name__ == '__main__':
app.run(debug=True)