Update App2.py
Browse files
App2.py
CHANGED
|
@@ -86,33 +86,40 @@ def generate_response(message, history, temperature, top_p, top_k, max_output_to
|
|
| 86 |
return "", history, history
|
| 87 |
|
| 88 |
|
| 89 |
-
# =====
|
| 90 |
THEMES = {
|
| 91 |
"Eternalstar": "Sakalti/Eternalstar",
|
| 92 |
"Mountainrainbow": "Sakalti/mountanrainbow"
|
| 93 |
}
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
return f"""
|
| 98 |
-
let old = document.getElementById("dynamic-theme");
|
| 99 |
-
if (old) old.remove();
|
| 100 |
-
let link = document.createElement("link");
|
| 101 |
-
link.id = "dynamic-theme";
|
| 102 |
-
link.rel = "stylesheet";
|
| 103 |
-
link.href = "{css_link}";
|
| 104 |
-
document.head.appendChild(link);
|
| 105 |
-
"""
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
gr.Markdown("## Gemini AIキャラクターチャット")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
| 113 |
|
| 114 |
with gr.Tab("チャット"):
|
| 115 |
-
chatbot = gr.Chatbot(type="messages")
|
| 116 |
msg = gr.Textbox(placeholder="メッセージを入力...")
|
| 117 |
state = gr.State([])
|
| 118 |
system_prompt = gr.Textbox(label="キャラのシステムプロンプト", lines=4)
|
|
@@ -159,12 +166,5 @@ with gr.Blocks(theme=THEMES["Eternalstar"]) as demo:
|
|
| 159 |
character_list.change(load_prompt, inputs=[character_list], outputs=[system_prompt])
|
| 160 |
demo.load(refresh_characters, outputs=[character_list])
|
| 161 |
|
| 162 |
-
# ドロップダウンでテーマ切り替え
|
| 163 |
-
theme_dropdown.change(
|
| 164 |
-
fn=None,
|
| 165 |
-
inputs=[theme_dropdown],
|
| 166 |
-
outputs=[],
|
| 167 |
-
js=theme_js # ← Gradio v4 は _js ではなく js を直接渡せる
|
| 168 |
-
)
|
| 169 |
|
| 170 |
demo.launch()
|
|
|
|
| 86 |
return "", history, history
|
| 87 |
|
| 88 |
|
| 89 |
+
# ===== 再ロード方式のテーマ設定 =====
|
| 90 |
THEMES = {
|
| 91 |
"Eternalstar": "Sakalti/Eternalstar",
|
| 92 |
"Mountainrainbow": "Sakalti/mountanrainbow"
|
| 93 |
}
|
| 94 |
|
| 95 |
+
# 保存用ファイル
|
| 96 |
+
THEME_FILE = "theme_choice.txt"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
def save_theme(choice):
|
| 99 |
+
with open(THEME_FILE, "w", encoding="utf-8") as f:
|
| 100 |
+
f.write(choice)
|
| 101 |
+
return f"テーマを「{choice}」に変更しました。再起動してください。"
|
| 102 |
|
| 103 |
+
# 前回の選択をロード
|
| 104 |
+
if os.path.exists(THEME_FILE):
|
| 105 |
+
with open(THEME_FILE, "r", encoding="utf-8") as f:
|
| 106 |
+
theme_choice = f.read().strip()
|
| 107 |
+
else:
|
| 108 |
+
theme_choice = "Eternalstar"
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
# ===== Gradio UI構築 =====
|
| 112 |
+
with gr.Blocks(theme=THEMES[theme_choice]) as demo:
|
| 113 |
gr.Markdown("## Gemini AIキャラクターチャット")
|
| 114 |
|
| 115 |
+
with gr.Row():
|
| 116 |
+
theme_dropdown = gr.Dropdown(choices=list(THEMES.keys()), value=theme_choice, label="テーマ切り替え")
|
| 117 |
+
theme_status = gr.Textbox(label="テーマステータス", interactive=False)
|
| 118 |
+
|
| 119 |
+
theme_dropdown.change(save_theme, inputs=[theme_dropdown], outputs=[theme_status])
|
| 120 |
|
| 121 |
with gr.Tab("チャット"):
|
| 122 |
+
chatbot = gr.Chatbot(type="messages")
|
| 123 |
msg = gr.Textbox(placeholder="メッセージを入力...")
|
| 124 |
state = gr.State([])
|
| 125 |
system_prompt = gr.Textbox(label="キャラのシステムプロンプト", lines=4)
|
|
|
|
| 166 |
character_list.change(load_prompt, inputs=[character_list], outputs=[system_prompt])
|
| 167 |
demo.load(refresh_characters, outputs=[character_list])
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
|
| 170 |
demo.launch()
|