-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
61 lines (49 loc) · 1.43 KB
/
app.py
File metadata and controls
61 lines (49 loc) · 1.43 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
import os
import click
import google.generativeai as genai
from flask import Flask
from flask.cli import with_appcontext
from database import init_db, close_db
from views import main_bp
try:
FLASK_KEY = os.environ['FLASK_SECRET_KEY']
except KeyError:
raise RuntimeError(
'FATAL: FLASK_SECRET_KEY environment variable is not set. '
'Run: export FLASK_SECRET_KEY=$(openssl rand -hex 16)'
)
GOOGLE_KEY_ERROR = None
try:
genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
except KeyError:
GOOGLE_KEY_ERROR = (
'Error: GOOGLE_API_KEY environment variable not set. '
'Please set it in your terminal and restart the server.'
)
VALID_MODELS = [
'gemini-2.5-pro',
'gemini-2.5-flash',
'gemini-2.5-flash-lite',
]
CODE_DIR = 'code'
def create_app():
app = Flask(__name__)
app.secret_key = FLASK_KEY
app.config['DATABASE'] = 'app.db'
app.config['GOOGLE_KEY_ERROR'] = GOOGLE_KEY_ERROR
app.config['VALID_MODELS'] = VALID_MODELS
app.config['CODE_DIR'] = CODE_DIR
if not os.path.exists(CODE_DIR):
os.makedirs(CODE_DIR)
@click.command('init-db')
@with_appcontext
def init_db_command():
init_db()
click.echo('Initialized the database.')
app.cli.add_command(init_db_command)
app.teardown_appcontext(close_db)
app.register_blueprint(main_bp)
return app
if __name__ == '__main__':
app = create_app()
app.run(debug=True)