ina111 Keldos MZhaovo MZhao commited on
Commit
ce202f9
1 Parent(s): bd5237a

Internationalization (#631)

Browse files

* add internationalization

Perform the following two steps for internationalization.
1. created a locale folder and put json for each language. The json files are loaded by identifying the language from the system language of the PC.
2. Call an instance of I18nAuto class to call the language dictionary in json to display the text displayed in Gradio, the WebUI.

* Minor changes in translation

* Update en_US.json

* Minor changes in translation

* detach html in presets.py for i18n

* `i18n`only needs to be declared once.

* setting language in `config.json`

* fix json read bug

* update translation

Chuanhu told me to translate like this :-)

* update translation

---------

Co-authored-by: Keldos <i@keldos.me>
Co-authored-by: mzlegion <mzhao.ouo@gmail.com>
Co-authored-by: MZhao <70903329+MZhao-ouo@users.noreply.github.com>

ChuanhuChatbot.py CHANGED
@@ -12,6 +12,7 @@ from modules.presets import *
12
  from modules.overwrites import *
13
  from modules.models import get_model
14
 
 
15
  gr.Chatbot._postprocess_chat_messages = postprocess_chat_messages
16
  gr.Chatbot.postprocess = postprocess
17
  PromptHelper.compact_text_chunks = compact_text_chunks
@@ -29,7 +30,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
29
  user_api_key = gr.State(my_api_key)
30
  current_model = gr.State(create_new_model)
31
 
32
- topic = gr.State("未命名对话历史记录")
33
 
34
  with gr.Row():
35
  gr.HTML(CHUANHU_TITLE, elem_id="app_title")
@@ -54,22 +55,22 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
54
  with gr.Column(min_width=225, scale=12):
55
  user_input = gr.Textbox(
56
  elem_id="user_input_tb",
57
- show_label=False, placeholder="在这里输入"
58
  ).style(container=False)
59
  with gr.Column(min_width=42, scale=1):
60
  submitBtn = gr.Button(value="", variant="primary", elem_id="submit_btn")
61
  cancelBtn = gr.Button(value="", variant="secondary", visible=False, elem_id="cancel_btn")
62
  with gr.Row():
63
  emptyBtn = gr.Button(
64
- "🧹 新的对话",
65
  )
66
- retryBtn = gr.Button("🔄 重新生成")
67
- delFirstBtn = gr.Button("🗑️ 删除最旧对话")
68
- delLastBtn = gr.Button("🗑️ 删除最新对话")
69
 
70
  with gr.Column():
71
  with gr.Column(min_width=50, scale=1):
72
- with gr.Tab(label="模型"):
73
  keyTxt = gr.Textbox(
74
  show_label=True,
75
  placeholder=f"OpenAI API-key...",
@@ -79,95 +80,95 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
79
  label="API-Key",
80
  )
81
  if multi_api_key:
82
- usageTxt = gr.Markdown("多账号模式已开启,无需输入key,可直接开始对话", elem_id="usage_display", elem_classes="insert_block")
83
  else:
84
- usageTxt = gr.Markdown("**发送消息** 或 **提交key** 以显示额度", elem_id="usage_display", elem_classes="insert_block")
85
  model_select_dropdown = gr.Dropdown(
86
- label="选择模型", choices=MODELS, multiselect=False, value=MODELS[DEFAULT_MODEL], interactive=True
87
  )
88
  lora_select_dropdown = gr.Dropdown(
89
- label="选择LoRA模型", choices=[], multiselect=False, interactive=True, visible=False
90
  )
91
  with gr.Row():
92
  use_streaming_checkbox = gr.Checkbox(
93
- label="实时传输回答", value=True, visible=ENABLE_STREAMING_OPTION
94
  )
95
- single_turn_checkbox = gr.Checkbox(label="单轮对话", value=False)
96
- use_websearch_checkbox = gr.Checkbox(label="使用在线搜索", value=False)
97
  language_select_dropdown = gr.Dropdown(
98
- label="选择回复语言(针对搜索&索引功能)",
99
  choices=REPLY_LANGUAGES,
100
  multiselect=False,
101
  value=REPLY_LANGUAGES[0],
102
  )
103
- index_files = gr.Files(label="上传索引文件", type="file")
104
- two_column = gr.Checkbox(label="双栏pdf", value=advance_docs["pdf"].get("two_column", False))
105
  # TODO: 公式ocr
106
- # formula_ocr = gr.Checkbox(label="识别公式", value=advance_docs["pdf"].get("formula_ocr", False))
107
 
108
  with gr.Tab(label="Prompt"):
109
  systemPromptTxt = gr.Textbox(
110
  show_label=True,
111
- placeholder=f"在这里输入System Prompt...",
112
  label="System prompt",
113
  value=INITIAL_SYSTEM_PROMPT,
114
  lines=10,
115
  ).style(container=False)
116
- with gr.Accordion(label="加载Prompt模板", open=True):
117
  with gr.Column():
118
  with gr.Row():
119
  with gr.Column(scale=6):
120
  templateFileSelectDropdown = gr.Dropdown(
121
- label="选择Prompt模板集合文件",
122
  choices=get_template_names(plain=True),
123
  multiselect=False,
124
  value=get_template_names(plain=True)[0],
125
  ).style(container=False)
126
  with gr.Column(scale=1):
127
- templateRefreshBtn = gr.Button("🔄 刷新")
128
  with gr.Row():
129
  with gr.Column():
130
  templateSelectDropdown = gr.Dropdown(
131
- label="从Prompt模板中加载",
132
  choices=load_template(
133
  get_template_names(plain=True)[0], mode=1
134
  ),
135
  multiselect=False,
136
  ).style(container=False)
137
 
138
- with gr.Tab(label="保存/加载"):
139
- with gr.Accordion(label="保存/加载对话历史记录", open=True):
140
  with gr.Column():
141
  with gr.Row():
142
  with gr.Column(scale=6):
143
  historyFileSelectDropdown = gr.Dropdown(
144
- label="从列表中加载对话",
145
  choices=get_history_names(plain=True),
146
  multiselect=False,
147
  value=get_history_names(plain=True)[0],
148
  )
149
  with gr.Column(scale=1):
150
- historyRefreshBtn = gr.Button("🔄 刷新")
151
  with gr.Row():
152
  with gr.Column(scale=6):
153
  saveFileName = gr.Textbox(
154
  show_label=True,
155
- placeholder=f"设置文件名: 默认为.json,可选为.md",
156
- label="设置保存文件名",
157
- value="对话历史记录",
158
  ).style(container=True)
159
  with gr.Column(scale=1):
160
- saveHistoryBtn = gr.Button("💾 保存对话")
161
- exportMarkdownBtn = gr.Button("📝 导出为Markdown")
162
- gr.Markdown("默认保存于history文件夹")
163
  with gr.Row():
164
  with gr.Column():
165
  downloadFile = gr.File(interactive=True)
166
 
167
- with gr.Tab(label="高级"):
168
- gr.Markdown("# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置")
169
  gr.HTML(APPEARANCE_SWITCHER, elem_classes="insert_block")
170
- with gr.Accordion("参数", open=False):
171
  temperature_slider = gr.Slider(
172
  minimum=-0,
173
  maximum=2.0,
@@ -194,7 +195,7 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
194
  )
195
  stop_sequence_txt = gr.Textbox(
196
  show_label=True,
197
- placeholder=f"在这里输入停止符,用英文逗号隔开...",
198
  label="stop",
199
  value="",
200
  lines=1,
@@ -240,33 +241,33 @@ with gr.Blocks(css=customCSS, theme=small_and_beautiful_theme) as demo:
240
  )
241
  user_identifier_txt = gr.Textbox(
242
  show_label=True,
243
- placeholder=f"用于定位滥用行为",
244
- label="用户名",
245
  value=user_name.value,
246
  lines=1,
247
  )
248
 
249
- with gr.Accordion("网络设置", open=False):
250
  # 优先展示自定义的api_host
251
  apihostTxt = gr.Textbox(
252
  show_label=True,
253
- placeholder=f"在这里输入API-Host...",
254
  label="API-Host",
255
  value=config.api_host or shared.API_HOST,
256
  lines=1,
257
  )
258
- changeAPIURLBtn = gr.Button("🔄 切换API地址")
259
  proxyTxt = gr.Textbox(
260
  show_label=True,
261
- placeholder=f"在这里输入代理地址...",
262
- label="代理地址(示例:http://127.0.0.1:10809)",
263
  value="",
264
  lines=2,
265
  )
266
- changeProxyBtn = gr.Button("🔄 设置代理地址")
267
- default_btn = gr.Button("🔙 恢复默认设置")
268
 
269
- gr.Markdown(CHUANHU_DESCRIPTION)
270
  gr.HTML(FOOTER.format(versions=versions_html()), elem_id="footer")
271
  chatgpt_predict_args = dict(
272
  fn=predict,
@@ -437,7 +438,7 @@ logging.info(
437
  + colorama.Style.RESET_ALL
438
  )
439
  # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
440
- demo.title = "川虎ChatGPT 🚀"
441
 
442
  if __name__ == "__main__":
443
  reload_javascript()
 
12
  from modules.overwrites import *
13
  from modules.models import get_model
14
 
15
+
16
  gr.Chatbot._postprocess_chat_messages = postprocess_chat_messages
17
  gr.Chatbot.postprocess = postprocess
18
  PromptHelper.compact_text_chunks = compact_text_chunks
 
30
  user_api_key = gr.State(my_api_key)
31
  current_model = gr.State(create_new_model)
32
 
33
+ topic = gr.State(i18n("未命名对话历史记录"))
34
 
35
  with gr.Row():
36
  gr.HTML(CHUANHU_TITLE, elem_id="app_title")
 
55
  with gr.Column(min_width=225, scale=12):
56
  user_input = gr.Textbox(
57
  elem_id="user_input_tb",
58
+ show_label=False, placeholder=i18n("在这里输入")
59
  ).style(container=False)
60
  with gr.Column(min_width=42, scale=1):
61
  submitBtn = gr.Button(value="", variant="primary", elem_id="submit_btn")
62
  cancelBtn = gr.Button(value="", variant="secondary", visible=False, elem_id="cancel_btn")
63
  with gr.Row():
64
  emptyBtn = gr.Button(
65
+ i18n("🧹 新的对话"),
66
  )
67
+ retryBtn = gr.Button(i18n("🔄 重新生成"))
68
+ delFirstBtn = gr.Button(i18n("🗑️ 删除最旧对话"))
69
+ delLastBtn = gr.Button(i18n("🗑️ 删除最新对话"))
70
 
71
  with gr.Column():
72
  with gr.Column(min_width=50, scale=1):
73
+ with gr.Tab(label=i18n("模型")):
74
  keyTxt = gr.Textbox(
75
  show_label=True,
76
  placeholder=f"OpenAI API-key...",
 
80
  label="API-Key",
81
  )
82
  if multi_api_key:
83
+ usageTxt = gr.Markdown(i18n("多账号模式已开启,无需输入key,可直接开始对话"), elem_id="usage_display", elem_classes="insert_block")
84
  else:
85
+ usageTxt = gr.Markdown(i18n("**发送消息** 或 **提交key** 以显示额度"), elem_id="usage_display", elem_classes="insert_block")
86
  model_select_dropdown = gr.Dropdown(
87
+ label=i18n("选择模型"), choices=MODELS, multiselect=False, value=MODELS[DEFAULT_MODEL], interactive=True
88
  )
89
  lora_select_dropdown = gr.Dropdown(
90
+ label=i18n("选择LoRA模型"), choices=[], multiselect=False, interactive=True, visible=False
91
  )
92
  with gr.Row():
93
  use_streaming_checkbox = gr.Checkbox(
94
+ label=i18n("实时传输回答"), value=True, visible=ENABLE_STREAMING_OPTION
95
  )
96
+ single_turn_checkbox = gr.Checkbox(label=i18n("单轮对话"), value=False)
97
+ use_websearch_checkbox = gr.Checkbox(label=i18n("使用在线搜索"), value=False)
98
  language_select_dropdown = gr.Dropdown(
99
+ label=i18n("选择回复语言(针对搜索&索引功能)"),
100
  choices=REPLY_LANGUAGES,
101
  multiselect=False,
102
  value=REPLY_LANGUAGES[0],
103
  )
104
+ index_files = gr.Files(label=i18n("上传索引文件"), type="file")
105
+ two_column = gr.Checkbox(label=i18n("双栏pdf"), value=advance_docs["pdf"].get("two_column", False))
106
  # TODO: 公式ocr
107
+ # formula_ocr = gr.Checkbox(label=i18n("识别公式"), value=advance_docs["pdf"].get("formula_ocr", False))
108
 
109
  with gr.Tab(label="Prompt"):
110
  systemPromptTxt = gr.Textbox(
111
  show_label=True,
112
+ placeholder=i18n("在这里输入System Prompt..."),
113
  label="System prompt",
114
  value=INITIAL_SYSTEM_PROMPT,
115
  lines=10,
116
  ).style(container=False)
117
+ with gr.Accordion(label=i18n("加载Prompt模板"), open=True):
118
  with gr.Column():
119
  with gr.Row():
120
  with gr.Column(scale=6):
121
  templateFileSelectDropdown = gr.Dropdown(
122
+ label=i18n("选择Prompt模板集合文件"),
123
  choices=get_template_names(plain=True),
124
  multiselect=False,
125
  value=get_template_names(plain=True)[0],
126
  ).style(container=False)
127
  with gr.Column(scale=1):
128
+ templateRefreshBtn = gr.Button(i18n("🔄 刷新"))
129
  with gr.Row():
130
  with gr.Column():
131
  templateSelectDropdown = gr.Dropdown(
132
+ label=i18n("从Prompt模板中加载"),
133
  choices=load_template(
134
  get_template_names(plain=True)[0], mode=1
135
  ),
136
  multiselect=False,
137
  ).style(container=False)
138
 
139
+ with gr.Tab(label=i18n("保存/加载")):
140
+ with gr.Accordion(label=i18n("保存/加载对话历史记录"), open=True):
141
  with gr.Column():
142
  with gr.Row():
143
  with gr.Column(scale=6):
144
  historyFileSelectDropdown = gr.Dropdown(
145
+ label=i18n("从列表中加载对话"),
146
  choices=get_history_names(plain=True),
147
  multiselect=False,
148
  value=get_history_names(plain=True)[0],
149
  )
150
  with gr.Column(scale=1):
151
+ historyRefreshBtn = gr.Button(i18n("🔄 刷新"))
152
  with gr.Row():
153
  with gr.Column(scale=6):
154
  saveFileName = gr.Textbox(
155
  show_label=True,
156
+ placeholder=i18n("设置文件名: 默认为.json,可选为.md"),
157
+ label=i18n("设置保存文件名"),
158
+ value=i18n("对话历史记录"),
159
  ).style(container=True)
160
  with gr.Column(scale=1):
161
+ saveHistoryBtn = gr.Button(i18n("💾 保存对话"))
162
+ exportMarkdownBtn = gr.Button(i18n("📝 导出为Markdown"))
163
+ gr.Markdown(i18n("默认保存于history文件夹"))
164
  with gr.Row():
165
  with gr.Column():
166
  downloadFile = gr.File(interactive=True)
167
 
168
+ with gr.Tab(label=i18n("高级")):
169
+ gr.Markdown(i18n("# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置"))
170
  gr.HTML(APPEARANCE_SWITCHER, elem_classes="insert_block")
171
+ with gr.Accordion(i18n("参数"), open=False):
172
  temperature_slider = gr.Slider(
173
  minimum=-0,
174
  maximum=2.0,
 
195
  )
196
  stop_sequence_txt = gr.Textbox(
197
  show_label=True,
198
+ placeholder=i18n("在这里输入停止符,用英文逗号隔开..."),
199
  label="stop",
200
  value="",
201
  lines=1,
 
241
  )
242
  user_identifier_txt = gr.Textbox(
243
  show_label=True,
244
+ placeholder=i18n("用于定位滥用行为"),
245
+ label=i18n("用户名"),
246
  value=user_name.value,
247
  lines=1,
248
  )
249
 
250
+ with gr.Accordion(i18n("网络设置"), open=False):
251
  # 优先展示自定义的api_host
252
  apihostTxt = gr.Textbox(
253
  show_label=True,
254
+ placeholder=i18n("在这里输入API-Host..."),
255
  label="API-Host",
256
  value=config.api_host or shared.API_HOST,
257
  lines=1,
258
  )
259
+ changeAPIURLBtn = gr.Button(i18n("🔄 切换API地址"))
260
  proxyTxt = gr.Textbox(
261
  show_label=True,
262
+ placeholder=i18n("在这里输入代理地址..."),
263
+ label=i18n("代理地址(示例:http://127.0.0.1:10809)"),
264
  value="",
265
  lines=2,
266
  )
267
+ changeProxyBtn = gr.Button(i18n("🔄 设置代理地址"))
268
+ default_btn = gr.Button(i18n("🔙 恢复默认设置"))
269
 
270
+ gr.Markdown(CHUANHU_DESCRIPTION, elem_id="description")
271
  gr.HTML(FOOTER.format(versions=versions_html()), elem_id="footer")
272
  chatgpt_predict_args = dict(
273
  fn=predict,
 
438
  + colorama.Style.RESET_ALL
439
  )
440
  # 默认开启本地服务器,默认可以直接从IP访问,默认不创建公开分享链接
441
+ demo.title = i18n("川虎Chat 🚀")
442
 
443
  if __name__ == "__main__":
444
  reload_javascript()
assets/custom.css CHANGED
@@ -4,9 +4,18 @@
4
  }
5
 
6
  #app_title {
 
 
 
 
7
  margin-top: 6px;
8
  white-space: nowrap;
9
  }
 
 
 
 
 
10
  /* 覆盖gradio的页脚信息QAQ */
11
  footer {
12
  display: none !important;
 
4
  }
5
 
6
  #app_title {
7
+ font-weight: var(--prose-header-text-weight);
8
+ font-size: var(--text-xxl);
9
+ line-height: 1.3;
10
+ text-align: left;
11
  margin-top: 6px;
12
  white-space: nowrap;
13
  }
14
+ #description {
15
+ text-align: center;
16
+ margin:16px 0
17
+ }
18
+
19
  /* 覆盖gradio的页脚信息QAQ */
20
  footer {
21
  display: none !important;
config_example.json CHANGED
@@ -2,6 +2,7 @@
2
  // 你的OpenAI API Key,一般必填,
3
  // 若缺省填为 "openai_api_key": "" 则必须再在图形界面中填入API Key
4
  "openai_api_key": "",
 
5
  // 如果使用代理,请取消注释下面的两行,并替换代理URL
6
  // "https_proxy": "http://127.0.0.1:1079",
7
  // "http_proxy": "http://127.0.0.1:1079",
 
2
  // 你的OpenAI API Key,一般必填,
3
  // 若缺省填为 "openai_api_key": "" 则必须再在图形界面中填入API Key
4
  "openai_api_key": "",
5
+ "language": "auto",
6
  // 如果使用代理,请取消注释下面的两行,并替换代理URL
7
  // "https_proxy": "http://127.0.0.1:1079",
8
  // "http_proxy": "http://127.0.0.1:1079",
locale/en_US.json ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "未命名对话历史记录": "Unnamed Dialog History",
3
+ "在这里输入": "Type in here",
4
+ "🧹 新的对话": "🧹 New Dialogue",
5
+ "🔄 重新生成": "🔄 Regeneration",
6
+ "🗑️ 删除最旧对话": "🗑️ Delete oldest dialog",
7
+ "🗑️ 删除最新对话": "🗑️ Delete latest dialog",
8
+ "模型": "Model",
9
+ "多账号模式已开启,无需输入key,可直接开始对话": "Multi-account mode is enabled, no need to enter key, you can start the dialogue directly",
10
+ "**发送消息** 或 **提交key** 以显示额度": "**Send message** or **Submit key** to display credit",
11
+ "选择模型": "Select Model",
12
+ "选择LoRA模型": "Select LoRA Model",
13
+ "实时传输回答": "Stream output",
14
+ "单轮对话": "Single-turn dialogue",
15
+ "使用在线搜索": "Use online search",
16
+ "选择回复语言(针对搜索&索引功能)": "Select reply language (for search & index)",
17
+ "上传索引文件": "Upload index file",
18
+ "双栏pdf": "Two-column pdf",
19
+ "识别公式": "formula OCR",
20
+ "在这里输入System Prompt...": "Type in System Prompt here...",
21
+ "加载Prompt模板": "Load Prompt Template",
22
+ "选择Prompt模板集合文件": "Select Prompt Template Collection File",
23
+ "🔄 刷新": "🔄 Refresh",
24
+ "从Prompt模板中加载": "Load from Prompt Template",
25
+ "保存/加载": "Save/Load",
26
+ "保存/加载对话历史记录": "Save/Load Dialog History",
27
+ "从列表中加载对话": "Load dialog from list",
28
+ "设置文件名: 默认为.json,可选为.md": "Set file name: default is .json, optional is .md",
29
+ "设置保存文件名": "Set save file name",
30
+ "对话历史记录": "Dialog History",
31
+ "💾 保存对话": "💾 Save Dialog",
32
+ "📝 导出为Markdown": "📝 Export as Markdown",
33
+ "默认保存于history文件夹": "Default save in history folder",
34
+ "高级": "Advanced",
35
+ "# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置": "# ⚠️ Caution: Changes require care. ⚠️\n\nIf unable to use, restore default settings.",
36
+ "参数": "Parameters",
37
+ "在这里输入停止符,用英文逗号隔开...": "Type in stop token here, separated by comma...",
38
+ "用于定位滥用行为": "Used to locate abuse",
39
+ "用户名": "Username",
40
+ "网络设置": "Network Settings",
41
+ "在这里输入API-Host...": "Type in API-Host here...",
42
+ "🔄 切换API地址": "🔄 Switch API Address",
43
+ "在这里输入代理地址...": "Type in proxy address here...",
44
+ "代理地址(示例:http://127.0.0.1:10809)": "Proxy address (example: http://127.0.0.1:10809)",
45
+ "🔄 设置代理地址": "🔄 Set Proxy Address",
46
+ "🔙 恢复默认设置": "🔙 Restore Default Settings",
47
+ "川虎Chat 🚀": "Chuanhu Chat 🚀",
48
+ "开始实时传输回答……": "Start streaming output...",
49
+ "Token 计数: ": "Token Count: ",
50
+ ",本次对话累计消耗了 ": ",Total cost for this dialogue is ",
51
+ "**获取API使用情况失败**": "**Failed to get API usage**",
52
+ "**本月使用金额** ": "**Monthly usage** ",
53
+ "获取API使用情况失败:": "Failed to get API usage:",
54
+ "API密钥更改为了": "The API key is changed to",
55
+ "JSON解析错误,收到的内容: ": "JSON parsing error, received content: ",
56
+ "模型设置为了:": "Model is set to: ",
57
+ "☹️发生了错误:": "☹️Error: ",
58
+ "获取对话时发生错误,请查看后台日志": "Error occurred when getting dialogue, check the background log",
59
+ "请检查网络连接,或者API-Key是否有效。": "Check the network connection or whether the API-Key is valid.",
60
+ "连接超时,无法获取对话。": "Connection timed out, unable to get dialogue.",
61
+ "读取超时,无法获取对话。": "Read timed out, unable to get dialogue.",
62
+ "代理错误,无法获取对话。": "Proxy error, unable to get dialogue.",
63
+ "SSL错误,无法获取对话。": "SSL error, unable to get dialogue.",
64
+ "API key为空,请检查是否输入正确。": "API key is empty, check whether it is entered correctly.",
65
+ "请输入对话内容。": "Enter the content of the conversation.",
66
+ "账单信息不适用": "Billing information is not applicable",
67
+ "由Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) 和 [明昭MZhao](https://space.bilibili.com/24807452)开发<br />访问川虎Chat的 [GitHub项目](https://github.com/GaiZhenbiao/ChuanhuChatGPT) 下载最新版脚本": "developor: Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) and [明昭MZhao](https://space.bilibili.com/24807452)\n\nDownload latest code from [GitHub](https://github.com/GaiZhenbiao/ChuanhuChatGPT)",
68
+ "切换亮暗色主题": "Switch light/dark theme",
69
+ "您的IP区域:未知。": "Your IP region: Unknown.",
70
+ "获取IP地理位置失败。原因:": "Failed to get IP location. Reason: ",
71
+ "。你仍然可以使用聊天功能。": ". You can still use the chat function.",
72
+ "您的IP区域:": "Your IP region: "
73
+ }
locale/extract_locale.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import re
4
+
5
+ # Define regular expression patterns
6
+ pattern = r'i18n\((\"{3}.*?\"{3}|\".*?\")\)'
7
+
8
+ # Load the .py file
9
+ with open('ChuanhuChatbot.py', 'r', encoding='utf-8') as f:
10
+ contents = f.read()
11
+
12
+ # Load the .py files in the modules folder
13
+ for filename in os.listdir("modules"):
14
+ if filename.endswith(".py"):
15
+ with open(os.path.join("modules", filename), "r", encoding="utf-8") as f:
16
+ contents += f.read()
17
+
18
+ # Matching with regular expressions
19
+ matches = re.findall(pattern, contents, re.DOTALL)
20
+
21
+ # Convert to key/value pairs
22
+ data = {match.strip('()"'): '' for match in matches}
23
+
24
+ # Save as a JSON file
25
+ with open('labels.json', 'w', encoding='utf-8') as f:
26
+ json.dump(data, f, ensure_ascii=False, indent=4)
locale/ja_JP.json ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "未命名对话历史记录": "名無しの会話履歴",
3
+ "在这里输入": "ここに入力",
4
+ "🧹 新的对话": "🧹 新しい会話",
5
+ "🔄 重新生成": "🔄 再生成",
6
+ "🗑️ 删除最旧对话": "🗑️ 最古の会話削除",
7
+ "🗑️ 删除最新对话": "🗑️ 最新の会話削除",
8
+ "模型": "LLMモデル",
9
+ "多账号模式已开启,无需输入key,可直接开始对话": "複数アカウントモードがオンになっています。キーを入力する必要はありません。会話を開始できます",
10
+ "**发送消息** 或 **提交key** 以显示额度": "**メッセージを送信** または **キーを送信** して、クレジットを表示します",
11
+ "选择模型": "LLMモデルを選択",
12
+ "选择LoRA模型": "LoRAモデルを選択",
13
+ "实时传输回答": "ストリーム出力",
14
+ "单轮对话": "単発会話",
15
+ "使用在线搜索": "オンライン検索を使用",
16
+ "选择回复语言(针对搜索&索引功能)": "回答言語を選択(検索とインデックス機能に対して)",
17
+ "上传索引文件": "インデックスファイルをアップロード",
18
+ "双栏pdf": "2カラムpdf",
19
+ "识别公式": "formula OCR",
20
+ "在这里输入System Prompt...": "System Promptを入力してください...",
21
+ "加载Prompt模板": "Promptテンプレートを読込",
22
+ "选择Prompt模板集合文件": "Promptテンプレートコレクションを選択",
23
+ "🔄 刷新": "🔄 更新",
24
+ "从Prompt模板中加载": "Promptテンプレートから読込",
25
+ "保存/加载": "保存/読込",
26
+ "保存/加载对话历史记录": "会話履歴を保存/読込",
27
+ "从列表中加载对话": "リストから会話を読込",
28
+ "设置文件名: 默认为.json,可选为.md": "ファイル名を設定: デフォルトは.json、.mdを選択できます",
29
+ "设置保存文件名": "保存ファイル名を設定",
30
+ "对话历史记录": "会話履歴",
31
+ "💾 保存对话": "💾 会話を保存",
32
+ "📝 导出为Markdown": "📝 Markdownでエクスポート",
33
+ "默认保存于history文件夹": "デフォルトでhistoryフォルダに保存されます",
34
+ "高级": "Advanced",
35
+ "# ⚠️ 务必谨慎更改 ⚠️\n\n如果无法使用请恢复默认设置": "# ⚠️ 変更には慎重に ⚠️\n\nもし動作しない場合は、デフォルト設定に戻してください。",
36
+ "参数": "パラメータ",
37
+ "在这里输入停止符,用英文逗号隔开...": "ここにストップ文字を英語のカンマで区切って入力してください...",
38
+ "用于定位滥用行为": "不正行為を特定するために使用されます",
39
+ "用户名": "ユーザー名",
40
+ "网络设置": "ネットワーク設定",
41
+ "在这里输入API-Host...": "API-Hostを入力してください...",
42
+ "🔄 切换API地址": "🔄 APIアドレスを切り替え",
43
+ "在这里输入代理地址...": "プロキシアドレスを入力してください...",
44
+ "代理地址(示例:http://127.0.0.1:10809)": "プロキシアドレス(例:http://127.0.0.1:10809)",
45
+ "🔄 设置代理地址": "🔄 プロキシアドレスを設定",
46
+ "🔙 恢复默认设置": "🔙 デフォルト設定に戻す",
47
+ "川虎Chat 🚀": "川虎Chat 🚀",
48
+ "开始实时传输回答……": "ストリーム出力開始……",
49
+ "Token 计数: ": "Token数: ",
50
+ ",本次对话累计消耗了 ": ", 今の会話で消費合計 ",
51
+ "**获取API使用情况失败**": "**API使用状況の取得に失敗しました**",
52
+ "**本月使用金额** ": "**今月の使用料金** ",
53
+ "获取API使用情况失败:": "API使用状況の取得に失敗しました:",
54
+ "API密钥更改为了": "APIキーが変更されました",
55
+ "JSON解析错误,收到的内容: ": "JSON解析エラー、受信内容: ",
56
+ "模型设置为了:": "LLMモデルを設定しました: ",
57
+ "☹️发生了错误:": "エラーが発生しました: ",
58
+ "获取对话时发生错误,请查看后台日志": "会話取得時にエラー発生、あとのログを確認してください",
59
+ "请检查网络连接,或者API-Key是否有效。": "ネットワーク接続を確認するか、APIキーが有効かどうかを確認してください。",
60
+ "连接超时,无法获取对话。": "接続タイムアウト、会話を取得できません。",
61
+ "读取超时,无法获取对话。": "読み込みタイムアウト、会話を取得できません。",
62
+ "代理错误,无法获取对话。": "プロキシエラー、会話を取得できません。",
63
+ "SSL错误,无法获取对话。": "SSLエラー、会話を取得できません。",
64
+ "API key为空,请检查是否输入正确。": "APIキーが入力されていません。正しく入力されているか確認してください。",
65
+ "请输入对话内容。": "会話内容を入力してください。",
66
+ "账单信息不适用": "課金情報は対象外です",
67
+ "由Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) 和 [明昭MZhao](https://space.bilibili.com/24807452)开发<br />访问川虎Chat的 [GitHub项目](https://github.com/GaiZhenbiao/ChuanhuChatGPT) 下载最新版脚本": "開発:Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) と [明昭MZhao](https://space.bilibili.com/24807452)\n\n最新コードは川虎Chatのサイトへ [GitHubプロジェクト](https://github.com/GaiZhenbiao/ChuanhuChatGPT)",
68
+ "切换亮暗色主题": "テーマの明暗切替",
69
+ "您的IP区域:未知。": "あなたのIPアドレス地域:不明",
70
+ "获取IP地理位置失败。原因:": "IPアドレス地域の取得に失敗しました。理由:",
71
+ "。你仍然可以使用聊天功能。": "。あなたはまだチャット機能を使用できます。",
72
+ "您的IP区域:": "あなたのIPアドレス地域:"
73
+ }
modules/base_model.py CHANGED
@@ -124,7 +124,7 @@ class BaseLLMModel:
124
  def get_return_value():
125
  return chatbot, status_text
126
 
127
- status_text = "开始实时传输回答……"
128
  if fake_input:
129
  chatbot.append((fake_input, ""))
130
  else:
@@ -505,7 +505,7 @@ class BaseLLMModel:
505
  token_sum = 0
506
  for i in range(len(token_lst)):
507
  token_sum += sum(token_lst[: i + 1])
508
- return f"Token 计数: {sum(token_lst)},本次对话累计消耗了 {token_sum} tokens"
509
 
510
  def save_chat_history(self, filename, chatbot, user_name):
511
  if filename == "":
 
124
  def get_return_value():
125
  return chatbot, status_text
126
 
127
+ status_text = i18n("开始实时传输回答……")
128
  if fake_input:
129
  chatbot.append((fake_input, ""))
130
  else:
 
505
  token_sum = 0
506
  for i in range(len(token_lst)):
507
  token_sum += sum(token_lst[: i + 1])
508
+ return i18n("Token 计数: ") + f"{sum(token_lst)}" + i18n(",本次对话累计消耗了 ") + f"{token_sum} tokens"
509
 
510
  def save_chat_history(self, filename, chatbot, user_name):
511
  if filename == "":
modules/config.py CHANGED
@@ -31,6 +31,8 @@ if os.path.exists("config.json"):
31
  config = json.load(f)
32
  else:
33
  config = {}
 
 
34
 
35
  if os.path.exists("api_key.txt"):
36
  logging.info("检测到api_key.txt文件,正在进行迁移...")
 
31
  config = json.load(f)
32
  else:
33
  config = {}
34
+
35
+ language = config.get("language", "auto")
36
 
37
  if os.path.exists("api_key.txt"):
38
  logging.info("检测到api_key.txt文件,正在进行迁移...")
modules/models.py CHANGED
@@ -84,9 +84,9 @@ class OpenAIClient(BaseLLMModel):
84
  usage_data = self._get_billing_data(usage_url)
85
  except Exception as e:
86
  logging.error(f"获取API使用情况失败:" + str(e))
87
- return f"**获取API使用情况失败**"
88
  rounded_usage = "{:.5f}".format(usage_data["total_usage"] / 100)
89
- return f"**本月使用金额** \u3000 ${rounded_usage}"
90
  except requests.exceptions.ConnectTimeout:
91
  status_text = (
92
  STANDARD_ERROR_MSG + CONNECTION_TIMEOUT_MSG + ERROR_RETRIEVE_MSG
@@ -96,7 +96,7 @@ class OpenAIClient(BaseLLMModel):
96
  status_text = STANDARD_ERROR_MSG + READ_TIMEOUT_MSG + ERROR_RETRIEVE_MSG
97
  return status_text
98
  except Exception as e:
99
- logging.error(f"获取API使用情况失败:" + str(e))
100
  return STANDARD_ERROR_MSG + ERROR_RETRIEVE_MSG
101
 
102
  def set_token_upper_limit(self, new_upper_limit):
@@ -105,7 +105,7 @@ class OpenAIClient(BaseLLMModel):
105
  def set_key(self, new_access_key):
106
  self.api_key = new_access_key.strip()
107
  self._refresh_header()
108
- msg = f"API密钥更改为了{hide_middle_chars(self.api_key)}"
109
  logging.info(msg)
110
  return msg
111
 
@@ -197,7 +197,7 @@ class OpenAIClient(BaseLLMModel):
197
  try:
198
  chunk = json.loads(chunk[6:])
199
  except json.JSONDecodeError:
200
- print(f"JSON解析错误,收到的内容: {chunk}")
201
  error_msg += chunk
202
  continue
203
  if chunk_length > 6 and "delta" in chunk["choices"][0]:
@@ -497,7 +497,7 @@ def get_model(
497
  top_p=None,
498
  system_prompt=None,
499
  ) -> BaseLLMModel:
500
- msg = f"模型设置为了: {model_name}"
501
  model_type = ModelType.get_type(model_name)
502
  lora_selector_visibility = False
503
  lora_choices = []
 
84
  usage_data = self._get_billing_data(usage_url)
85
  except Exception as e:
86
  logging.error(f"获取API使用情况失败:" + str(e))
87
+ return i18n("**获取API使用情况失败**")
88
  rounded_usage = "{:.5f}".format(usage_data["total_usage"] / 100)
89
+ return i18n("**本月使用金额** ") + f"\u3000 ${rounded_usage}"
90
  except requests.exceptions.ConnectTimeout:
91
  status_text = (
92
  STANDARD_ERROR_MSG + CONNECTION_TIMEOUT_MSG + ERROR_RETRIEVE_MSG
 
96
  status_text = STANDARD_ERROR_MSG + READ_TIMEOUT_MSG + ERROR_RETRIEVE_MSG
97
  return status_text
98
  except Exception as e:
99
+ logging.error(i18n("获取API使用情况失败:") + str(e))
100
  return STANDARD_ERROR_MSG + ERROR_RETRIEVE_MSG
101
 
102
  def set_token_upper_limit(self, new_upper_limit):
 
105
  def set_key(self, new_access_key):
106
  self.api_key = new_access_key.strip()
107
  self._refresh_header()
108
+ msg = i18n("API密钥更改为了") + f"{hide_middle_chars(self.api_key)}"
109
  logging.info(msg)
110
  return msg
111
 
 
197
  try:
198
  chunk = json.loads(chunk[6:])
199
  except json.JSONDecodeError:
200
+ print(i18n("JSON解析错误,收到的内容: ") + f"{chunk}")
201
  error_msg += chunk
202
  continue
203
  if chunk_length > 6 and "delta" in chunk["choices"][0]:
 
497
  top_p=None,
498
  system_prompt=None,
499
  ) -> BaseLLMModel:
500
+ msg = i18n("模型设置为了:") + f" {model_name}"
501
  model_type = ModelType.get_type(model_name)
502
  lora_selector_visibility = False
503
  lora_choices = []
modules/presets.py CHANGED
@@ -1,8 +1,10 @@
1
  # -*- coding:utf-8 -*-
2
  import os
3
  from pathlib import Path
4
-
5
  import gradio as gr
 
 
 
6
 
7
  CHATGLM_MODEL = None
8
  CHATGLM_TOKENIZER = None
@@ -20,16 +22,16 @@ HISTORY_DIR = "history"
20
  TEMPLATES_DIR = "templates"
21
 
22
  # 错误信息
23
- STANDARD_ERROR_MSG = "☹️发生了错误:" # 错误信息的标准前缀
24
- GENERAL_ERROR_MSG = "获取对话时发生错误,请查看后台日志"
25
- ERROR_RETRIEVE_MSG = "请检查网络连接,或者API-Key是否有效。"
26
- CONNECTION_TIMEOUT_MSG = "连接超时,无法获取对话。" # 连接超时
27
- READ_TIMEOUT_MSG = "读取超时,无法获取对话。" # 读取超时
28
- PROXY_ERROR_MSG = "代理错误,无法获取对话。" # 代理错误
29
- SSL_ERROR_PROMPT = "SSL错误,无法获取对话。" # SSL 错误
30
- NO_APIKEY_MSG = "API key为空,请检查是否输入正确。" # API key 长度不足 51 位
31
- NO_INPUT_MSG = "请输入对话内容。" # 未输入对话内容
32
- BILLING_NOT_APPLICABLE_MSG = "账单信息不适用" # 本地运行的模型返回的账单信息
33
 
34
  TIMEOUT_STREAMING = 60 # 流式对话时的超时时间
35
  TIMEOUT_ALL = 200 # 非流式对话时的超时时间
@@ -40,22 +42,15 @@ CONCURRENT_COUNT = 100 # 允许同时使用的用户数量
40
  SIM_K = 5
41
  INDEX_QUERY_TEMPRATURE = 1.0
42
 
43
- CHUANHU_TITLE = """<h1 align="left">川虎Chat 🚀</h1>"""
44
- CHUANHU_DESCRIPTION = """\
45
- <div align="center" style="margin:16px 0">
46
-
47
- 由Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) 和 [明昭MZhao](https://space.bilibili.com/24807452)开发
48
 
49
- 访问川虎Chat的 [GitHub项目](https://github.com/GaiZhenbiao/ChuanhuChatGPT) 下载最新版脚本
50
-
51
- </div>
52
- """
53
 
54
  FOOTER = """<div class="versions">{versions}</div>"""
55
 
56
  APPEARANCE_SWITCHER = """
57
  <div style="display: flex; justify-content: space-between;">
58
- <span style="margin-top: 4px !important;">切换亮暗色主题</span>
59
  <span><label class="apSwitch" for="checkbox">
60
  <input type="checkbox" id="checkbox">
61
  <div class="apSlider"></div>
 
1
  # -*- coding:utf-8 -*-
2
  import os
3
  from pathlib import Path
 
4
  import gradio as gr
5
+ from .webui_locale import I18nAuto
6
+
7
+ i18n = I18nAuto() # internationalization
8
 
9
  CHATGLM_MODEL = None
10
  CHATGLM_TOKENIZER = None
 
22
  TEMPLATES_DIR = "templates"
23
 
24
  # 错误信息
25
+ STANDARD_ERROR_MSG = i18n("☹️发生了错误:") # 错误信息的标准前缀
26
+ GENERAL_ERROR_MSG = i18n("获取对话时发生错误,请查看后台日志")
27
+ ERROR_RETRIEVE_MSG = i18n("请检查网络连接,或者API-Key是否有效。")
28
+ CONNECTION_TIMEOUT_MSG = i18n("连接超时,无法获取对话。") # 连接超时
29
+ READ_TIMEOUT_MSG = i18n("读取超时,无法获取对话。") # 读取超时
30
+ PROXY_ERROR_MSG = i18n("代理错误,无法获取对话。") # 代理错误
31
+ SSL_ERROR_PROMPT = i18n("SSL错误,无法获取对话。") # SSL 错误
32
+ NO_APIKEY_MSG = i18n("API key为空,请检查是否输入正确。") # API key 长度不足 51 位
33
+ NO_INPUT_MSG = i18n("请输入对话内容。") # 未输入对话内容
34
+ BILLING_NOT_APPLICABLE_MSG = i18n("账单信息不适用") # 本地运行的模型返回的账单信息
35
 
36
  TIMEOUT_STREAMING = 60 # 流式对话时的超时时间
37
  TIMEOUT_ALL = 200 # 非流式对话时的超时时间
 
42
  SIM_K = 5
43
  INDEX_QUERY_TEMPRATURE = 1.0
44
 
45
+ CHUANHU_TITLE = i18n("川虎Chat 🚀")
 
 
 
 
46
 
47
+ CHUANHU_DESCRIPTION = i18n("由Bilibili [土川虎虎虎](https://space.bilibili.com/29125536) 和 [明昭MZhao](https://space.bilibili.com/24807452)开发<br />访问川虎Chat的 [GitHub项目](https://github.com/GaiZhenbiao/ChuanhuChatGPT) 下载最新版脚本")
 
 
 
48
 
49
  FOOTER = """<div class="versions">{versions}</div>"""
50
 
51
  APPEARANCE_SWITCHER = """
52
  <div style="display: flex; justify-content: space-between;">
53
+ <span style="margin-top: 4px !important;">"""+ i18n("切换亮暗色主题") + """</span>
54
  <span><label class="apSwitch" for="checkbox">
55
  <input type="checkbox" id="checkbox">
56
  <div class="apSlider"></div>
modules/utils.py CHANGED
@@ -379,16 +379,16 @@ def get_geoip():
379
  logging.warning(f"无法获取IP地址信息。\n{data}")
380
  if data["reason"] == "RateLimited":
381
  return (
382
- f"您的IP区域:未知。"
383
  )
384
  else:
385
- return f"获取IP地理位置失败。原因:{data['reason']}。你仍然可以使用聊天功能。"
386
  else:
387
  country = data["country_name"]
388
  if country == "China":
389
  text = "**您的IP区域:中国。请立即检查代理设置,在不受支持的地区使用API可能导致账号被封禁。**"
390
  else:
391
- text = f"您的IP区域:{country}。"
392
  logging.info(text)
393
  return text
394
 
 
379
  logging.warning(f"无法获取IP地址信息。\n{data}")
380
  if data["reason"] == "RateLimited":
381
  return (
382
+ i18n("您的IP区域:未知。")
383
  )
384
  else:
385
+ return i18n("获取IP地理位置失败。原因:") + f"{data['reason']}" + i18n("。你仍然可以使用聊天功能。")
386
  else:
387
  country = data["country_name"]
388
  if country == "China":
389
  text = "**您的IP区域:中国。请立即检查代理设置,在不受支持的地区使用API可能导致账号被封禁。**"
390
  else:
391
+ text = i18n("您的IP区域:") + f"{country}。"
392
  logging.info(text)
393
  return text
394
 
modules/webui_locale.py ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import locale
3
+ import commentjson as json
4
+
5
+ class I18nAuto:
6
+ def __init__(self):
7
+ if os.path.exists("config.json"):
8
+ with open("config.json", "r", encoding='utf-8') as f:
9
+ config = json.load(f)
10
+ else:
11
+ config = {}
12
+ language = config.get("language", "auto")
13
+ if language == "auto":
14
+ language = locale.getdefaultlocale()[0] # get the language code of the system (ex. zh_CN)
15
+ self.language_map = {}
16
+ self.file_is_exists = os.path.isfile(f"./locale/{language}.json")
17
+ if self.file_is_exists:
18
+ with open(f"./locale/{language}.json", "r", encoding="utf-8") as f:
19
+ self.language_map.update(json.load(f))
20
+
21
+ def __call__(self, key):
22
+ if self.file_is_exists and key in self.language_map:
23
+ return self.language_map[key]
24
+ else:
25
+ return key