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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import re
|
| 3 |
+
import hashlib
|
| 4 |
+
import sqlite3
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
import cohere
|
| 8 |
+
import os
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
|
| 11 |
+
import numpy as np
|
| 12 |
+
import psycopg2
|
| 13 |
+
from google import genai
|
| 14 |
+
|
| 15 |
+
import uuid
|
| 16 |
+
import base64
|
| 17 |
+
import requests
|
| 18 |
+
import asyncio
|
| 19 |
+
import random
|
| 20 |
+
import pandas as pd
|
| 21 |
+
|
| 22 |
+
load_dotenv(verbose=True)
|
| 23 |
+
|
| 24 |
+
# Initialize Qdrant and Cohere clients
|
| 25 |
+
co = cohere.ClientV2(api_key=os.environ.get("COHERE_API_KEY"))
|
| 26 |
+
client = genai.Client(api_key=os.environ["GEMINI_API_KEY"])
|
| 27 |
+
|
| 28 |
+
# Database Initialization and Utility Functions
|
| 29 |
+
def init_db():
|
| 30 |
+
conn = sqlite3.connect('auth.db')
|
| 31 |
+
c = conn.cursor()
|
| 32 |
+
c.execute('''
|
| 33 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 34 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 35 |
+
username TEXT UNIQUE NOT NULL,
|
| 36 |
+
email TEXT UNIQUE NOT NULL,
|
| 37 |
+
password TEXT NOT NULL,
|
| 38 |
+
phone TEXT,
|
| 39 |
+
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
| 40 |
+
)
|
| 41 |
+
''')
|
| 42 |
+
conn.commit()
|
| 43 |
+
conn.close()
|
| 44 |
+
|
| 45 |
+
init_db()
|
| 46 |
+
|
| 47 |
+
def hash_password(password):
|
| 48 |
+
return hashlib.sha256(password.encode()).hexdigest()
|
| 49 |
+
|
| 50 |
+
def validate_username(username):
|
| 51 |
+
if len(username) < 4:
|
| 52 |
+
return "ユーザー名は4文字以上である必要があります"
|
| 53 |
+
if not re.match("^[a-zA-Z0-9_]+$", username):
|
| 54 |
+
return "ユーザー名には文字、数字、アンダースコアのみ使用できます"
|
| 55 |
+
return None
|
| 56 |
+
|
| 57 |
+
def validate_email(email):
|
| 58 |
+
if not re.match(r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$", email):
|
| 59 |
+
return "有効なメールアドレスを入力してください"
|
| 60 |
+
return None
|
| 61 |
+
|
| 62 |
+
def validate_password(password):
|
| 63 |
+
if len(password) < 8:
|
| 64 |
+
return "パスワードは8文字以上でなければなりません"
|
| 65 |
+
if not any(char.isdigit() for char in password):
|
| 66 |
+
return "パスワードには少なくとも1つの数字を含める必要があります"
|
| 67 |
+
if not any(char.isupper() for char in password):
|
| 68 |
+
return "パスワードには少なくとも1つの大文字を含める必要があります"
|
| 69 |
+
return None
|
| 70 |
+
|
| 71 |
+
def validate_phone(phone):
|
| 72 |
+
if phone and not re.match(r"^\+?[0-9\s\-]+$", phone):
|
| 73 |
+
return "有効な電話番号を入力してください"
|
| 74 |
+
return None
|
| 75 |
+
|
| 76 |
+
def register_user(username, email, password, phone):
|
| 77 |
+
conn = sqlite3.connect('auth.db')
|
| 78 |
+
c = conn.cursor()
|
| 79 |
+
c.execute("SELECT * FROM users WHERE username = ? OR email = ?", (username, email))
|
| 80 |
+
if c.fetchone():
|
| 81 |
+
conn.close()
|
| 82 |
+
return False, "ユーザー名またはメールアドレスは既に存在します"
|
| 83 |
+
|
| 84 |
+
hashed_pw = hash_password(password)
|
| 85 |
+
c.execute(
|
| 86 |
+
"INSERT INTO users (username, email, password, phone) VALUES (?, ?, ?, ?)",
|
| 87 |
+
(username, email, hashed_pw, phone))
|
| 88 |
+
conn.commit()
|
| 89 |
+
conn.close()
|
| 90 |
+
return True, "登録が成功しました"
|
| 91 |
+
|
| 92 |
+
def login_user(username, password):
|
| 93 |
+
conn = sqlite3.connect('auth.db')
|
| 94 |
+
c = conn.cursor()
|
| 95 |
+
hashed_pw = hash_password(password)
|
| 96 |
+
|
| 97 |
+
c.execute("SELECT * FROM users WHERE username = ? AND password = ?", (username, hashed_pw))
|
| 98 |
+
user = c.fetchone()
|
| 99 |
+
conn.close()
|
| 100 |
+
|
| 101 |
+
if user:
|
| 102 |
+
return True, user
|
| 103 |
+
return False, "ユーザー名またはパスワードが無効です"
|
| 104 |
+
|
| 105 |
+
# Cohere and Gemini Helper Functions
|
| 106 |
+
def embed(input, input_type):
|
| 107 |
+
response = co.embed(texts=input, model='embed-multilingual-v3.0', input_type=input_type, embedding_types=['ubinary'])
|
| 108 |
+
return [np.unpackbits(np.array(embedding, dtype=np.uint8)) for embedding in response.embeddings.ubinary]
|
| 109 |
+
|
| 110 |
+
def summarize_text(long_text, username):
|
| 111 |
+
if not long_text:
|
| 112 |
+
return "要約するテキストがありません。"
|
| 113 |
+
|
| 114 |
+
prompt = f"次の文章をuserとassistantに分けて的確に要約してください:\n{long_text}"
|
| 115 |
+
gresponse = client.models.generate_content(
|
| 116 |
+
model="gemini-2.5-flash",
|
| 117 |
+
contents=[prompt]
|
| 118 |
+
)
|
| 119 |
+
summary = gresponse.text
|
| 120 |
+
return summary
|
| 121 |
+
|
| 122 |
+
def respond(ctype, msg, username):
|
| 123 |
+
conn = psycopg2.connect(
|
| 124 |
+
dbname="smair",
|
| 125 |
+
user="smairuser",
|
| 126 |
+
password="smairuser",
|
| 127 |
+
host="www.ryhintl.com",
|
| 128 |
+
port=10629
|
| 129 |
+
)
|
| 130 |
+
cur = conn.cursor()
|
| 131 |
+
|
| 132 |
+
query_embedding = embed([msg], 'search_query')[0].tolist()
|
| 133 |
+
|
| 134 |
+
cur.execute(
|
| 135 |
+
'SELECT content, 1 - (embedding <=> %s::vector) AS similarity FROM dailog_logs WHERE (1 - (embedding <=> %s::vector)) <> 0 ORDER BY similarity ASC',
|
| 136 |
+
(query_embedding, query_embedding)
|
| 137 |
+
)
|
| 138 |
+
proof = [row[0] for row in cur.fetchall()]
|
| 139 |
+
|
| 140 |
+
cur.execute(
|
| 141 |
+
'SELECT content, 1 - (embedding <=> %s::vector) AS similarity FROM monitoring_dialog WHERE (1 - (embedding <=> %s::vector)) <> 0 ORDER BY similarity ASC',
|
| 142 |
+
(query_embedding, query_embedding)
|
| 143 |
+
)
|
| 144 |
+
vector_resp = [row[0] for row in cur.fetchall()]
|
| 145 |
+
|
| 146 |
+
if ctype == "デフォルト":
|
| 147 |
+
message = f"{vector_resp}に基づいて{proof}を交えながら{msg}に対する答えを正確に出力してください。"
|
| 148 |
+
else:
|
| 149 |
+
message = f"{vector_resp}に基づいて{proof}を交えながら{msg}に対する答えを正確に関西弁で出力してください。"
|
| 150 |
+
|
| 151 |
+
messages = [
|
| 152 |
+
{"role": "system", "content": "あなたは、優秀なアシスタントです。"},
|
| 153 |
+
{"role": "user", "content": message},
|
| 154 |
+
]
|
| 155 |
+
|
| 156 |
+
response = co.chat(model="command-a-03-2025", messages=messages)
|
| 157 |
+
bot_message = response.message.content[0].text
|
| 158 |
+
|
| 159 |
+
cur.close()
|
| 160 |
+
conn.close()
|
| 161 |
+
return bot_message
|
| 162 |
+
|
| 163 |
+
# Gradio UI with Blocks
|
| 164 |
+
with gr.Blocks(title="Fund Manager Buddy", css="""footer {visibility: hidden;} #header {display: flex; justify-content: space-between; align-items: center; font-size: 24px; font-weight: bold;} #logo {width: 50px; height: 50px;}
|
| 165 |
+
.gradio-container {
|
| 166 |
+
background-color: #f8f9fa;
|
| 167 |
+
}
|
| 168 |
+
.main {
|
| 169 |
+
background-color: #f8f9fa;
|
| 170 |
+
}
|
| 171 |
+
.logo-container {
|
| 172 |
+
position: absolute;
|
| 173 |
+
top: 20px;
|
| 174 |
+
left: 20px;
|
| 175 |
+
z-index: 1000;
|
| 176 |
+
}
|
| 177 |
+
.logo-container img {
|
| 178 |
+
height: 50px;
|
| 179 |
+
width: auto;
|
| 180 |
+
}
|
| 181 |
+
.title {
|
| 182 |
+
font-size: 1.5rem;
|
| 183 |
+
font-weight: 700;
|
| 184 |
+
color: #2c3e50;
|
| 185 |
+
text-align: center;
|
| 186 |
+
margin-bottom: 1.5rem;
|
| 187 |
+
}
|
| 188 |
+
.subtitle {
|
| 189 |
+
font-size: 0.1rem;
|
| 190 |
+
color: #7f8c8d;
|
| 191 |
+
text-align: center;
|
| 192 |
+
margin-bottom: 2rem;
|
| 193 |
+
}
|
| 194 |
+
.card {
|
| 195 |
+
background: white;
|
| 196 |
+
border-radius: 15px;
|
| 197 |
+
padding: 2rem;
|
| 198 |
+
box-shadow: 0 10px 20px rgba(0,0,0,0.1);
|
| 199 |
+
margin-bottom: 1rem;
|
| 200 |
+
}
|
| 201 |
+
.success-message {
|
| 202 |
+
color: #27ae60;
|
| 203 |
+
text-align: center;
|
| 204 |
+
margin-top: 1rem;
|
| 205 |
+
}
|
| 206 |
+
.error-message {
|
| 207 |
+
color: #e74c3c;
|
| 208 |
+
text-align: center;
|
| 209 |
+
margin-top: 1rem;
|
| 210 |
+
}
|
| 211 |
+
.footer {
|
| 212 |
+
text-align: center;
|
| 213 |
+
margin-top: 2rem;
|
| 214 |
+
color: #95a5a6;
|
| 215 |
+
font-size: 0.5rem;
|
| 216 |
+
}
|
| 217 |
+
.avatar {
|
| 218 |
+
width: 100px;
|
| 219 |
+
height: 100px;
|
| 220 |
+
border-radius: 50%;
|
| 221 |
+
margin: 0 auto 1rem auto;
|
| 222 |
+
display: block;
|
| 223 |
+
object-fit: cover;
|
| 224 |
+
border: 3px solid #4a90e2;
|
| 225 |
+
}
|
| 226 |
+
.gr-button {
|
| 227 |
+
width: 100%;
|
| 228 |
+
border-radius: 10px;
|
| 229 |
+
padding: 10px;
|
| 230 |
+
background-color: #4a90e2;
|
| 231 |
+
color: white;
|
| 232 |
+
border: none;
|
| 233 |
+
font-weight: 500;
|
| 234 |
+
transition: all 0.3s;
|
| 235 |
+
}
|
| 236 |
+
.gr-button:hover {
|
| 237 |
+
background-color: #357abd;
|
| 238 |
+
color: lightyellow;
|
| 239 |
+
transform: translateY(-2px);
|
| 240 |
+
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
|
| 241 |
+
}
|
| 242 |
+
.gr-textinput, .gr-textbox {
|
| 243 |
+
border-radius: 10px;
|
| 244 |
+
padding: 10px;
|
| 245 |
+
border: 1px solid #ced4da;
|
| 246 |
+
}
|
| 247 |
+
""") as buddy:
|
| 248 |
+
|
| 249 |
+
gr.HTML('<div id="header"><span>🛡️ FUND MANAGER BUDDY</span><img id="logo" src="https://www.ryhintl.com/images/ryhlogo/ryhlogo.png" width="64" height="64" alt="Logo"></div>')
|
| 250 |
+
gr.Markdown("꧁ FM Buddy ꧂ ベクターDBに保存されている知識ベースのインベントリを使用してFMへの情報を共有します。")
|
| 251 |
+
|
| 252 |
+
# State variables
|
| 253 |
+
current_username = gr.State(None)
|
| 254 |
+
current_user_info = gr.State(None)
|
| 255 |
+
logged_in_state = gr.State(False)
|
| 256 |
+
|
| 257 |
+
with gr.Column(elem_classes="main"):
|
| 258 |
+
gr.HTML('<div class="logo-container"><img src="https://www.ryhintl.com/images/ryhlogo/ryhlogo.png" alt="Logo"></div>')
|
| 259 |
+
|
| 260 |
+
# Login/Register Section
|
| 261 |
+
with gr.Column(visible=True, elem_id="login_register_section") as login_register_section:
|
| 262 |
+
with gr.Tab("ログイン"):
|
| 263 |
+
gr.HTML('<h5 class="title">お帰りなさい!</h5>')
|
| 264 |
+
gr.HTML('<p style="text-align: center; margin-top: 1rem; font-size: 13px;">アカウントにアクセスするにはサインインしてください</p>')
|
| 265 |
+
|
| 266 |
+
with gr.Column(elem_classes="card"):
|
| 267 |
+
login_username = gr.Textbox(label="ユーザー名", placeholder="ユーザー名を入力してください")
|
| 268 |
+
login_password = gr.Textbox(label="パスワード", type="password", placeholder="パスワードを入力してください")
|
| 269 |
+
login_status = gr.Markdown("")
|
| 270 |
+
login_btn = gr.Button("サインイン")
|
| 271 |
+
|
| 272 |
+
with gr.Tab("アカウントを作成"):
|
| 273 |
+
gr.HTML('<h1 class="title">アカウントを作成</h1>')
|
| 274 |
+
gr.HTML('<p class="subtitle">今すぐ、参加して始めましょう!</p>')
|
| 275 |
+
|
| 276 |
+
with gr.Column(elem_classes="card"):
|
| 277 |
+
reg_username = gr.Textbox(label="ユーザー名", placeholder="ユーザー名を選択してください")
|
| 278 |
+
reg_email = gr.Textbox(label="電子メール", placeholder="メールアドレスを入力してください")
|
| 279 |
+
reg_phone = gr.Textbox(label="電話番号 (オプション)", placeholder="+81-1234567890")
|
| 280 |
+
reg_password = gr.Textbox(label="パスワード", type="password", placeholder="パスワードを入力してください")
|
| 281 |
+
reg_confirm_password = gr.Textbox(label="パスワード再確認", type="password", placeholder="パスワードを再度入力してください")
|
| 282 |
+
register_status = gr.Markdown("")
|
| 283 |
+
register_btn = gr.Button("登録")
|
| 284 |
+
|
| 285 |
+
# Dashboard Section
|
| 286 |
+
with gr.Column(visible=False, elem_id="dashboard_section") as dashboard_section:
|
| 287 |
+
welcome_message = gr.Markdown()
|
| 288 |
+
gr.HTML('<p style="text-align: center; margin-top: 1rem; font-size: 10px;"">あなたは現在、アカウントにログインしています</p>')
|
| 289 |
+
|
| 290 |
+
user_avatar = gr.HTML()
|
| 291 |
+
|
| 292 |
+
with gr.Row():
|
| 293 |
+
with gr.Column(scale=0.1):
|
| 294 |
+
gr.Markdown("### QUERY EXAMPLES")
|
| 295 |
+
gr.Markdown("""
|
| 296 |
+
- **トヨタ自動車の株を買いたいと思ってるんだけど、どう思いますか?**
|
| 297 |
+
- **トヨタの株、買おかな思てんねんけど、どう思う?それと、買うときに参考にしたらええ判断材料とか基準、教えてくれへん?**
|
| 298 |
+
- **トヨタ自動車の株価の上昇余地はどれくらいだと思いますか?現在の株価は¥2,508で、証券街のEPS予測(¥241.37)に対してPERは20倍です。**
|
| 299 |
+
- **トヨタ自動車の過去の予測履歴があれば教えてください。**
|
| 300 |
+
- **トヨタ自動車のハイブリッド自動車の販売が予想以上に好調でコスト増加が抑えられていると思われるが、過去のコストや利益率の推移を確認できますか?**
|
| 301 |
+
""")
|
| 302 |
+
with gr.Column(scale=3):
|
| 303 |
+
with gr.Tabs():
|
| 304 |
+
with gr.TabItem("ダイアログ"):
|
| 305 |
+
dialog_ctype = gr.Dropdown(["デフォルト", "関西弁"], label="会話形式", value="デフォルト")
|
| 306 |
+
dialog_query = gr.Textbox(label="クエリーを入力してください。", lines=5, value="トヨタ自動車の株を買いたいと思ってるんだけど、どう思いますか? 尚、購入のための参考にすべき判断材料や基準を教えてください")
|
| 307 |
+
dialog_output = gr.Chatbot(label="対話履歴", bubble_full_width=False, height=400)
|
| 308 |
+
dialog_submit_btn = gr.Button("生成")
|
| 309 |
+
|
| 310 |
+
with gr.TabItem("ダイアログ・サマリー"):
|
| 311 |
+
summary_input = gr.Textbox(label="要約したいユーザー・ダイアログを入力", lines=5)
|
| 312 |
+
summary_output = gr.Markdown(label="要約結果")
|
| 313 |
+
summary_submit_btn = gr.Button("要約")
|
| 314 |
+
|
| 315 |
+
logout_btn = gr.Button("ログアウト")
|
| 316 |
+
|
| 317 |
+
gr.HTML('<p class="footer">© 2025 Fund Manager Buddy. All rights reserved.</p>')
|
| 318 |
+
|
| 319 |
+
# Gradio Event Handlers
|
| 320 |
+
|
| 321 |
+
# Login Logic
|
| 322 |
+
def process_login(username, password, current_user_info_state):
|
| 323 |
+
username_error = validate_username(username)
|
| 324 |
+
password_error = validate_password(password)
|
| 325 |
+
|
| 326 |
+
if username_error:
|
| 327 |
+
return gr.update(value=f"<p class='error-message'>{username_error}</p>"), False, None, None, gr.update(visible=True), gr.update(visible=False)
|
| 328 |
+
elif password_error:
|
| 329 |
+
return gr.update(value=f"<p class='error-message'>{password_error}</p>"), False, None, None, gr.update(visible=True), gr.update(visible=False)
|
| 330 |
+
else:
|
| 331 |
+
success, result = login_user(username, password)
|
| 332 |
+
if success:
|
| 333 |
+
user_info = {
|
| 334 |
+
"id": result[0],
|
| 335 |
+
"username": result[1],
|
| 336 |
+
"email": result[2],
|
| 337 |
+
"phone": result[4]
|
| 338 |
+
}
|
| 339 |
+
return gr.update(value="<p class='success-message'></p>"), True, username, user_info, gr.update(visible=False), gr.update(visible=True)
|
| 340 |
+
else:
|
| 341 |
+
return gr.update(value=f"<p class='error-message'>{result}</p>"), False, None, None, gr.update(visible=True), gr.update(visible=False)
|
| 342 |
+
|
| 343 |
+
login_btn.click(
|
| 344 |
+
process_login,
|
| 345 |
+
inputs=[login_username, login_password, current_user_info],
|
| 346 |
+
outputs=[login_status, logged_in_state, current_username, current_user_info, login_register_section, dashboard_section]
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
# Register Logic
|
| 350 |
+
def process_register(username, email, phone, password, confirm_password):
|
| 351 |
+
errors = []
|
| 352 |
+
username_error = validate_username(username)
|
| 353 |
+
email_error = validate_email(email)
|
| 354 |
+
password_error = validate_password(password)
|
| 355 |
+
phone_error = validate_phone(phone)
|
| 356 |
+
|
| 357 |
+
if username_error:
|
| 358 |
+
errors.append(username_error)
|
| 359 |
+
if email_error:
|
| 360 |
+
errors.append(email_error)
|
| 361 |
+
if password_error:
|
| 362 |
+
errors.append(password_error)
|
| 363 |
+
if phone_error:
|
| 364 |
+
errors.append(phone_error)
|
| 365 |
+
if password != confirm_password:
|
| 366 |
+
errors.append("パスワードが一致しません。")
|
| 367 |
+
|
| 368 |
+
if errors:
|
| 369 |
+
error_html = "".join([f"<p class='error-message'>{e}</p>" for e in errors])
|
| 370 |
+
return gr.update(value=error_html), gr.update(visible=True), gr.update(visible=False)
|
| 371 |
+
else:
|
| 372 |
+
success, message = register_user(username, email, password, phone)
|
| 373 |
+
if success:
|
| 374 |
+
return gr.update(value=f"<p class='success-message'>{message}</p>"), gr.update(visible=True), gr.update(visible=False)
|
| 375 |
+
else:
|
| 376 |
+
return gr.update(value=f"<p class='error-message'>{message}</p>"), gr.update(visible=True), gr.update(visible=False)
|
| 377 |
+
|
| 378 |
+
register_btn.click(
|
| 379 |
+
process_register,
|
| 380 |
+
inputs=[reg_username, reg_email, reg_phone, reg_password, reg_confirm_password],
|
| 381 |
+
outputs=[register_status, login_register_section, dashboard_section]
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
|
| 385 |
+
# Update Dashboard on Login Success
|
| 386 |
+
def update_dashboard_ui(is_logged_in, username, user_info):
|
| 387 |
+
if is_logged_in:
|
| 388 |
+
welcome_msg = f'<h3 class="title">ようこそ! {username} 様!</h3>'
|
| 389 |
+
avatar_html = f'<img src="https://ui-avatars.com/api/?name=' + username + '&background=4a90e2&color=fff&size=200" class="avatar">'
|
| 390 |
+
return welcome_msg, avatar_html, gr.update(visible=False), gr.update(visible=True)
|
| 391 |
+
return "", "", gr.update(visible=True), gr.update(visible=False)
|
| 392 |
+
|
| 393 |
+
logged_in_state.change(
|
| 394 |
+
update_dashboard_ui,
|
| 395 |
+
inputs=[logged_in_state, current_username, current_user_info],
|
| 396 |
+
outputs=[welcome_message, user_avatar, login_register_section, dashboard_section]
|
| 397 |
+
)
|
| 398 |
+
|
| 399 |
+
# Logout Logic
|
| 400 |
+
def process_logout():
|
| 401 |
+
time.sleep(1)
|
| 402 |
+
return False, None, None, gr.update(visible=True), gr.update(visible=False)
|
| 403 |
+
|
| 404 |
+
logout_btn.click(
|
| 405 |
+
process_logout,
|
| 406 |
+
inputs=[],
|
| 407 |
+
outputs=[logged_in_state, current_username, current_user_info, login_register_section, dashboard_section]
|
| 408 |
+
)
|
| 409 |
+
|
| 410 |
+
# Chat Response Logic
|
| 411 |
+
def generate_response(ctype, msg, chat_history, username):
|
| 412 |
+
if not username: # Should not happen if UI is correctly managed
|
| 413 |
+
return chat_history, gr.update(value="ログインしてください。")
|
| 414 |
+
|
| 415 |
+
bot_message = respond(ctype, msg, username)
|
| 416 |
+
chat_history.append([msg, bot_message])
|
| 417 |
+
return chat_history, "" # Clear the input after sending
|
| 418 |
+
|
| 419 |
+
dialog_submit_btn.click(
|
| 420 |
+
generate_response,
|
| 421 |
+
inputs=[dialog_ctype, dialog_query, dialog_output, current_username],
|
| 422 |
+
outputs=[dialog_output, dialog_query]
|
| 423 |
+
)
|
| 424 |
+
|
| 425 |
+
# Summarize Logic
|
| 426 |
+
def generate_summary(input_text, username):
|
| 427 |
+
if not username:
|
| 428 |
+
return "ログインしてください。"
|
| 429 |
+
summary = summarize_text(input_text, username)
|
| 430 |
+
return summary
|
| 431 |
+
|
| 432 |
+
summary_submit_btn.click(
|
| 433 |
+
generate_summary,
|
| 434 |
+
inputs=[summary_input, current_username],
|
| 435 |
+
outputs=summary_output
|
| 436 |
+
)
|
| 437 |
+
|
| 438 |
+
buddy.launch(favicon_path="favicon.ico", show_api=False)
|