CoolFace
Apppublic

SoDa12321/ChatGPT_for_Academic_Releases1

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
theme.py354 linesDownload Raw Back to root
1import gradio as gr2from toolbox import get_conf3CODE_HIGHLIGHT, ADD_WAIFU = get_conf('CODE_HIGHLIGHT', 'ADD_WAIFU')4# gradio可用颜色列表5# gr.themes.utils.colors.slate (石板色)6# gr.themes.utils.colors.gray (灰色)7# gr.themes.utils.colors.zinc (锌色)8# gr.themes.utils.colors.neutral (中性色)9# gr.themes.utils.colors.stone (石头色)10# gr.themes.utils.colors.red (红色)11# gr.themes.utils.colors.orange (橙色)12# gr.themes.utils.colors.amber (琥珀色)13# gr.themes.utils.colors.yellow (黄色)14# gr.themes.utils.colors.lime (酸橙色)15# gr.themes.utils.colors.green (绿色)16# gr.themes.utils.colors.emerald (祖母绿)17# gr.themes.utils.colors.teal (青蓝色)18# gr.themes.utils.colors.cyan (青色)19# gr.themes.utils.colors.sky (天蓝色)20# gr.themes.utils.colors.blue (蓝色)21# gr.themes.utils.colors.indigo (靛蓝色)22# gr.themes.utils.colors.violet (紫罗兰色)23# gr.themes.utils.colors.purple (紫色)24# gr.themes.utils.colors.fuchsia (洋红色)25# gr.themes.utils.colors.pink (粉红色)26# gr.themes.utils.colors.rose (玫瑰色)27 28 29def adjust_theme():30 31    try:32        color_er = gr.themes.utils.colors.fuchsia33        set_theme = gr.themes.Default(34            primary_hue=gr.themes.utils.colors.sky,35            neutral_hue=gr.themes.utils.colors.fuchsia,36            font=["sans-serif", "Microsoft YaHei", "ui-sans-serif", "system-ui",37                  "sans-serif", gr.themes.utils.fonts.GoogleFont("Source Sans Pro")],38            font_mono=["ui-monospace", "Consolas", "monospace", gr.themes.utils.fonts.GoogleFont("IBM Plex Mono")])39        set_theme.set(40            # Colors41            input_background_fill_dark="*neutral_800",42            # Transition43            button_transition="none",44            # Shadows45            button_shadow="*shadow_drop",46            button_shadow_hover="*shadow_drop_lg",47            button_shadow_active="*shadow_inset",48            input_shadow="0 0 0 *shadow_spread transparent, *shadow_inset",49            input_shadow_focus="0 0 0 *shadow_spread *secondary_50, *shadow_inset",50            input_shadow_focus_dark="0 0 0 *shadow_spread *neutral_700, *shadow_inset",51            checkbox_label_shadow="*shadow_drop",52            block_shadow="*shadow_drop",53            form_gap_width="1px",54            # Button borders55            input_border_width="1px",56            input_background_fill="white",57            # Gradients58            stat_background_fill="linear-gradient(to right, *primary_400, *primary_200)",59            stat_background_fill_dark="linear-gradient(to right, *primary_400, *primary_600)",60            error_background_fill=f"linear-gradient(to right, {color_er.c100}, *background_fill_secondary)",61            error_background_fill_dark="*background_fill_primary",62            checkbox_label_background_fill="linear-gradient(to top, *neutral_50, white)",63            checkbox_label_background_fill_dark="linear-gradient(to top, *neutral_900, *neutral_800)",64            checkbox_label_background_fill_hover="linear-gradient(to top, *neutral_100, white)",65            checkbox_label_background_fill_hover_dark="linear-gradient(to top, *neutral_900, *neutral_800)",66            button_primary_background_fill="linear-gradient(to bottom right, *primary_100, *primary_300)",67            button_primary_background_fill_dark="linear-gradient(to bottom right, *primary_500, *primary_600)",68            button_primary_background_fill_hover="linear-gradient(to bottom right, *primary_100, *primary_200)",69            button_primary_background_fill_hover_dark="linear-gradient(to bottom right, *primary_500, *primary_500)",70            button_primary_border_color_dark="*primary_500",71            button_secondary_background_fill="linear-gradient(to bottom right, *neutral_100, *neutral_200)",72            button_secondary_background_fill_dark="linear-gradient(to bottom right, *neutral_600, *neutral_700)",73            button_secondary_background_fill_hover="linear-gradient(to bottom right, *neutral_100, *neutral_100)",74            button_secondary_background_fill_hover_dark="linear-gradient(to bottom right, *neutral_600, *neutral_600)",75            button_cancel_background_fill=f"linear-gradient(to bottom right, {color_er.c100}, {color_er.c200})",76            button_cancel_background_fill_dark=f"linear-gradient(to bottom right, {color_er.c600}, {color_er.c700})",77            button_cancel_background_fill_hover=f"linear-gradient(to bottom right, {color_er.c100}, {color_er.c100})",78            button_cancel_background_fill_hover_dark=f"linear-gradient(to bottom right, {color_er.c600}, {color_er.c600})",79            button_cancel_border_color=color_er.c200,80            button_cancel_border_color_dark=color_er.c600,81            button_cancel_text_color=color_er.c600,82            button_cancel_text_color_dark="white",83        )84 85        # 添加一个萌萌的看板娘86        if ADD_WAIFU:87            js = """88                <script src="file=docs/waifu_plugin/jquery.min.js"></script>89                <script src="file=docs/waifu_plugin/jquery-ui.min.js"></script>90                <script src="file=docs/waifu_plugin/autoload.js"></script>91            """92            gradio_original_template_fn = gr.routes.templates.TemplateResponse93            def gradio_new_template_fn(*args, **kwargs):94                res = gradio_original_template_fn(*args, **kwargs)95                res.body = res.body.replace(b'</html>', f'{js}</html>'.encode("utf8"))96                res.init_headers()97                return res98            gr.routes.templates.TemplateResponse = gradio_new_template_fn   # override gradio template99    except:100        set_theme = None101        print('gradio版本较旧, 不能自定义字体和颜色')102    return set_theme103 104 105advanced_css = """106.markdown-body table {107    margin: 1em 0;108    border-collapse: collapse;109    empty-cells: show;110}111 112.markdown-body th, .markdown-body td {113    border: 1.2px solid var(--border-color-primary);114    padding: 5px;115}116 117.markdown-body thead {118    background-color: rgba(175,184,193,0.2);119}120 121.markdown-body thead th {122    padding: .5em .2em;123}124 125.markdown-body ol, .markdown-body ul {126    padding-inline-start: 2em !important;127}128 129/* chat box. */130[class *= "message"] {131    border-radius: var(--radius-xl) !important;132    /* padding: var(--spacing-xl) !important; */133    /* font-size: var(--text-md) !important; */134    /* line-height: var(--line-md) !important; */135    /* min-height: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl)); */136    /* min-width: calc(var(--text-md)*var(--line-md) + 2*var(--spacing-xl)); */137}138[data-testid = "bot"] {139    max-width: 95%;140    /* width: auto !important; */141    border-bottom-left-radius: 0 !important;142}143[data-testid = "user"] {144    max-width: 100%;145    /* width: auto !important; */146    border-bottom-right-radius: 0 !important;147}148 149/* linein code block. */150.markdown-body code {151    display: inline;152    white-space: break-spaces;153    border-radius: 6px;154    margin: 0 2px 0 2px;155    padding: .2em .4em .1em .4em;156    background-color: rgba(13, 17, 23, 0.95);157    color: #c9d1d9;158}159 160.dark .markdown-body code {161    display: inline;162    white-space: break-spaces;163    border-radius: 6px;164    margin: 0 2px 0 2px;165    padding: .2em .4em .1em .4em;166    background-color: rgba(175,184,193,0.2);167}168 169/* code block css */170.markdown-body pre code {171    display: block;172    overflow: auto;173    white-space: pre;174    background-color: rgba(13, 17, 23, 0.95);175    border-radius: 10px;176    padding: 1em;177    margin: 1em 2em 1em 0.5em;178}179 180.dark .markdown-body pre code {181    display: block;182    overflow: auto;183    white-space: pre;184    background-color: rgba(175,184,193,0.2);185    border-radius: 10px;186    padding: 1em;187    margin: 1em 2em 1em 0.5em;188}189 190"""191 192if CODE_HIGHLIGHT:193    advanced_css += """194 195.codehilite .hll { background-color: #6e7681 }196.codehilite .c { color: #8b949e; font-style: italic } /* Comment */197.codehilite .err { color: #f85149 } /* Error */198.codehilite .esc { color: #c9d1d9 } /* Escape */199.codehilite .g { color: #c9d1d9 } /* Generic */200.codehilite .k { color: #ff7b72 } /* Keyword */201.codehilite .l { color: #a5d6ff } /* Literal */202.codehilite .n { color: #c9d1d9 } /* Name */203.codehilite .o { color: #ff7b72; font-weight: bold } /* Operator */204.codehilite .x { color: #c9d1d9 } /* Other */205.codehilite .p { color: #c9d1d9 } /* Punctuation */206.codehilite .ch { color: #8b949e; font-style: italic } /* Comment.Hashbang */207.codehilite .cm { color: #8b949e; font-style: italic } /* Comment.Multiline */208.codehilite .cp { color: #8b949e; font-weight: bold; font-style: italic } /* Comment.Preproc */209.codehilite .cpf { color: #8b949e; font-style: italic } /* Comment.PreprocFile */210.codehilite .c1 { color: #8b949e; font-style: italic } /* Comment.Single */211.codehilite .cs { color: #8b949e; font-weight: bold; font-style: italic } /* Comment.Special */212.codehilite .gd { color: #ffa198; background-color: #490202 } /* Generic.Deleted */213.codehilite .ge { color: #c9d1d9; font-style: italic } /* Generic.Emph */214.codehilite .gr { color: #ffa198 } /* Generic.Error */215.codehilite .gh { color: #79c0ff; font-weight: bold } /* Generic.Heading */216.codehilite .gi { color: #56d364; background-color: #0f5323 } /* Generic.Inserted */217.codehilite .go { color: #8b949e } /* Generic.Output */218.codehilite .gp { color: #8b949e } /* Generic.Prompt */219.codehilite .gs { color: #c9d1d9; font-weight: bold } /* Generic.Strong */220.codehilite .gu { color: #79c0ff } /* Generic.Subheading */221.codehilite .gt { color: #ff7b72 } /* Generic.Traceback */222.codehilite .g-Underline { color: #c9d1d9; text-decoration: underline } /* Generic.Underline */223.codehilite .kc { color: #79c0ff } /* Keyword.Constant */224.codehilite .kd { color: #ff7b72 } /* Keyword.Declaration */225.codehilite .kn { color: #ff7b72 } /* Keyword.Namespace */226.codehilite .kp { color: #79c0ff } /* Keyword.Pseudo */227.codehilite .kr { color: #ff7b72 } /* Keyword.Reserved */228.codehilite .kt { color: #ff7b72 } /* Keyword.Type */229.codehilite .ld { color: #79c0ff } /* Literal.Date */230.codehilite .m { color: #a5d6ff } /* Literal.Number */231.codehilite .s { color: #a5d6ff } /* Literal.String */232.codehilite .na { color: #c9d1d9 } /* Name.Attribute */233.codehilite .nb { color: #c9d1d9 } /* Name.Builtin */234.codehilite .nc { color: #f0883e; font-weight: bold } /* Name.Class */235.codehilite .no { color: #79c0ff; font-weight: bold } /* Name.Constant */236.codehilite .nd { color: #d2a8ff; font-weight: bold } /* Name.Decorator */237.codehilite .ni { color: #ffa657 } /* Name.Entity */238.codehilite .ne { color: #f0883e; font-weight: bold } /* Name.Exception */239.codehilite .nf { color: #d2a8ff; font-weight: bold } /* Name.Function */240.codehilite .nl { color: #79c0ff; font-weight: bold } /* Name.Label */241.codehilite .nn { color: #ff7b72 } /* Name.Namespace */242.codehilite .nx { color: #c9d1d9 } /* Name.Other */243.codehilite .py { color: #79c0ff } /* Name.Property */244.codehilite .nt { color: #7ee787 } /* Name.Tag */245.codehilite .nv { color: #79c0ff } /* Name.Variable */246.codehilite .ow { color: #ff7b72; font-weight: bold } /* Operator.Word */247.codehilite .pm { color: #c9d1d9 } /* Punctuation.Marker */248.codehilite .w { color: #6e7681 } /* Text.Whitespace */249.codehilite .mb { color: #a5d6ff } /* Literal.Number.Bin */250.codehilite .mf { color: #a5d6ff } /* Literal.Number.Float */251.codehilite .mh { color: #a5d6ff } /* Literal.Number.Hex */252.codehilite .mi { color: #a5d6ff } /* Literal.Number.Integer */253.codehilite .mo { color: #a5d6ff } /* Literal.Number.Oct */254.codehilite .sa { color: #79c0ff } /* Literal.String.Affix */255.codehilite .sb { color: #a5d6ff } /* Literal.String.Backtick */256.codehilite .sc { color: #a5d6ff } /* Literal.String.Char */257.codehilite .dl { color: #79c0ff } /* Literal.String.Delimiter */258.codehilite .sd { color: #a5d6ff } /* Literal.String.Doc */259.codehilite .s2 { color: #a5d6ff } /* Literal.String.Double */260.codehilite .se { color: #79c0ff } /* Literal.String.Escape */261.codehilite .sh { color: #79c0ff } /* Literal.String.Heredoc */262.codehilite .si { color: #a5d6ff } /* Literal.String.Interpol */263.codehilite .sx { color: #a5d6ff } /* Literal.String.Other */264.codehilite .sr { color: #79c0ff } /* Literal.String.Regex */265.codehilite .s1 { color: #a5d6ff } /* Literal.String.Single */266.codehilite .ss { color: #a5d6ff } /* Literal.String.Symbol */267.codehilite .bp { color: #c9d1d9 } /* Name.Builtin.Pseudo */268.codehilite .fm { color: #d2a8ff; font-weight: bold } /* Name.Function.Magic */269.codehilite .vc { color: #79c0ff } /* Name.Variable.Class */270.codehilite .vg { color: #79c0ff } /* Name.Variable.Global */271.codehilite .vi { color: #79c0ff } /* Name.Variable.Instance */272.codehilite .vm { color: #79c0ff } /* Name.Variable.Magic */273.codehilite .il { color: #a5d6ff } /* Literal.Number.Integer.Long */274 275.dark .codehilite .hll { background-color: #2C3B41 }276.dark .codehilite .c { color: #79d618; font-style: italic } /* Comment */277.dark .codehilite .err { color: #FF5370 } /* Error */278.dark .codehilite .esc { color: #89DDFF } /* Escape */279.dark .codehilite .g { color: #EEFFFF } /* Generic */280.dark .codehilite .k { color: #BB80B3 } /* Keyword */281.dark .codehilite .l { color: #C3E88D } /* Literal */282.dark .codehilite .n { color: #EEFFFF } /* Name */283.dark .codehilite .o { color: #89DDFF } /* Operator */284.dark .codehilite .p { color: #89DDFF } /* Punctuation */285.dark .codehilite .ch { color: #79d618; font-style: italic } /* Comment.Hashbang */286.dark .codehilite .cm { color: #79d618; font-style: italic } /* Comment.Multiline */287.dark .codehilite .cp { color: #79d618; font-style: italic } /* Comment.Preproc */288.dark .codehilite .cpf { color: #79d618; font-style: italic } /* Comment.PreprocFile */289.dark .codehilite .c1 { color: #79d618; font-style: italic } /* Comment.Single */290.dark .codehilite .cs { color: #79d618; font-style: italic } /* Comment.Special */291.dark .codehilite .gd { color: #FF5370 } /* Generic.Deleted */292.dark .codehilite .ge { color: #89DDFF } /* Generic.Emph */293.dark .codehilite .gr { color: #FF5370 } /* Generic.Error */294.dark .codehilite .gh { color: #C3E88D } /* Generic.Heading */295.dark .codehilite .gi { color: #C3E88D } /* Generic.Inserted */296.dark .codehilite .go { color: #79d618 } /* Generic.Output */297.dark .codehilite .gp { color: #FFCB6B } /* Generic.Prompt */298.dark .codehilite .gs { color: #FF5370 } /* Generic.Strong */299.dark .codehilite .gu { color: #89DDFF } /* Generic.Subheading */300.dark .codehilite .gt { color: #FF5370 } /* Generic.Traceback */301.dark .codehilite .kc { color: #89DDFF } /* Keyword.Constant */302.dark .codehilite .kd { color: #BB80B3 } /* Keyword.Declaration */303.dark .codehilite .kn { color: #89DDFF; font-style: italic } /* Keyword.Namespace */304.dark .codehilite .kp { color: #89DDFF } /* Keyword.Pseudo */305.dark .codehilite .kr { color: #BB80B3 } /* Keyword.Reserved */306.dark .codehilite .kt { color: #BB80B3 } /* Keyword.Type */307.dark .codehilite .ld { color: #C3E88D } /* Literal.Date */308.dark .codehilite .m { color: #F78C6C } /* Literal.Number */309.dark .codehilite .s { color: #C3E88D } /* Literal.String */310.dark .codehilite .na { color: #BB80B3 } /* Name.Attribute */311.dark .codehilite .nb { color: #82AAFF } /* Name.Builtin */312.dark .codehilite .nc { color: #FFCB6B } /* Name.Class */313.dark .codehilite .no { color: #EEFFFF } /* Name.Constant */314.dark .codehilite .nd { color: #82AAFF } /* Name.Decorator */315.dark .codehilite .ni { color: #89DDFF } /* Name.Entity */316.dark .codehilite .ne { color: #FFCB6B } /* Name.Exception */317.dark .codehilite .nf { color: #82AAFF } /* Name.Function */318.dark .codehilite .nl { color: #82AAFF } /* Name.Label */319.dark .codehilite .nn { color: #FFCB6B } /* Name.Namespace */320.dark .codehilite .nx { color: #EEFFFF } /* Name.Other */321.dark .codehilite .py { color: #FFCB6B } /* Name.Property */322.dark .codehilite .nt { color: #FF5370 } /* Name.Tag */323.dark .codehilite .nv { color: #89DDFF } /* Name.Variable */324.dark .codehilite .ow { color: #89DDFF; font-style: italic } /* Operator.Word */325.dark .codehilite .pm { color: #89DDFF } /* Punctuation.Marker */326.dark .codehilite .w { color: #EEFFFF } /* Text.Whitespace */327.dark .codehilite .mb { color: #F78C6C } /* Literal.Number.Bin */328.dark .codehilite .mf { color: #F78C6C } /* Literal.Number.Float */329.dark .codehilite .mh { color: #F78C6C } /* Literal.Number.Hex */330.dark .codehilite .mi { color: #F78C6C } /* Literal.Number.Integer */331.dark .codehilite .mo { color: #F78C6C } /* Literal.Number.Oct */332.dark .codehilite .sa { color: #BB80B3 } /* Literal.String.Affix */333.dark .codehilite .sb { color: #C3E88D } /* Literal.String.Backtick */334.dark .codehilite .sc { color: #C3E88D } /* Literal.String.Char */335.dark .codehilite .dl { color: #EEFFFF } /* Literal.String.Delimiter */336.dark .codehilite .sd { color: #79d618; font-style: italic } /* Literal.String.Doc */337.dark .codehilite .s2 { color: #C3E88D } /* Literal.String.Double */338.dark .codehilite .se { color: #EEFFFF } /* Literal.String.Escape */339.dark .codehilite .sh { color: #C3E88D } /* Literal.String.Heredoc */340.dark .codehilite .si { color: #89DDFF } /* Literal.String.Interpol */341.dark .codehilite .sx { color: #C3E88D } /* Literal.String.Other */342.dark .codehilite .sr { color: #89DDFF } /* Literal.String.Regex */343.dark .codehilite .s1 { color: #C3E88D } /* Literal.String.Single */344.dark .codehilite .ss { color: #89DDFF } /* Literal.String.Symbol */345.dark .codehilite .bp { color: #89DDFF } /* Name.Builtin.Pseudo */346.dark .codehilite .fm { color: #82AAFF } /* Name.Function.Magic */347.dark .codehilite .vc { color: #89DDFF } /* Name.Variable.Class */348.dark .codehilite .vg { color: #89DDFF } /* Name.Variable.Global */349.dark .codehilite .vi { color: #89DDFF } /* Name.Variable.Instance */350.dark .codehilite .vm { color: #82AAFF } /* Name.Variable.Magic */351.dark .codehilite .il { color: #F78C6C } /* Literal.Number.Integer.Long */352 353"""354