-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (109 loc) · 3.89 KB
/
main.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
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from starlette.exceptions import HTTPException as StarletteHTTPException
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from languia.block_arena import demo
import logging
import gradio as gr
from languia import config
from languia.utils import size_desc, license_desc, license_attrs
app = FastAPI()
app.mount("/assets", StaticFiles(directory="assets"), name="assets")
# app.mount("/arene/custom_components", StaticFiles(directory="custom_components"), name="custom_components")
templates = Jinja2Templates(directory="templates")
# TODO: use gr.set_static_paths(paths=["test/test_files/"])?
gr.set_static_paths(paths=[config.assets_absolute_path])
# broken... using path set up by fastapi instead
logging.info("Allowing assets absolute path: " + config.assets_absolute_path)
# Set authorization credentials
auth = None
# Clashes with hot reloading
# if not config.debug:
# test_all_endpoints(config.controller_url)
demo = demo.queue(
default_concurrency_limit=None,
# default_concurrency_limit=40,
# status_update_rate="auto",
api_open=False,
)
# Should enable queue w/ mount_gradio_app: https://github.com/gradio-app/gradio/issues/8839
# demo.startup_events()
app = gr.mount_gradio_app(
app,
demo,
path="/arene",
root_path="/arene",
# allowed_paths=[config.assets_absolute_path],
allowed_paths=[
config.assets_absolute_path,
"/tmp",
"/tmp/gradio",
"custom_components",
],
show_error=config.debug,
)
@app.get("/", response_class=HTMLResponse)
async def home(request: Request):
return templates.TemplateResponse(
"index.html", {"request": request, "config": config}
)
@app.get("/modeles", response_class=HTMLResponse)
async def models(request: Request):
return templates.TemplateResponse(
"models.html",
{
"request": request,
"config": config,
"models": config.models_extra_info,
"size_desc": size_desc,
"license_desc": license_desc,
"license_attrs": license_attrs,
},
)
@app.get("/a-propos", response_class=HTMLResponse)
async def about(request: Request):
return templates.TemplateResponse(
"about.html",
{"request": request, "config": config},
)
@app.get("/partenaires", response_class=HTMLResponse)
async def partners(request: Request):
return templates.TemplateResponse(
"partners.html",
{"request": request, "config": config},
)
@app.get("/mentions-legales", response_class=HTMLResponse)
async def legal(request: Request):
return templates.TemplateResponse(
"legal.html",
{"request": request, "config": config},
)
@app.get("/donnees-personnelles", response_class=HTMLResponse)
async def policy(request: Request):
return templates.TemplateResponse(
"policy.html",
{"request": request, "config": config},
)
@app.get("/modalites", response_class=HTMLResponse)
async def tos(request: Request):
return templates.TemplateResponse(
"tos.html",
{"request": request, "config": config, "models": config.models_extra_info},
)
@app.get("/accessibilite", response_class=HTMLResponse)
async def accessibility(request: Request):
return templates.TemplateResponse(
"accessibility.html",
{"request": request, "config": config},
)
@app.exception_handler(500)
async def http_exception_handler(request, exc):
return FileResponse("templates/50x.html", status_code=500)
@app.exception_handler(StarletteHTTPException)
async def not_found_handler(request, exc):
return templates.TemplateResponse(
"404.html", {"request": request, "config": config}, status_code=404
)
app = SentryAsgiMiddleware(app)