From e79dcb1b48f1017e462a3e1ca5f21ce5445ddc89 Mon Sep 17 00:00:00 2001 From: zjw49246 <695849246@qq.com> Date: Mon, 26 Jun 2023 20:27:31 -0700 Subject: [PATCH] fix bugs of baidu stats --- main.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index cd65ecd..e59ea02 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ import os; os.environ['no_proxy'] = '*' # 避免代理网络产生意外污染 +from pathlib import Path def main(): import gradio as gr @@ -56,7 +57,16 @@ def main(): baidu_stats_code = Path('./sites/baidu_stats.html').read_text() with gr.Blocks(title="ChatGPT 学术优化", theme=set_theme, analytics_enabled=False, css=advanced_css) as demo: # Insert your Baidu statistics code here - gr.HTML(baidu_stats_code) + gradio_original_template_fn = gr.routes.templates.TemplateResponse + + def gradio_new_template_fn(*args, **kwargs): + res = gradio_original_template_fn(*args, **kwargs) + res.body = res.body.replace(b'', f'{baidu_stats_code}'.encode("utf8")) + res.init_headers() + return res + + gr.routes.templates.TemplateResponse = gradio_new_template_fn # override gradio template + # Insert Title gr.HTML(title_html) cookies = gr.State({'api_key': API_KEY, 'llm_model': LLM_MODEL})