Spaces:
Running
Running
Deploy frontend
Browse files- __pycache__/app.cpython-312.pyc +0 -0
- app.py +40 -17
__pycache__/app.cpython-312.pyc
ADDED
|
Binary file (11.7 kB). View file
|
|
|
app.py
CHANGED
|
@@ -225,6 +225,7 @@ def build_result_html(data: dict) -> str:
|
|
| 225 |
# App
|
| 226 |
# ---------------------------------------------------------------------------
|
| 227 |
|
|
|
|
| 228 |
def ping() -> str:
|
| 229 |
print("Ping received")
|
| 230 |
return "pong"
|
|
@@ -240,24 +241,50 @@ TITLE_HTML = """\
|
|
| 240 |
"""
|
| 241 |
|
| 242 |
backend_space = os.environ.get("BACKEND_SPACE", "")
|
| 243 |
-
backend_client: Client | None = None
|
| 244 |
|
| 245 |
|
| 246 |
-
def get_client() -> Client:
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 253 |
|
| 254 |
|
| 255 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
text = text.strip()
|
| 257 |
if not text:
|
| 258 |
return build_result_html({})
|
| 259 |
-
|
| 260 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
return build_result_html(data)
|
| 262 |
|
| 263 |
|
|
@@ -271,7 +298,6 @@ text_input = gr.Textbox(
|
|
| 271 |
|
| 272 |
with gr.Blocks(
|
| 273 |
title="Parayomi — ふりがな予測モデル",
|
| 274 |
-
theme=gr.themes.Ocean(),
|
| 275 |
) as demo:
|
| 276 |
gr.HTML(TITLE_HTML)
|
| 277 |
|
|
@@ -288,7 +314,7 @@ with gr.Blocks(
|
|
| 288 |
with gr.Row():
|
| 289 |
submit_btn = gr.Button("予測", variant="primary")
|
| 290 |
|
| 291 |
-
output_html = gr.HTML(label="結果")
|
| 292 |
|
| 293 |
submit_btn.click(
|
| 294 |
fn=process, inputs=[text_input], outputs=output_html, api_visibility="private"
|
|
@@ -297,9 +323,6 @@ with gr.Blocks(
|
|
| 297 |
fn=process, inputs=[text_input], outputs=output_html, api_visibility="private"
|
| 298 |
)
|
| 299 |
|
| 300 |
-
# Run inference on default text at load
|
| 301 |
-
demo.load(fn=process, inputs=[text_input], outputs=output_html)
|
| 302 |
-
|
| 303 |
# Keep-alive endpoint for external cron services
|
| 304 |
gr.Textbox(visible=False).change(fn=ping, inputs=[], outputs=[], api_name="ping")
|
| 305 |
|
|
@@ -311,4 +334,4 @@ if __name__ == "__main__":
|
|
| 311 |
"--share", action="store_true", help="Create a public Gradio link"
|
| 312 |
)
|
| 313 |
args = parser.parse_args()
|
| 314 |
-
demo.launch(share=args.share)
|
|
|
|
| 225 |
# App
|
| 226 |
# ---------------------------------------------------------------------------
|
| 227 |
|
| 228 |
+
|
| 229 |
def ping() -> str:
|
| 230 |
print("Ping received")
|
| 231 |
return "pong"
|
|
|
|
| 241 |
"""
|
| 242 |
|
| 243 |
backend_space = os.environ.get("BACKEND_SPACE", "")
|
|
|
|
| 244 |
|
| 245 |
|
| 246 |
+
def get_client(request: gr.Request) -> Client:
|
| 247 |
+
if not backend_space:
|
| 248 |
+
raise gr.Error("BACKEND_SPACE env var is not set.")
|
| 249 |
+
|
| 250 |
+
headers: dict[str, str] = {}
|
| 251 |
+
if x_ip_token := request.headers.get("x-ip-token"):
|
| 252 |
+
headers["x-ip-token"] = x_ip_token
|
| 253 |
+
|
| 254 |
+
# A Client owns a session hash and request headers. Do not share it across
|
| 255 |
+
# visitors: each request needs its own user identity and queue session.
|
| 256 |
+
return Client(backend_space, headers=headers or None)
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
def _is_rate_limit_error(error: Exception) -> bool:
|
| 260 |
+
response = getattr(error, "response", None)
|
| 261 |
+
return (
|
| 262 |
+
getattr(response, "status_code", None) == 429
|
| 263 |
+
or error.__class__.__name__ == "TooManyRequestsError"
|
| 264 |
+
or "429 Too Many Requests" in str(error)
|
| 265 |
+
)
|
| 266 |
|
| 267 |
|
| 268 |
+
def _rate_limit_html() -> str:
|
| 269 |
+
return (
|
| 270 |
+
'<p style="color:var(--body-text-color-subdued);text-align:center;">'
|
| 271 |
+
"ただいまアクセスが集中しています。少し待ってからもう一度お試しください。"
|
| 272 |
+
"</p>"
|
| 273 |
+
)
|
| 274 |
+
|
| 275 |
+
|
| 276 |
+
def process(text: str, request: gr.Request) -> str:
|
| 277 |
text = text.strip()
|
| 278 |
if not text:
|
| 279 |
return build_result_html({})
|
| 280 |
+
|
| 281 |
+
try:
|
| 282 |
+
client = get_client(request)
|
| 283 |
+
data = client.predict(text, TOP_K, api_name="/predict")
|
| 284 |
+
except Exception as error:
|
| 285 |
+
if _is_rate_limit_error(error):
|
| 286 |
+
return _rate_limit_html()
|
| 287 |
+
raise
|
| 288 |
return build_result_html(data)
|
| 289 |
|
| 290 |
|
|
|
|
| 298 |
|
| 299 |
with gr.Blocks(
|
| 300 |
title="Parayomi — ふりがな予測モデル",
|
|
|
|
| 301 |
) as demo:
|
| 302 |
gr.HTML(TITLE_HTML)
|
| 303 |
|
|
|
|
| 314 |
with gr.Row():
|
| 315 |
submit_btn = gr.Button("予測", variant="primary")
|
| 316 |
|
| 317 |
+
output_html = gr.HTML(value=build_result_html({}), label="結果")
|
| 318 |
|
| 319 |
submit_btn.click(
|
| 320 |
fn=process, inputs=[text_input], outputs=output_html, api_visibility="private"
|
|
|
|
| 323 |
fn=process, inputs=[text_input], outputs=output_html, api_visibility="private"
|
| 324 |
)
|
| 325 |
|
|
|
|
|
|
|
|
|
|
| 326 |
# Keep-alive endpoint for external cron services
|
| 327 |
gr.Textbox(visible=False).change(fn=ping, inputs=[], outputs=[], api_name="ping")
|
| 328 |
|
|
|
|
| 334 |
"--share", action="store_true", help="Create a public Gradio link"
|
| 335 |
)
|
| 336 |
args = parser.parse_args()
|
| 337 |
+
demo.queue(api_open=False).launch(share=args.share, theme=gr.themes.Ocean())
|