JunchuanYu commited on
Commit
4d6b2bb
1 Parent(s): 384f1ca

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +55 -6
utils.py CHANGED
@@ -1,23 +1,22 @@
1
- # -*- coding:utf-8 -*-
2
- from __future__ import annotations
3
- from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type
4
  import logging
5
  import json
6
  import os
7
-
8
  import csv
9
  import requests
10
  import re
11
-
12
  import gradio as gr
13
  from pypinyin import lazy_pinyin
14
  import tiktoken
15
  import mdtex2html
 
16
  from markdown import markdown
17
  from pygments import highlight
18
  from pygments.lexers import get_lexer_by_name
19
  from pygments.formatters import HtmlFormatter
 
20
 
 
21
  use_websearch_checkbox=False
22
  use_streaming_checkbox=True
23
  model_select_dropdown="gpt-3.5-turbo"
@@ -61,7 +60,6 @@ The default model role of the app is the original assistant of ChatGPT, but you
61
 
62
  MODELS = ["gpt-3.5-turbo", "gpt-3.5-turbo-0301",]
63
 
64
-
65
  if TYPE_CHECKING:
66
  from typing import TypedDict
67
 
@@ -309,5 +307,56 @@ def submit_key(key):
309
  logging.info(msg)
310
  return key, msg
311
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
312
 
313
 
 
 
 
 
1
  import logging
2
  import json
3
  import os
4
+ import pathlib
5
  import csv
6
  import requests
7
  import re
 
8
  import gradio as gr
9
  from pypinyin import lazy_pinyin
10
  import tiktoken
11
  import mdtex2html
12
+ from typing import TYPE_CHECKING, Any, Callable, Dict, List, Tuple, Type
13
  from markdown import markdown
14
  from pygments import highlight
15
  from pygments.lexers import get_lexer_by_name
16
  from pygments.formatters import HtmlFormatter
17
+ from gradio.themes.utils import ThemeAsset
18
 
19
+
20
  use_websearch_checkbox=False
21
  use_streaming_checkbox=True
22
  model_select_dropdown="gpt-3.5-turbo"
 
60
 
61
  MODELS = ["gpt-3.5-turbo", "gpt-3.5-turbo-0301",]
62
 
 
63
  if TYPE_CHECKING:
64
  from typing import TypedDict
65
 
 
307
  logging.info(msg)
308
  return key, msg
309
 
310
+ def create_theme_dropdown():
311
+ import gradio as gr
312
+
313
+ asset_path = pathlib.Path(__file__).parent / "themes"
314
+ themes = []
315
+ for theme_asset in os.listdir(str(asset_path)):
316
+ themes.append(
317
+ (ThemeAsset(theme_asset), gr.Theme.load(str(asset_path / theme_asset)))
318
+ )
319
+
320
+ def make_else_if(theme_asset):
321
+ return f"""
322
+ else if (theme == '{str(theme_asset[0].version)}') {{
323
+ var theme_css = `{theme_asset[1]._get_theme_css()}`
324
+ }}"""
325
+
326
+ head, tail = themes[0], themes[1:]
327
+ if_statement = f"""
328
+ if (theme == "{str(head[0].version)}") {{
329
+ var theme_css = `{head[1]._get_theme_css()}`
330
+ }} {" ".join(make_else_if(t) for t in tail)}
331
+ """
332
+
333
+ latest_to_oldest = sorted([t[0] for t in themes], key=lambda asset: asset.version)[
334
+ ::-1
335
+ ]
336
+ latest_to_oldest = [str(t.version) for t in latest_to_oldest]
337
+
338
+ component = gr.Dropdown(
339
+ choices=latest_to_oldest,
340
+ value=latest_to_oldest[0],
341
+ render=False,
342
+ label="Select Version",
343
+ ).style(container=False)
344
+
345
+ return (
346
+ component,
347
+ f"""
348
+ (theme) => {{
349
+ if (!document.querySelector('.theme-css')) {{
350
+ var theme_elem = document.createElement('style');
351
+ theme_elem.classList.add('theme-css');
352
+ document.head.appendChild(theme_elem);
353
+ }} else {{
354
+ var theme_elem = document.querySelector('.theme-css');
355
+ }}
356
+ {if_statement}
357
+ theme_elem.innerHTML = theme_css;
358
+ }}
359
+ """,
360
+ )
361
 
362