Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import spaces
|
| 3 |
import sys
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from llama_cpp import Llama
|
|
|
|
|
|
|
| 6 |
import configparser
|
| 7 |
from functools import partial
|
| 8 |
-
from utils.dl_utils import dl_guff_model
|
| 9 |
import threading
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# 定数
|
| 12 |
DEFAULT_INI_FILE = 'settings.ini'
|
|
@@ -16,18 +21,6 @@ MODEL_FILE_EXTENSION = '.gguf'
|
|
| 16 |
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
|
| 17 |
MODEL_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models")
|
| 18 |
|
| 19 |
-
# モデルディレクトリが存在しない場合は作成
|
| 20 |
-
if not os.path.exists("models"):
|
| 21 |
-
os.makedirs("models")
|
| 22 |
-
|
| 23 |
-
# 使用するモデルのファイル名を指定
|
| 24 |
-
model_filename = "EZO-Common-9B-gemma-2-it.f16.gguf"
|
| 25 |
-
model_path = os.path.join("models", model_filename)
|
| 26 |
-
|
| 27 |
-
# モデルファイルが存在しない場合はダウンロード
|
| 28 |
-
if not os.path.exists(model_path):
|
| 29 |
-
dl_guff_model("models", f"https://huggingface.co/MCZK/EZO-Common-9B-gemma-2-it-GGUF/resolve/main/{model_filename}")
|
| 30 |
-
|
| 31 |
class ConfigManager:
|
| 32 |
@staticmethod
|
| 33 |
def load_settings(filename):
|
|
@@ -47,41 +40,68 @@ class ConfigManager:
|
|
| 47 |
ConfigManager.save_settings(config, filename)
|
| 48 |
return f"設定を更新しました: [{section}] {key} = {value}"
|
| 49 |
|
| 50 |
-
@staticmethod
|
| 51 |
-
def create_default_settings(filename):
|
| 52 |
-
config = configparser.ConfigParser()
|
| 53 |
-
config['Character'] = {
|
| 54 |
-
'gen_author_description': 'あなたは新進気鋭の和風伝奇ミステリー小説家で、細やかな筆致と巧みな構成で若い世代にとても人気があります。'
|
| 55 |
-
}
|
| 56 |
-
config['Models'] = {
|
| 57 |
-
'DEFAULT_GEN_MODEL': 'EZO-Common-9B-gemma-2-it.f16.gguf'
|
| 58 |
-
}
|
| 59 |
-
config['GenerateParameters'] = {
|
| 60 |
-
'n_gpu_layers': '-1',
|
| 61 |
-
'temperature': '0.35',
|
| 62 |
-
'top_p': '0.9',
|
| 63 |
-
'top_k': '40',
|
| 64 |
-
'repetition_penalty': '1.2',
|
| 65 |
-
'n_ctx': '10000'
|
| 66 |
-
}
|
| 67 |
-
ConfigManager.save_settings(config, filename)
|
| 68 |
-
print(f"デフォルト設定ファイル {filename} を作成しました。")
|
| 69 |
-
|
| 70 |
class ModelManager:
|
| 71 |
@staticmethod
|
| 72 |
def get_model_files():
|
| 73 |
return [f for f in os.listdir(MODEL_DIR) if f.endswith(MODEL_FILE_EXTENSION)]
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
class Settings:
|
| 76 |
@staticmethod
|
| 77 |
def _parse_config(config):
|
| 78 |
settings = {}
|
| 79 |
if 'Character' in config:
|
|
|
|
|
|
|
|
|
|
| 80 |
settings['gen_author_description'] = config['Character'].get('gen_author_description', '')
|
| 81 |
if 'Models' in config:
|
|
|
|
| 82 |
settings['DEFAULT_GEN_MODEL'] = config['Models'].get('DEFAULT_GEN_MODEL', '')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
if 'GenerateParameters' in config:
|
| 84 |
-
settings['gen_n_gpu_layers'] = int(config['GenerateParameters'].get('n_gpu_layers', '
|
| 85 |
settings['gen_temperature'] = float(config['GenerateParameters'].get('temperature', '0.35'))
|
| 86 |
settings['gen_top_p'] = float(config['GenerateParameters'].get('top_p', '0.9'))
|
| 87 |
settings['gen_top_k'] = int(config['GenerateParameters'].get('top_k', '40'))
|
|
@@ -89,109 +109,504 @@ class Settings:
|
|
| 89 |
settings['gen_n_ctx'] = int(config['GenerateParameters'].get('n_ctx', '10000'))
|
| 90 |
return settings
|
| 91 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
@staticmethod
|
| 93 |
def load_from_ini(filename):
|
| 94 |
config = ConfigManager.load_settings(filename)
|
| 95 |
return Settings._parse_config(config)
|
| 96 |
|
| 97 |
-
class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
def __init__(self):
|
| 99 |
-
self.
|
|
|
|
|
|
|
| 100 |
self.settings = None
|
|
|
|
| 101 |
self.current_model = None
|
| 102 |
-
self.
|
| 103 |
self.use_chat_format = False
|
| 104 |
-
self.model_loaded = threading.Event()
|
| 105 |
|
| 106 |
@spaces.GPU(duration=120)
|
| 107 |
-
def load_model(self):
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
self.llm = Llama(model_path=model_path, n_ctx=self.settings['gen_n_ctx'], n_gpu_layers=n_gpu_layers)
|
| 117 |
-
self.current_model = 'GEN'
|
| 118 |
-
print(f"GEN モデル {model_path} のロードが完了しました。(n_gpu_layers: {n_gpu_layers})")
|
| 119 |
-
self.model_loaded.set()
|
| 120 |
-
except Exception as e:
|
| 121 |
-
print(f"GEN モデルのロード中にエラーが発生しました: {str(e)}")
|
| 122 |
-
self.model_loaded.set()
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
| 133 |
|
| 134 |
-
def generate_response(self, input_str
|
| 135 |
-
|
|
|
|
| 136 |
return "モデルのロードに失敗しました。設定を確認してください。"
|
| 137 |
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
prompt = self._generate_prompt(input_str)
|
| 143 |
-
|
| 144 |
-
res_text = res["choices"][0]["text"]
|
| 145 |
self.history.append({"user": input_str, "assistant": res_text})
|
| 146 |
-
return res_text
|
| 147 |
-
except Exception as e:
|
| 148 |
-
print(f"既存の形式でのレスポンス生成に失敗しました: {str(e)}")
|
| 149 |
-
print("チャット形式に切り替えます。")
|
| 150 |
-
self.use_chat_format = True
|
| 151 |
-
|
| 152 |
-
if self.use_chat_format:
|
| 153 |
-
chat_messages = [
|
| 154 |
-
{"role": "system", "content": self.settings.get('gen_author_description', '')},
|
| 155 |
-
{"role": "user", "content": input_str}
|
| 156 |
-
]
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
max_tokens=max_tokens,
|
| 162 |
-
temperature=self.
|
| 163 |
-
top_p=self.
|
| 164 |
-
top_k=self.
|
| 165 |
-
repeat_penalty=self.
|
| 166 |
)
|
| 167 |
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
-
def
|
|
|
|
|
|
|
| 179 |
self.settings = Settings.load_from_ini(filename)
|
| 180 |
|
| 181 |
def reset(self):
|
| 182 |
self.history = []
|
|
|
|
| 183 |
self.use_chat_format = False
|
| 184 |
|
| 185 |
# グローバル変数
|
| 186 |
-
|
|
|
|
| 187 |
model_files = ModelManager.get_model_files()
|
| 188 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
# Gradioインターフェース
|
| 190 |
def build_gradio_interface():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
with gr.Blocks() as iface:
|
| 192 |
gr.HTML("""
|
| 193 |
<style>
|
| 194 |
-
#
|
| 195 |
resize: both;
|
| 196 |
overflow: auto;
|
| 197 |
min-height: 100px;
|
|
@@ -199,124 +614,240 @@ def build_gradio_interface():
|
|
| 199 |
}
|
| 200 |
</style>
|
| 201 |
""")
|
| 202 |
-
|
| 203 |
-
with
|
| 204 |
-
with gr.
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
with gr.
|
| 219 |
-
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
inputs=[gen_input_text, gen_characters, gen_token_multiplier, gen_instruction],
|
| 228 |
-
outputs=[generated_output]
|
| 229 |
-
)
|
| 230 |
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
)
|
| 246 |
|
| 247 |
-
|
| 248 |
-
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
|
|
|
|
|
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
value=
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
outputs=[output]
|
| 272 |
)
|
| 273 |
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
lines=5
|
| 279 |
)
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
)
|
| 285 |
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
input_component.change(
|
| 301 |
-
|
| 302 |
inputs=[input_component],
|
| 303 |
outputs=[output]
|
| 304 |
)
|
| 305 |
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 312 |
return iface
|
| 313 |
|
| 314 |
-
|
| 315 |
if not os.path.exists(DEFAULT_INI_FILE):
|
| 316 |
print(f"{DEFAULT_INI_FILE} が見つかりません。デフォルト設定で作成します。")
|
| 317 |
-
|
| 318 |
|
| 319 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
demo = build_gradio_interface()
|
| 322 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import spaces
|
| 3 |
import sys
|
| 4 |
+
import time
|
| 5 |
+
import socket
|
| 6 |
import gradio as gr
|
| 7 |
from llama_cpp import Llama
|
| 8 |
+
import datetime
|
| 9 |
+
from jinja2 import Template
|
| 10 |
import configparser
|
| 11 |
from functools import partial
|
|
|
|
| 12 |
import threading
|
| 13 |
+
import asyncio
|
| 14 |
+
import csv
|
| 15 |
|
| 16 |
# 定数
|
| 17 |
DEFAULT_INI_FILE = 'settings.ini'
|
|
|
|
| 21 |
BASE_PATH = os.path.dirname(os.path.abspath(__file__))
|
| 22 |
MODEL_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "models")
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
class ConfigManager:
|
| 25 |
@staticmethod
|
| 26 |
def load_settings(filename):
|
|
|
|
| 40 |
ConfigManager.save_settings(config, filename)
|
| 41 |
return f"設定を更新しました: [{section}] {key} = {value}"
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
class ModelManager:
|
| 44 |
@staticmethod
|
| 45 |
def get_model_files():
|
| 46 |
return [f for f in os.listdir(MODEL_DIR) if f.endswith(MODEL_FILE_EXTENSION)]
|
| 47 |
|
| 48 |
+
@staticmethod
|
| 49 |
+
def update_model_dropdown(config, section, key):
|
| 50 |
+
current_value = config[section][key]
|
| 51 |
+
model_files = ModelManager.get_model_files()
|
| 52 |
+
|
| 53 |
+
if current_value not in model_files:
|
| 54 |
+
download_message = f"現在の{key}({current_value})が見つかりません。ダウンロードしてください。"
|
| 55 |
+
model_files.insert(0, current_value)
|
| 56 |
+
else:
|
| 57 |
+
download_message = ""
|
| 58 |
+
|
| 59 |
+
return model_files, current_value, download_message
|
| 60 |
+
|
| 61 |
+
class NetworkUtils:
|
| 62 |
+
@staticmethod
|
| 63 |
+
def get_ip_address():
|
| 64 |
+
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
|
| 65 |
+
try:
|
| 66 |
+
s.connect(('10.255.255.255', 1))
|
| 67 |
+
return s.getsockname()[0]
|
| 68 |
+
except Exception:
|
| 69 |
+
return '127.0.0.1'
|
| 70 |
+
|
| 71 |
+
@staticmethod
|
| 72 |
+
def find_available_port(starting_port):
|
| 73 |
+
port = starting_port
|
| 74 |
+
while NetworkUtils.is_port_in_use(port):
|
| 75 |
+
print(f"Port {port} is in use, trying next one.")
|
| 76 |
+
port += 1
|
| 77 |
+
return port
|
| 78 |
+
|
| 79 |
+
@staticmethod
|
| 80 |
+
def is_port_in_use(port):
|
| 81 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 82 |
+
return s.connect_ex(('localhost', port)) == 0
|
| 83 |
+
|
| 84 |
class Settings:
|
| 85 |
@staticmethod
|
| 86 |
def _parse_config(config):
|
| 87 |
settings = {}
|
| 88 |
if 'Character' in config:
|
| 89 |
+
settings['chat_author_description'] = config['Character'].get('chat_author_description', '')
|
| 90 |
+
settings['chat_instructions'] = config['Character'].get('chat_instructions', '')
|
| 91 |
+
settings['example_qa'] = config['Character'].get('example_qa', '').split('\n')
|
| 92 |
settings['gen_author_description'] = config['Character'].get('gen_author_description', '')
|
| 93 |
if 'Models' in config:
|
| 94 |
+
settings['DEFAULT_CHAT_MODEL'] = config['Models'].get('DEFAULT_CHAT_MODEL', '')
|
| 95 |
settings['DEFAULT_GEN_MODEL'] = config['Models'].get('DEFAULT_GEN_MODEL', '')
|
| 96 |
+
if 'ChatParameters' in config:
|
| 97 |
+
settings['chat_n_gpu_layers'] = int(config['ChatParameters'].get('n_gpu_layers', '0'))
|
| 98 |
+
settings['chat_temperature'] = float(config['ChatParameters'].get('temperature', '0.5'))
|
| 99 |
+
settings['chat_top_p'] = float(config['ChatParameters'].get('top_p', '0.7'))
|
| 100 |
+
settings['chat_top_k'] = int(config['ChatParameters'].get('top_k', '80'))
|
| 101 |
+
settings['chat_rep_pen'] = float(config['ChatParameters'].get('repetition_penalty', '1.2'))
|
| 102 |
+
settings['chat_n_ctx'] = int(config['ChatParameters'].get('n_ctx', '10000'))
|
| 103 |
if 'GenerateParameters' in config:
|
| 104 |
+
settings['gen_n_gpu_layers'] = int(config['GenerateParameters'].get('n_gpu_layers', '0'))
|
| 105 |
settings['gen_temperature'] = float(config['GenerateParameters'].get('temperature', '0.35'))
|
| 106 |
settings['gen_top_p'] = float(config['GenerateParameters'].get('top_p', '0.9'))
|
| 107 |
settings['gen_top_k'] = int(config['GenerateParameters'].get('top_k', '40'))
|
|
|
|
| 109 |
settings['gen_n_ctx'] = int(config['GenerateParameters'].get('n_ctx', '10000'))
|
| 110 |
return settings
|
| 111 |
|
| 112 |
+
@staticmethod
|
| 113 |
+
def save_to_ini(settings, filename):
|
| 114 |
+
config = configparser.ConfigParser()
|
| 115 |
+
config['Character'] = {
|
| 116 |
+
'chat_author_description': settings.get('chat_author_description', ''),
|
| 117 |
+
'chat_instructions': settings.get('chat_instructions', ''),
|
| 118 |
+
'example_qa': '\n'.join(settings.get('example_qa', [])),
|
| 119 |
+
'gen_author_description': settings.get('gen_author_description', '')
|
| 120 |
+
}
|
| 121 |
+
config['Models'] = {
|
| 122 |
+
'DEFAULT_CHAT_MODEL': settings.get('DEFAULT_CHAT_MODEL', ''),
|
| 123 |
+
'DEFAULT_GEN_MODEL': settings.get('DEFAULT_GEN_MODEL', '')
|
| 124 |
+
}
|
| 125 |
+
config['ChatParameters'] = {
|
| 126 |
+
'n_gpu_layers': str(settings.get('chat_n_gpu_layers', 0)),
|
| 127 |
+
'temperature': str(settings.get('chat_temperature', 0.5)),
|
| 128 |
+
'top_p': str(settings.get('chat_top_p', 0.7)),
|
| 129 |
+
'top_k': str(settings.get('chat_top_k', 80)),
|
| 130 |
+
'repetition_penalty': str(settings.get('chat_rep_pen', 1.2)),
|
| 131 |
+
'n_ctx': str(settings.get('chat_n_ctx', 10000))
|
| 132 |
+
}
|
| 133 |
+
config['GenerateParameters'] = {
|
| 134 |
+
'n_gpu_layers': str(settings.get('gen_n_gpu_layers', 0)),
|
| 135 |
+
'temperature': str(settings.get('gen_temperature', 0.35)),
|
| 136 |
+
'top_p': str(settings.get('gen_top_p', 0.9)),
|
| 137 |
+
'top_k': str(settings.get('gen_top_k', 40)),
|
| 138 |
+
'repetition_penalty': str(settings.get('gen_rep_pen', 1.2)),
|
| 139 |
+
'n_ctx': str(settings.get('gen_n_ctx', 10000))
|
| 140 |
+
}
|
| 141 |
+
ConfigManager.save_settings(config, filename)
|
| 142 |
+
|
| 143 |
+
@staticmethod
|
| 144 |
+
def create_default_ini(filename):
|
| 145 |
+
default_settings = {
|
| 146 |
+
'chat_author_description': "あなたは優秀な小説執筆アシスタントです。三幕構造や起承転結、劇中劇などのあらゆる小説理論や小説技法にも通じています。",
|
| 147 |
+
'chat_instructions': "丁寧な敬語でアイディアのヒアリングしてください。物語をより面白くする提案、キャラクター造形の考察、世界観を膨らませる手伝いなどをお願いします。求められた時以外は基本、聞き役に徹してユーザー自身に言語化させるよう促してください。ユーザーのことは『ユーザー』と呼んでください。",
|
| 148 |
+
'example_qa': [
|
| 149 |
+
"user: キャラクターの設定について悩んでいます。",
|
| 150 |
+
"assistant: キャラクター設定は物語の核となる重要な要素ですね。ユーザーが現在考えているキャラクターについて、簡単にご説明いただけますでしょうか?",
|
| 151 |
+
"user: どんな設定を説明をしたらいいでしょうか?",
|
| 152 |
+
"assistant: 例えば、年齢、性別、職業、性格の特徴などから始めていただけると、より具体的なアドバイスができるかと思います。",
|
| 153 |
+
"user: プロットを書き出したいので、ヒアリングお願いします。",
|
| 154 |
+
"assistant: 承知しました。ではまず『起承転結』の起から考えていきましょう。",
|
| 155 |
+
"user: 読者を惹きこむ為のコツを提案してください",
|
| 156 |
+
"assistant: 諸説ありますが、『謎・ピンチ・意外性』を冒頭に持ってくることが重要だと言います。",
|
| 157 |
+
"user: プロットが面白いか自信がないので、考察のお手伝いをお願いします。",
|
| 158 |
+
"assistant: プロットについてコメントをする前に、まずこの物語の『売り』について簡単に説明してください",
|
| 159 |
+
],
|
| 160 |
+
'gen_author_description': 'あなたは新進気鋭の和風伝奇ミステリー小説家で、細やかな筆致と巧みな構成で若い世代にとても人気があります。',
|
| 161 |
+
'DEFAULT_CHAT_MODEL': 'EZO-Common-9B-gemma-2-it.f16.gguf',
|
| 162 |
+
'DEFAULT_GEN_MODEL': 'EZO-Common-9B-gemma-2-it.f16.gguf',
|
| 163 |
+
'chat_n_gpu_layers': 0,
|
| 164 |
+
'chat_temperature': 0.5,
|
| 165 |
+
'chat_top_p': 0.7,
|
| 166 |
+
'chat_top_k': 80,
|
| 167 |
+
'chat_rep_pen': 1.2,
|
| 168 |
+
'chat_n_ctx': 10000,
|
| 169 |
+
'gen_n_gpu_layers': 0,
|
| 170 |
+
'gen_temperature': 0.35,
|
| 171 |
+
'gen_top_p': 0.9,
|
| 172 |
+
'gen_top_k': 40,
|
| 173 |
+
'gen_rep_pen': 1.2,
|
| 174 |
+
'gen_n_ctx': 10000
|
| 175 |
+
}
|
| 176 |
+
Settings.save_to_ini(default_settings, filename)
|
| 177 |
+
|
| 178 |
@staticmethod
|
| 179 |
def load_from_ini(filename):
|
| 180 |
config = ConfigManager.load_settings(filename)
|
| 181 |
return Settings._parse_config(config)
|
| 182 |
|
| 183 |
+
class GenTextParams:
|
| 184 |
+
def __init__(self):
|
| 185 |
+
self.gen_n_gpu_layers = 0
|
| 186 |
+
self.gen_temperature = 0.35
|
| 187 |
+
self.gen_top_p = 1.0
|
| 188 |
+
self.gen_top_k = 40
|
| 189 |
+
self.gen_rep_pen = 1.0
|
| 190 |
+
self.gen_n_ctx = 10000
|
| 191 |
+
self.chat_n_gpu_layers = 0
|
| 192 |
+
self.chat_temperature = 0.5
|
| 193 |
+
self.chat_top_p = 0.7
|
| 194 |
+
self.chat_top_k = 80
|
| 195 |
+
self.chat_rep_pen = 1.2
|
| 196 |
+
self.chat_n_ctx = 10000
|
| 197 |
+
|
| 198 |
+
def update_generate_parameters(self, n_gpu_layers, temperature, top_p, top_k, rep_pen, n_ctx):
|
| 199 |
+
self.gen_n_gpu_layers = n_gpu_layers
|
| 200 |
+
self.gen_temperature = temperature
|
| 201 |
+
self.gen_top_p = top_p
|
| 202 |
+
self.gen_top_k = top_k
|
| 203 |
+
self.gen_rep_pen = rep_pen
|
| 204 |
+
self.gen_n_ctx = n_ctx
|
| 205 |
+
|
| 206 |
+
def update_chat_parameters(self, n_gpu_layers, temperature, top_p, top_k, rep_pen, n_ctx):
|
| 207 |
+
self.chat_n_gpu_layers = n_gpu_layers
|
| 208 |
+
self.chat_temperature = temperature
|
| 209 |
+
self.chat_top_p = top_p
|
| 210 |
+
self.chat_top_k = top_k
|
| 211 |
+
self.chat_rep_pen = rep_pen
|
| 212 |
+
self.chat_n_ctx = n_ctx
|
| 213 |
+
|
| 214 |
+
class LlamaAdapter:
|
| 215 |
+
def __init__(self, model_path, params, n_gpu_layers):
|
| 216 |
+
self.llm = Llama(model_path=model_path, n_ctx=params.chat_n_ctx, n_gpu_layers=n_gpu_layers)
|
| 217 |
+
self.params = params
|
| 218 |
+
|
| 219 |
+
def generate_text(self, text, author_description, gen_characters, gen_token_multiplier, instruction):
|
| 220 |
+
max_tokens = int(gen_characters * gen_token_multiplier)
|
| 221 |
+
|
| 222 |
+
messages = [
|
| 223 |
+
{"role": "system", "content": author_description},
|
| 224 |
+
{"role": "user", "content": f"以下の指示に従ってテキストを生成してください:\n\n{instruction}\n\n生成するテキスト(目安は{gen_characters}文字):\n\n{text}"}
|
| 225 |
+
]
|
| 226 |
+
|
| 227 |
+
response = self.llm.create_chat_completion(
|
| 228 |
+
messages=messages,
|
| 229 |
+
max_tokens=max_tokens,
|
| 230 |
+
temperature=self.params.gen_temperature,
|
| 231 |
+
top_p=self.params.gen_top_p,
|
| 232 |
+
top_k=self.params.gen_top_k,
|
| 233 |
+
repeat_penalty=self.params.gen_rep_pen,
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
return response["choices"][0]["message"]["content"].strip()
|
| 237 |
+
|
| 238 |
+
def generate(self, prompt, max_new_tokens=10000, temperature=None, top_p=None, top_k=None, repeat_penalty=None):
|
| 239 |
+
if temperature is None:
|
| 240 |
+
temperature = self.params.chat_temperature
|
| 241 |
+
if top_p is None:
|
| 242 |
+
top_p = self.params.chat_top_p
|
| 243 |
+
if top_k is None:
|
| 244 |
+
top_k = self.params.chat_top_k
|
| 245 |
+
if repeat_penalty is None:
|
| 246 |
+
repeat_penalty = self.params.chat_rep_pen
|
| 247 |
+
|
| 248 |
+
response = self.llm(
|
| 249 |
+
prompt,
|
| 250 |
+
max_tokens=max_new_tokens,
|
| 251 |
+
temperature=temperature,
|
| 252 |
+
top_p=top_p,
|
| 253 |
+
top_k=top_k,
|
| 254 |
+
repeat_penalty=repeat_penalty,
|
| 255 |
+
stop=["user:", "・会話履歴", "<END>"]
|
| 256 |
+
)
|
| 257 |
+
|
| 258 |
+
# 返り値の形式が変更された可能性があるため、より柔軟に処理
|
| 259 |
+
if isinstance(response, dict) and "choices" in response:
|
| 260 |
+
return response["choices"][0]["text"]
|
| 261 |
+
elif isinstance(response, str):
|
| 262 |
+
return response
|
| 263 |
+
else:
|
| 264 |
+
raise ValueError(f"Unexpected response format: {type(response)}")
|
| 265 |
+
|
| 266 |
+
def create_chat_completion(self, messages, max_tokens, temperature, top_p, top_k, repeat_penalty):
|
| 267 |
+
return self.llm.create_chat_completion(
|
| 268 |
+
messages=messages,
|
| 269 |
+
max_tokens=max_tokens,
|
| 270 |
+
temperature=temperature,
|
| 271 |
+
top_p=top_p,
|
| 272 |
+
top_k=top_k,
|
| 273 |
+
repeat_penalty=repeat_penalty
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
class CharacterMaker:
|
| 277 |
def __init__(self):
|
| 278 |
+
self.llama = None
|
| 279 |
+
self.history = []
|
| 280 |
+
self.chat_history = []
|
| 281 |
self.settings = None
|
| 282 |
+
self.model_loaded = threading.Event()
|
| 283 |
self.current_model = None
|
| 284 |
+
self.model_lock = threading.Lock()
|
| 285 |
self.use_chat_format = False
|
|
|
|
| 286 |
|
| 287 |
@spaces.GPU(duration=120)
|
| 288 |
+
def load_model(self, model_type):
|
| 289 |
+
with self.model_lock:
|
| 290 |
+
if self.current_model == model_type:
|
| 291 |
+
return
|
| 292 |
|
| 293 |
+
self.model_loaded.clear()
|
| 294 |
+
if self.llama:
|
| 295 |
+
del self.llama
|
| 296 |
+
self.llama = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
+
try:
|
| 299 |
+
model_path = os.path.join(MODEL_DIR, self.settings[f'DEFAULT_{model_type.upper()}_MODEL'])
|
| 300 |
+
n_gpu_layers = self.settings[f'{model_type.lower()}_n_gpu_layers']
|
| 301 |
+
self.llama = LlamaAdapter(model_path, params, n_gpu_layers)
|
| 302 |
+
self.current_model = model_type
|
| 303 |
+
self.model_loaded.set()
|
| 304 |
+
print(f"{model_type} モデル {model_path} のロードが完了しました。(n_gpu_layers: {n_gpu_layers})")
|
| 305 |
+
except Exception as e:
|
| 306 |
+
print(f"{model_type} モデルのロード中にエラーが発生しました: {str(e)}")
|
| 307 |
+
self.model_loaded.set()
|
| 308 |
|
| 309 |
+
def generate_response(self, input_str):
|
| 310 |
+
self.load_model('CHAT')
|
| 311 |
+
if not self.model_loaded.wait(timeout=30) or not self.llama:
|
| 312 |
return "モデルのロードに失敗しました。設定を確認してください。"
|
| 313 |
|
| 314 |
+
try:
|
| 315 |
+
if self.use_chat_format:
|
| 316 |
+
chat_messages = [{"role": "system", "content": self.settings.get('chat_author_description', '')}]
|
| 317 |
+
chat_messages.extend(self.chat_history)
|
| 318 |
+
chat_messages.append({"role": "user", "content": input_str})
|
| 319 |
+
|
| 320 |
+
response = self.llama.llm.create_chat_completion(
|
| 321 |
+
messages=chat_messages,
|
| 322 |
+
max_tokens=1000,
|
| 323 |
+
temperature=self.llama.params.chat_temperature,
|
| 324 |
+
top_p=self.llama.params.chat_top_p,
|
| 325 |
+
top_k=self.llama.params.chat_top_k,
|
| 326 |
+
repeat_penalty=self.llama.params.chat_rep_pen,
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
res_text = response["choices"][0]["message"]["content"].strip()
|
| 330 |
+
self.chat_history.append({"role": "user", "content": input_str})
|
| 331 |
+
self.chat_history.append({"role": "assistant", "content": res_text})
|
| 332 |
+
else:
|
| 333 |
prompt = self._generate_prompt(input_str)
|
| 334 |
+
res_text = self.llama.generate(prompt, max_new_tokens=1000)
|
|
|
|
| 335 |
self.history.append({"user": input_str, "assistant": res_text})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
+
return res_text
|
| 338 |
+
except Exception as e:
|
| 339 |
+
print(f"レスポンス生成中にエラーが発生しました: {str(e)}")
|
| 340 |
+
return "レスポンス生成中にエラーが発生しました。設定を確認してください。"
|
| 341 |
+
|
| 342 |
+
def generate_text(self, text, gen_characters, gen_token_multiplier, instruction):
|
| 343 |
+
self.load_model('GEN')
|
| 344 |
+
if not self.model_loaded.wait(timeout=30) or not self.llama:
|
| 345 |
+
return "モデルのロードに失敗しました。設定を確認してください。"
|
| 346 |
+
|
| 347 |
+
author_description = self.settings.get('gen_author_description', '')
|
| 348 |
+
max_tokens = int(gen_characters * gen_token_multiplier)
|
| 349 |
+
|
| 350 |
+
try:
|
| 351 |
+
if self.use_chat_format:
|
| 352 |
+
messages = [
|
| 353 |
+
{"role": "system", "content": author_description},
|
| 354 |
+
{"role": "user", "content": f"以下の指示に従ってテキストを生成してください:\n\n{instruction}\n\n生成���るテキスト(目安は{gen_characters}文字):\n\n{text}"}
|
| 355 |
+
]
|
| 356 |
+
|
| 357 |
+
response = self.llama.create_chat_completion(
|
| 358 |
+
messages=messages,
|
| 359 |
max_tokens=max_tokens,
|
| 360 |
+
temperature=self.llama.params.gen_temperature,
|
| 361 |
+
top_p=self.llama.params.gen_top_p,
|
| 362 |
+
top_k=self.llama.params.gen_top_k,
|
| 363 |
+
repeat_penalty=self.llama.params.gen_rep_pen,
|
| 364 |
)
|
| 365 |
|
| 366 |
+
generated_text = response["choices"][0]["message"]["content"].strip()
|
| 367 |
+
else:
|
| 368 |
+
prompt = f"{author_description}\n\n以下の指示に従ってテキストを生成してください:\n\n{instruction}\n\n生成するテキスト(目安は{gen_characters}文字):\n\n{text}\n\n生成されたテキスト:"
|
| 369 |
+
generated_text = self.llama.generate(
|
| 370 |
+
prompt,
|
| 371 |
+
max_new_tokens=max_tokens
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
return generated_text
|
| 375 |
+
except Exception as e:
|
| 376 |
+
print(f"テキスト生成中にエラーが発生しました: {str(e)}")
|
| 377 |
+
return "テキスト生成中にエラーが発生しました。設定を確認してください。"
|
| 378 |
+
|
| 379 |
+
def set_chat_format(self, use_chat_format):
|
| 380 |
+
self.use_chat_format = use_chat_format
|
| 381 |
+
|
| 382 |
+
|
| 383 |
+
def make_prompt(self, input_str: str):
|
| 384 |
+
prompt_template = """{{chat_author_description}}
|
| 385 |
+
|
| 386 |
+
{{chat_instructions}}
|
| 387 |
+
|
| 388 |
+
・キャラクターの回答例
|
| 389 |
+
{% for qa in example_qa %}
|
| 390 |
+
{{qa}}
|
| 391 |
+
{% endfor %}
|
| 392 |
+
|
| 393 |
+
・会話履歴
|
| 394 |
+
{% for history in histories %}
|
| 395 |
+
user: {{history.user}}
|
| 396 |
+
assistant: {{history.assistant}}
|
| 397 |
+
{% endfor %}
|
| 398 |
+
|
| 399 |
+
user: {{input_str}}
|
| 400 |
+
assistant:"""
|
| 401 |
+
|
| 402 |
+
template = Template(prompt_template)
|
| 403 |
+
return template.render(
|
| 404 |
+
chat_author_description=self.settings.get('chat_author_description', ''),
|
| 405 |
+
chat_instructions=self.settings.get('chat_instructions', ''),
|
| 406 |
+
example_qa=self.settings.get('example_qa', []),
|
| 407 |
+
histories=self.history,
|
| 408 |
+
input_str=input_str
|
| 409 |
+
)
|
| 410 |
+
|
| 411 |
+
def _generate_prompt(self, input_str: str):
|
| 412 |
+
return self.make_prompt(input_str)
|
| 413 |
|
| 414 |
+
def load_character(self, filename):
|
| 415 |
+
if isinstance(filename, list):
|
| 416 |
+
filename = filename[0] if filename else ""
|
| 417 |
self.settings = Settings.load_from_ini(filename)
|
| 418 |
|
| 419 |
def reset(self):
|
| 420 |
self.history = []
|
| 421 |
+
self.chat_history = []
|
| 422 |
self.use_chat_format = False
|
| 423 |
|
| 424 |
# グローバル変数
|
| 425 |
+
params = GenTextParams()
|
| 426 |
+
character_maker = CharacterMaker()
|
| 427 |
model_files = ModelManager.get_model_files()
|
| 428 |
|
| 429 |
+
# チャット関連関数
|
| 430 |
+
def chat_with_character(message, history):
|
| 431 |
+
if character_maker.use_chat_format:
|
| 432 |
+
character_maker.chat_history = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg} for i, msg in enumerate(sum(history, []))]
|
| 433 |
+
else:
|
| 434 |
+
character_maker.history = [{"user": h[0], "assistant": h[1]} for h in history]
|
| 435 |
+
return character_maker.generate_response(message)
|
| 436 |
+
|
| 437 |
+
def chat_with_character_stream(message, history):
|
| 438 |
+
if character_maker.use_chat_format:
|
| 439 |
+
character_maker.chat_history = [{"role": "user" if i % 2 == 0 else "assistant", "content": msg} for i, msg in enumerate(sum(history, []))]
|
| 440 |
+
else:
|
| 441 |
+
character_maker.history = [{"user": h[0], "assistant": h[1]} for h in history]
|
| 442 |
+
response = character_maker.generate_response(message)
|
| 443 |
+
for i in range(len(response)):
|
| 444 |
+
time.sleep(0.05) # 各文字の表示間隔を調整
|
| 445 |
+
yield response[:i+1]
|
| 446 |
+
def clear_chat():
|
| 447 |
+
character_maker.reset()
|
| 448 |
+
return []
|
| 449 |
+
|
| 450 |
+
# ログ関連関数
|
| 451 |
+
def list_log_files():
|
| 452 |
+
logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
|
| 453 |
+
if not os.path.exists(logs_dir):
|
| 454 |
+
return []
|
| 455 |
+
return [f for f in os.listdir(logs_dir) if f.endswith('.csv')]
|
| 456 |
+
|
| 457 |
+
def load_chat_log(file_name):
|
| 458 |
+
file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs", file_name)
|
| 459 |
+
chat_history = []
|
| 460 |
+
with open(file_path, 'r', encoding='utf-8') as csvfile:
|
| 461 |
+
reader = csv.reader(csvfile)
|
| 462 |
+
next(reader) # Skip header
|
| 463 |
+
for row in reader:
|
| 464 |
+
if len(row) == 2:
|
| 465 |
+
role, message = row
|
| 466 |
+
if role == "user":
|
| 467 |
+
chat_history.append([message, None])
|
| 468 |
+
elif role == "assistant":
|
| 469 |
+
if chat_history and chat_history[-1][1] is None:
|
| 470 |
+
chat_history[-1][1] = message
|
| 471 |
+
else:
|
| 472 |
+
chat_history.append([None, message])
|
| 473 |
+
return chat_history
|
| 474 |
+
|
| 475 |
+
def save_chat_log(chat_history):
|
| 476 |
+
current_time = datetime.datetime.now().strftime("%Y%m%d%H%M%S")
|
| 477 |
+
filename = f"{current_time}.csv"
|
| 478 |
+
logs_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs")
|
| 479 |
+
if not os.path.exists(logs_dir):
|
| 480 |
+
os.makedirs(logs_dir)
|
| 481 |
+
file_path = os.path.join(logs_dir, filename)
|
| 482 |
+
|
| 483 |
+
with open(file_path, 'w', newline='', encoding='utf-8') as csvfile:
|
| 484 |
+
writer = csv.writer(csvfile)
|
| 485 |
+
writer.writerow(["Role", "Message"])
|
| 486 |
+
for user_message, assistant_message in chat_history:
|
| 487 |
+
if user_message:
|
| 488 |
+
writer.writerow(["user", user_message])
|
| 489 |
+
if assistant_message:
|
| 490 |
+
writer.writerow(["assistant", assistant_message])
|
| 491 |
+
|
| 492 |
+
return f"チャットログが {file_path} に保存されました。"
|
| 493 |
+
|
| 494 |
+
def resume_chat_from_log(chat_history):
|
| 495 |
+
# チャットボットのUIを更新
|
| 496 |
+
chatbot_ui = gr.update(value=chat_history)
|
| 497 |
+
|
| 498 |
+
# LLMの履歴を更新
|
| 499 |
+
character_maker.history = [{"user": h[0], "assistant": h[1]} for h in chat_history if h[0] is not None and h[1] is not None]
|
| 500 |
+
|
| 501 |
+
return chatbot_ui
|
| 502 |
+
|
| 503 |
+
# グローバル変数として定義
|
| 504 |
+
temp_settings = {}
|
| 505 |
+
|
| 506 |
+
def update_temp_setting(section, key, value):
|
| 507 |
+
global temp_settings
|
| 508 |
+
if section not in temp_settings:
|
| 509 |
+
temp_settings[section] = {}
|
| 510 |
+
temp_settings[section][key] = value
|
| 511 |
+
return f"{section}セクションの{key}を更新しました。適用ボタンを押すと設定が保存されます。"
|
| 512 |
+
|
| 513 |
+
def build_model_settings(config, section, output):
|
| 514 |
+
model_settings = []
|
| 515 |
+
|
| 516 |
+
for key in ['DEFAULT_CHAT_MODEL', 'DEFAULT_GEN_MODEL']:
|
| 517 |
+
if key in config[section]:
|
| 518 |
+
with gr.Row():
|
| 519 |
+
dropdown = gr.Dropdown(
|
| 520 |
+
label=key,
|
| 521 |
+
choices=ModelManager.get_model_files(),
|
| 522 |
+
value=config[section][key]
|
| 523 |
+
)
|
| 524 |
+
refresh_button = gr.Button("更新", size="sm")
|
| 525 |
+
status_message = gr.Markdown()
|
| 526 |
+
|
| 527 |
+
def update_dropdown(current_value):
|
| 528 |
+
model_files = ModelManager.get_model_files()
|
| 529 |
+
if current_value not in model_files:
|
| 530 |
+
model_files.insert(0, current_value)
|
| 531 |
+
status = f"現在の{key}({current_value})が見つかりません。ダウンロードしてください。"
|
| 532 |
+
else:
|
| 533 |
+
status = "モデルリストを更新しました。"
|
| 534 |
+
return gr.update(choices=model_files, value=current_value), status
|
| 535 |
+
|
| 536 |
+
refresh_button.click(
|
| 537 |
+
fn=update_dropdown,
|
| 538 |
+
inputs=[dropdown],
|
| 539 |
+
outputs=[dropdown, status_message]
|
| 540 |
+
)
|
| 541 |
+
|
| 542 |
+
dropdown.change(
|
| 543 |
+
partial(update_temp_setting, 'Models', key),
|
| 544 |
+
inputs=[dropdown],
|
| 545 |
+
outputs=[output]
|
| 546 |
+
)
|
| 547 |
+
|
| 548 |
+
model_settings.extend([dropdown, refresh_button, status_message])
|
| 549 |
+
|
| 550 |
+
return model_settings
|
| 551 |
+
|
| 552 |
+
def apply_settings():
|
| 553 |
+
global temp_settings
|
| 554 |
+
for section, settings in temp_settings.items():
|
| 555 |
+
for key, value in settings.items():
|
| 556 |
+
ConfigManager.update_setting(section, key, str(value), DEFAULT_INI_FILE)
|
| 557 |
+
|
| 558 |
+
# iniファイルを再読み込み
|
| 559 |
+
new_config = ConfigManager.load_settings(DEFAULT_INI_FILE)
|
| 560 |
+
|
| 561 |
+
# 設定を更新
|
| 562 |
+
character_maker.settings = Settings._parse_config(new_config)
|
| 563 |
+
|
| 564 |
+
# パラメータを更新
|
| 565 |
+
if 'ChatParameters' in new_config:
|
| 566 |
+
params.update_chat_parameters(
|
| 567 |
+
int(new_config['ChatParameters'].get('n_gpu_layers', '0')),
|
| 568 |
+
float(new_config['ChatParameters'].get('temperature', '0.5')),
|
| 569 |
+
float(new_config['ChatParameters'].get('top_p', '0.7')),
|
| 570 |
+
int(new_config['ChatParameters'].get('top_k', '80')),
|
| 571 |
+
float(new_config['ChatParameters'].get('repetition_penalty', '1.2')),
|
| 572 |
+
int(new_config['ChatParameters'].get('n_ctx', '10000'))
|
| 573 |
+
)
|
| 574 |
+
if 'GenerateParameters' in new_config:
|
| 575 |
+
params.update_generate_parameters(
|
| 576 |
+
int(new_config['GenerateParameters'].get('n_gpu_layers', '0')),
|
| 577 |
+
float(new_config['GenerateParameters'].get('temperature', '0.35')),
|
| 578 |
+
float(new_config['GenerateParameters'].get('top_p', '0.9')),
|
| 579 |
+
int(new_config['GenerateParameters'].get('top_k', '40')),
|
| 580 |
+
float(new_config['GenerateParameters'].get('repetition_penalty', '1.2')),
|
| 581 |
+
int(new_config['GenerateParameters'].get('n_ctx', '10000'))
|
| 582 |
+
)
|
| 583 |
+
|
| 584 |
+
# モデルを再ロード
|
| 585 |
+
character_maker.current_model = None
|
| 586 |
+
|
| 587 |
+
# temp_settings をクリア
|
| 588 |
+
temp_settings.clear()
|
| 589 |
+
|
| 590 |
+
return "設定をiniファイルに保存し、アプリケーションに反映しました。次回の操作時に新しいモデルがロードされます。"
|
| 591 |
+
|
| 592 |
# Gradioインターフェース
|
| 593 |
def build_gradio_interface():
|
| 594 |
+
global temp_settings
|
| 595 |
+
|
| 596 |
+
def apply_settings_wrapper():
|
| 597 |
+
return apply_settings()
|
| 598 |
+
|
| 599 |
+
def update_temp_setting(section, key, value):
|
| 600 |
+
global temp_settings
|
| 601 |
+
if section not in temp_settings:
|
| 602 |
+
temp_settings[section] = {}
|
| 603 |
+
temp_settings[section][key] = value
|
| 604 |
+
return f"{section}セクションの{key}を更新しました。適用ボタンを押すと設定が保存されます。"
|
| 605 |
+
|
| 606 |
with gr.Blocks() as iface:
|
| 607 |
gr.HTML("""
|
| 608 |
<style>
|
| 609 |
+
#chatbot, #chatbot_read {
|
| 610 |
resize: both;
|
| 611 |
overflow: auto;
|
| 612 |
min-height: 100px;
|
|
|
|
| 614 |
}
|
| 615 |
</style>
|
| 616 |
""")
|
| 617 |
+
tabs = gr.Tabs()
|
| 618 |
+
with tabs:
|
| 619 |
+
with gr.Tab("チャット", id="chat_tab") as chat_tab:
|
| 620 |
+
chatbot = gr.Chatbot(elem_id="chatbot")
|
| 621 |
+
chat_interface = gr.ChatInterface(
|
| 622 |
+
chat_with_character_stream,
|
| 623 |
+
chatbot=chatbot,
|
| 624 |
+
textbox=gr.Textbox(placeholder="メッセージを入力してください...", container=False, scale=7),
|
| 625 |
+
theme="soft",
|
| 626 |
+
submit_btn="送信",
|
| 627 |
+
stop_btn="停止",
|
| 628 |
+
retry_btn="もう一度生成",
|
| 629 |
+
undo_btn="前のメッセージを取り消す",
|
| 630 |
+
clear_btn="チャットをクリア",
|
| 631 |
+
)
|
| 632 |
+
|
| 633 |
+
with gr.Row():
|
| 634 |
+
save_log_button = gr.Button("チャットログを保存")
|
| 635 |
+
|
| 636 |
+
save_log_output = gr.Textbox(label="保存状態")
|
| 637 |
+
|
| 638 |
+
save_log_button.click(
|
| 639 |
+
save_chat_log,
|
| 640 |
+
inputs=[chatbot],
|
| 641 |
+
outputs=[save_log_output]
|
| 642 |
+
)
|
| 643 |
+
|
| 644 |
+
with gr.Tab("文章生成"):
|
| 645 |
+
with gr.Row():
|
| 646 |
+
with gr.Column(scale=2):
|
| 647 |
+
instruction_type = gr.Dropdown(
|
| 648 |
+
choices=["自由入力", "推敲", "プロット作成", "あらすじ作成"],
|
| 649 |
+
label="指示タイプ",
|
| 650 |
+
value="自由入力"
|
| 651 |
+
)
|
| 652 |
+
gen_instruction = gr.Textbox(
|
| 653 |
+
label="指示",
|
| 654 |
+
value="",
|
| 655 |
+
lines=3
|
| 656 |
+
)
|
| 657 |
+
gen_input_text = gr.Textbox(lines=5, label="処理されるテキストを入力してください")
|
| 658 |
+
gen_input_char_count = gr.HTML(value="文字数: 0")
|
| 659 |
+
with gr.Column(scale=1):
|
| 660 |
+
gen_characters = gr.Slider(minimum=10, maximum=10000, value=500, step=10, label="出力文字数", info="出力文字数の目安")
|
| 661 |
+
gen_token_multiplier = gr.Slider(minimum=0.5, maximum=3, value=1.75, step=0.01, label="文字/トークン数倍率", info="文字/最大トークン数倍率")
|
| 662 |
+
|
| 663 |
+
generate_button = gr.Button("文章生成開始")
|
| 664 |
+
generated_output = gr.Textbox(label="生成された文章")
|
| 665 |
|
| 666 |
+
generate_button.click(
|
| 667 |
+
character_maker.generate_text,
|
| 668 |
+
inputs=[gen_input_text, gen_characters, gen_token_multiplier, gen_instruction],
|
| 669 |
+
outputs=[generated_output]
|
| 670 |
+
)
|
|
|
|
|
|
|
|
|
|
| 671 |
|
| 672 |
+
def update_instruction(choice):
|
| 673 |
+
instructions = {
|
| 674 |
+
"自由入力": "",
|
| 675 |
+
"推敲": "以下のテキストを推敲してください。原文の文体や特徴的な表現は保持しつつ、必要に応じて微調整を加えてください。文章の流れを自然にし、表現を洗練させることが目標ですが、元の雰囲気や個性を損なわないよう注意してください。",
|
| 676 |
+
"プロット作成": "以下のテキストをプロットにしてください。起承転結に分割すること。",
|
| 677 |
+
"あらすじ作成": "以下のテキストをあらすじにして、簡潔にまとめて下さい。",
|
| 678 |
+
}
|
| 679 |
+
return instructions.get(choice, "")
|
| 680 |
+
|
| 681 |
+
instruction_type.change(
|
| 682 |
+
update_instruction,
|
| 683 |
+
inputs=[instruction_type],
|
| 684 |
+
outputs=[gen_instruction]
|
| 685 |
+
)
|
|
|
|
| 686 |
|
| 687 |
+
def update_char_count(text):
|
| 688 |
+
return f"文字数: {len(text)}"
|
| 689 |
|
| 690 |
+
gen_input_text.change(
|
| 691 |
+
update_char_count,
|
| 692 |
+
inputs=[gen_input_text],
|
| 693 |
+
outputs=[gen_input_char_count]
|
| 694 |
+
)
|
| 695 |
|
| 696 |
+
with gr.Tab("ログ閲覧", id="log_view_tab") as log_view_tab:
|
| 697 |
+
gr.Markdown("## チャットログ閲覧")
|
| 698 |
+
chatbot_read = gr.Chatbot(elem_id="chatbot_read")
|
| 699 |
+
log_file_dropdown = gr.Dropdown(label="ログファイル選択", choices=list_log_files())
|
| 700 |
+
refresh_log_list_button = gr.Button("ログファイルリストを更新")
|
| 701 |
+
resume_chat_button = gr.Button("選択したログから会話を再開")
|
| 702 |
|
| 703 |
+
def update_log_dropdown():
|
| 704 |
+
return gr.update(choices=list_log_files())
|
| 705 |
+
|
| 706 |
+
def load_and_display_chat_log(file_name):
|
| 707 |
+
chat_history = load_chat_log(file_name)
|
| 708 |
+
return gr.update(value=chat_history)
|
| 709 |
+
|
| 710 |
+
refresh_log_list_button.click(
|
| 711 |
+
update_log_dropdown,
|
| 712 |
+
outputs=[log_file_dropdown]
|
|
|
|
| 713 |
)
|
| 714 |
|
| 715 |
+
log_file_dropdown.change(
|
| 716 |
+
load_and_display_chat_log,
|
| 717 |
+
inputs=[log_file_dropdown],
|
| 718 |
+
outputs=[chatbot_read]
|
|
|
|
| 719 |
)
|
| 720 |
+
|
| 721 |
+
def resume_chat_and_switch_tab(chat_history):
|
| 722 |
+
chatbot_ui = resume_chat_from_log(chat_history)
|
| 723 |
+
return chatbot_ui, gr.update(selected="chat_tab")
|
| 724 |
+
|
| 725 |
+
resume_chat_button.click(
|
| 726 |
+
resume_chat_and_switch_tab,
|
| 727 |
+
inputs=[chatbot_read],
|
| 728 |
+
outputs=[chatbot, tabs]
|
| 729 |
)
|
| 730 |
|
| 731 |
+
with gr.Tab("設定"):
|
| 732 |
+
output = gr.Textbox(label="更新状態")
|
| 733 |
+
|
| 734 |
+
config = ConfigManager.load_settings(DEFAULT_INI_FILE)
|
| 735 |
+
|
| 736 |
+
with gr.Column():
|
| 737 |
+
gr.Markdown("### モデル設定")
|
| 738 |
+
model_settings = build_model_settings(config, "Models", output)
|
| 739 |
+
|
| 740 |
+
gr.Markdown("### チャット設定")
|
| 741 |
+
for key in ['chat_author_description', 'chat_instructions', 'example_qa']:
|
| 742 |
+
if key == 'example_qa':
|
| 743 |
+
input_component = gr.TextArea(label=key, value=config['Character'].get(key, ''), lines=10)
|
| 744 |
+
else:
|
| 745 |
+
input_component = gr.TextArea(label=key, value=config['Character'].get(key, ''), lines=5)
|
| 746 |
+
input_component.change(
|
| 747 |
+
partial(update_temp_setting, 'Character', key),
|
| 748 |
+
inputs=[input_component],
|
| 749 |
+
outputs=[output]
|
| 750 |
+
)
|
| 751 |
+
|
| 752 |
+
gr.Markdown("### 文章生成設定")
|
| 753 |
+
key = 'gen_author_description'
|
| 754 |
+
input_component = gr.TextArea(label=key, value=config['Character'].get(key, ''), lines=5)
|
| 755 |
input_component.change(
|
| 756 |
+
partial(update_temp_setting, 'Character', key),
|
| 757 |
inputs=[input_component],
|
| 758 |
outputs=[output]
|
| 759 |
)
|
| 760 |
|
| 761 |
+
gr.Markdown("### チャットパラメータ設定")
|
| 762 |
+
for key in ['n_gpu_layers', 'temperature', 'top_p', 'top_k', 'repetition_penalty', 'n_ctx']:
|
| 763 |
+
value = config['ChatParameters'].get(key, '0')
|
| 764 |
+
if key == 'n_gpu_layers':
|
| 765 |
+
input_component = gr.Slider(label=key, value=int(value), minimum=-1, maximum=255, step=1)
|
| 766 |
+
elif key in ['temperature', 'top_p', 'repetition_penalty']:
|
| 767 |
+
input_component = gr.Slider(label=key, value=float(value), minimum=0.0, maximum=1.0, step=0.05)
|
| 768 |
+
elif key == 'top_k':
|
| 769 |
+
input_component = gr.Slider(label=key, value=int(value), minimum=1, maximum=200, step=1)
|
| 770 |
+
elif key == 'n_ctx':
|
| 771 |
+
input_component = gr.Slider(label=key, value=int(value), minimum=10000, maximum=100000, step=1000)
|
| 772 |
+
else:
|
| 773 |
+
input_component = gr.Textbox(label=key, value=value)
|
| 774 |
+
|
| 775 |
+
input_component.change(
|
| 776 |
+
partial(update_temp_setting, 'ChatParameters', key),
|
| 777 |
+
inputs=[input_component],
|
| 778 |
+
outputs=[output]
|
| 779 |
+
)
|
| 780 |
+
|
| 781 |
+
gr.Markdown("### 文章生成パラメータ設定")
|
| 782 |
+
for key in ['n_gpu_layers', 'temperature', 'top_p', 'top_k', 'repetition_penalty', 'n_ctx']:
|
| 783 |
+
value = config['GenerateParameters'].get(key, '0')
|
| 784 |
+
if key == 'n_gpu_layers':
|
| 785 |
+
input_component = gr.Slider(label=key, value=int(value), minimum=-1, maximum=255, step=1)
|
| 786 |
+
elif key in ['temperature', 'top_p', 'repetition_penalty']:
|
| 787 |
+
input_component = gr.Slider(label=key, value=float(value), minimum=0.0, maximum=1.0, step=0.05)
|
| 788 |
+
elif key == 'top_k':
|
| 789 |
+
input_component = gr.Slider(label=key, value=int(value), minimum=1, maximum=200, step=1)
|
| 790 |
+
elif key == 'n_ctx':
|
| 791 |
+
input_component = gr.Slider(label=key, value=int(value), minimum=10000, maximum=100000, step=1000)
|
| 792 |
+
else:
|
| 793 |
+
input_component = gr.Textbox(label=key, value=value)
|
| 794 |
+
|
| 795 |
+
input_component.change(
|
| 796 |
+
partial(update_temp_setting, 'GenerateParameters', key),
|
| 797 |
+
inputs=[input_component],
|
| 798 |
+
outputs=[output]
|
| 799 |
+
)
|
| 800 |
+
|
| 801 |
+
apply_ini_settings_button = gr.Button("設定を適用")
|
| 802 |
+
apply_ini_settings_button.click(
|
| 803 |
+
apply_settings,
|
| 804 |
+
outputs=[output]
|
| 805 |
+
)
|
| 806 |
return iface
|
| 807 |
|
| 808 |
+
async def start_gradio():
|
| 809 |
if not os.path.exists(DEFAULT_INI_FILE):
|
| 810 |
print(f"{DEFAULT_INI_FILE} が見つかりません。デフォルト設定で作成します。")
|
| 811 |
+
Settings.create_default_ini(DEFAULT_INI_FILE)
|
| 812 |
|
| 813 |
+
config = ConfigManager.load_settings(DEFAULT_INI_FILE)
|
| 814 |
+
settings = Settings._parse_config(config)
|
| 815 |
+
|
| 816 |
+
character_maker.settings = settings
|
| 817 |
+
character_maker.load_character(DEFAULT_INI_FILE)
|
| 818 |
+
|
| 819 |
+
# パラメータの初期化
|
| 820 |
+
params.update_chat_parameters(
|
| 821 |
+
settings['chat_n_gpu_layers'],
|
| 822 |
+
settings['chat_temperature'],
|
| 823 |
+
settings['chat_top_p'],
|
| 824 |
+
settings['chat_top_k'],
|
| 825 |
+
settings['chat_rep_pen'],
|
| 826 |
+
settings['chat_n_ctx']
|
| 827 |
+
)
|
| 828 |
+
params.update_generate_parameters(
|
| 829 |
+
settings['gen_n_gpu_layers'],
|
| 830 |
+
settings['gen_temperature'],
|
| 831 |
+
settings['gen_top_p'],
|
| 832 |
+
settings['gen_top_k'],
|
| 833 |
+
settings['gen_rep_pen'],
|
| 834 |
+
settings['gen_n_ctx']
|
| 835 |
+
)
|
| 836 |
|
| 837 |
demo = build_gradio_interface()
|
| 838 |
+
|
| 839 |
+
ip_address = NetworkUtils.get_ip_address()
|
| 840 |
+
starting_port = 7860
|
| 841 |
+
port = NetworkUtils.find_available_port(starting_port)
|
| 842 |
+
print(f"サーバーのアドレス: http://{ip_address}:{port}")
|
| 843 |
+
|
| 844 |
+
demo.queue()
|
| 845 |
+
demo.launch(
|
| 846 |
+
server_name='0.0.0.0',
|
| 847 |
+
server_port=port,
|
| 848 |
+
share=True,
|
| 849 |
+
favicon_path=os.path.join(os.path.dirname(os.path.abspath(__file__)), "custom.html")
|
| 850 |
+
)
|
| 851 |
+
|
| 852 |
+
if __name__ == "__main__":
|
| 853 |
+
asyncio.run(start_gradio())
|