22
33import ollama
44import pygame
5+ import tomlkit
56from gtts import gTTS
67from platformdirs import user_data_dir
78from prompt_toolkit import PromptSession
89from prompt_toolkit .auto_suggest import AutoSuggestFromHistory
910from prompt_toolkit .history import FileHistory
11+ from prompt_toolkit .shortcuts import radiolist_dialog
1012from rich .console import Console
1113from rich .prompt import Prompt
1214from rich .text import Text
15+ from tomlkit import document , parse
1316
1417from .utils import random_mp3_fname
1518
@@ -39,6 +42,73 @@ def tts(text: str, lang="zh-tw", slow=False, file_name: str | None = None):
3942 os .remove (file_path )
4043
4144
45+ def set_lang (conf_file : str , lang : str ):
46+ with open (conf_file , "r" ) as f :
47+ doc = parse (f .read ())
48+
49+ doc ["voice_language" ] = lang
50+
51+ with open (conf_file , "w" ) as f :
52+ f .write (tomlkit .dumps (doc ))
53+
54+
55+ def get_lang (conf_file : str ):
56+ with open (conf_file , "r" ) as f :
57+ doc = parse (f .read ())
58+
59+ return doc ["voice_language" ]
60+
61+
62+ def select_language (conf_file : str ):
63+ # 定義語言選項
64+ languages = [
65+ ("中文 (Chinese)" , "Chinese" ),
66+ ("英文 (English)" , "English" ),
67+ ("日文 (Japanese)" , "Japanese" ),
68+ ("韓文 (Korean)" , "Korean" ),
69+ ("法文 (French)" , "French" ),
70+ ("西班牙文 (Spanish)" , "Spanish" ),
71+ ("其他 (Other)" , "Other" ),
72+ ]
73+
74+ # 使用 radiolist_dialog 顯示選單
75+ result = radiolist_dialog (
76+ title = "語言選單" ,
77+ text = "Please select a language for /voice command:" ,
78+ values = languages ,
79+ ).run ()
80+
81+ # 如果選擇「其他」,讓使用者輸入自訂語言名稱
82+ if result == "其他 (Other)" :
83+ session = PromptSession ()
84+ custom_language = session .prompt ("請輸入自訂語言名稱:" )
85+ print (f"You selected: { custom_language } " )
86+ set_lang (conf_file = conf_file , lang = custom_language )
87+
88+ else :
89+ print (f"You selected: { result } " )
90+ if result == "中文 (Chinese)" :
91+ set_lang (conf_file = conf_file , lang = "zh-tw" )
92+
93+ elif result == "英文 (English)" :
94+ set_lang (conf_file = conf_file , lang = "en" )
95+
96+ elif result == "日文 (Japanese)" :
97+ set_lang (conf_file = conf_file , lang = "ja" )
98+
99+ elif result == "韓文 (Korean)" :
100+ set_lang (conf_file = conf_file , lang = "ko" )
101+
102+ elif result == "法文 (French)" :
103+ set_lang (conf_file = conf_file , lang = "fr" )
104+
105+ elif result == "西班牙文 (Spanish)" :
106+ set_lang (conf_file = conf_file , lang = "es" )
107+
108+ else :
109+ raise ValueError (f"Invalid language selection: { result } " )
110+
111+
42112def chat_terminal ():
43113 """
44114 Runs a chat terminal where the user can interact with a simulated chatbot.
@@ -60,13 +130,23 @@ def chat_terminal():
60130 history = []
61131 bb7_dir = user_data_dir (appname = "bb7" , appauthor = "drunkwcodes" )
62132 history_file = bb7_dir + "/chat_history.txt"
133+
134+ conf_file = bb7_dir + "/bb7_config.toml"
135+
136+ if os .path .exists (conf_file ) is False :
137+ doc = document ()
138+ doc .add ("voice_language" , "ja" )
139+ with open (conf_file , "w" ) as f :
140+ tomlkit .dump (doc , f )
141+
63142 os .makedirs (bb7_dir , exist_ok = True )
64143 session = PromptSession (history = FileHistory (history_file ))
65144
66145 while True :
67146 try :
68147 # 使用 Prompt 讓用戶輸入訊息
69148 # user_input = Prompt.ask("[bold blue]>>[/bold blue]")
149+ lang = get_lang (conf_file )
70150 user_input = session .prompt (">> " , auto_suggest = AutoSuggestFromHistory ())
71151
72152 # 檢查是否是退出命令
@@ -84,10 +164,12 @@ def chat_terminal():
84164 inputs = user_input .split (" " )
85165 if len (inputs ) > 1 :
86166 lang = inputs [1 ]
87- else :
88- lang = "ja"
89167 tts (bot_reply , lang = lang )
90168 continue
169+
170+ elif "/select" in user_input .lower () or "/s" in user_input .lower ():
171+ select_language (conf_file = conf_file )
172+ continue
91173 else :
92174 console .print ("[bold red]Invalid command[/bold red]" )
93175 continue
0 commit comments