lamgamepro1 commited on
Commit
f642f20
·
verified ·
1 Parent(s): 0ca5b9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -40
app.py CHANGED
@@ -4,7 +4,7 @@ import os
4
  import uuid
5
  import json
6
 
7
- # 1. HỆ THỐNG & MODEL
8
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
9
  client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
10
  DB_FILE = "nodeai_user_vault.json"
@@ -20,43 +20,43 @@ def save_vault(vault_data):
20
  with open(DB_FILE, "w", encoding="utf-8") as f:
21
  json.dump(vault_data, f, ensure_ascii=False, indent=4)
22
 
23
- # 2. GIAO DIỆN SIÊU CẤP (GLASSMORPHISM)
24
  custom_css = """
25
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@800&family=Plus+Jakarta+Sans:wght@400;600&display=swap');
26
 
27
  .gradio-container {
28
- background: radial-gradient(circle at 50% 0%, #10101a 0%, #050505 100%) !important;
29
  font-family: 'Plus Jakarta Sans', sans-serif !important;
30
  }
31
 
32
- /* Sidebar */
33
  .sidebar-node {
34
  position: fixed !important;
35
  left: 0 !important; top: 0 !important;
36
  width: 280px !important; height: 100vh !important;
37
- background: rgba(10, 10, 15, 0.7) !important;
38
- backdrop-filter: blur(25px) !important;
39
- border-right: 1px solid rgba(255, 255, 255, 0.05) !important;
40
  z-index: 1000 !important;
41
  padding: 25px !important;
42
  }
43
 
44
  .logo-text {
45
  font-family: 'Orbitron', sans-serif !important;
46
- background: linear-gradient(90deg, #00f2fe, #7ef2ff);
47
  -webkit-background-clip: text;
48
  -webkit-text-fill-color: transparent;
49
- font-size: 24px; text-align: center; margin-bottom: 25px;
50
  }
51
 
52
  .orange-btn {
53
- background: linear-gradient(135deg, #ff6b6b 0%, #ff8e53 100%) !important;
54
  border: none !important; color: white !important;
55
  font-weight: 600 !important; border-radius: 12px !important;
56
- box-shadow: 0 4px 15px rgba(255, 107, 107, 0.3) !important;
57
  }
58
 
59
- /* Chat Area */
60
  .chat-main {
61
  margin-left: 280px !important;
62
  width: calc(100% - 280px) !important;
@@ -67,21 +67,21 @@ custom_css = """
67
 
68
  .gr-chatbot {
69
  background: transparent !important; border: none !important;
70
- height: 78vh !important; margin-bottom: 110px !important;
71
  }
72
 
73
- /* Input Area Fixed */
74
  .input-fixed-bottom {
75
  position: fixed !important;
76
- bottom: 30px !important;
77
  left: 50% !important;
78
  transform: translateX(calc(-50% + 140px)) !important;
79
- width: 90% !important;
80
  max-width: 800px !important;
81
- background: rgba(25, 25, 35, 0.9) !important;
82
- backdrop-filter: blur(15px) !important;
83
- border: 1px solid rgba(0, 242, 254, 0.3) !important;
84
- border-radius: 22px !important;
85
  padding: 5px 15px !important;
86
  z-index: 2000 !important;
87
  box-sizing: border-box !important;
@@ -92,7 +92,7 @@ footer { display: none !important; }
92
  @media screen and (max-width: 768px) {
93
  .sidebar-node { display: none !important; }
94
  .chat-main { margin-left: 0 !important; width: 100% !important; }
95
- .input-fixed-bottom { transform: translateX(-50%) !important; width: 95% !important; }
96
  }
97
  """
98
 
@@ -109,7 +109,7 @@ def chat_fn(message, history, session_id, profile: gr.OAuthProfile | None):
109
  if username not in vault: vault[username] = {}
110
 
111
  if session_id not in vault[username]:
112
- vault[username][session_id] = {"title": user_text[:25], "history": []}
113
 
114
  api_messages = [{"role": "system", "content": f"Master: {username}. Bạn là NodeAI Genesis."}]
115
  for msg in history: api_messages.append({"role": msg["role"], "content": msg["content"]})
@@ -126,35 +126,25 @@ def chat_fn(message, history, session_id, profile: gr.OAuthProfile | None):
126
  final_h = new_h + [{"role": "assistant", "content": ans}]
127
  vault[username][session_id]["history"] = final_h
128
  save_vault(vault)
129
- # Trả về ans cuối cùng để đưa vào TTS
130
  yield {"text": ""}, final_h, session_id, ans
131
  except Exception as e:
132
  yield {"text": ""}, history + [{"role": "assistant", "content": str(e)}], session_id, ""
133
 
134
- def tts_engine(text):
135
- if not text or len(text) < 2: return None
136
- try:
137
- # Sử dụng API TTS của Google trực tiếp để tránh lỗi file
138
- return f"https://translate.google.com/translate_tts?ie=UTF-8&tl=vi&client=tw-ob&q={text[:200]}"
139
- except: return None
140
-
141
  # 4. GIAO DIỆN
142
- with gr.Blocks(title="NodeAI Genesis v32.6") as demo:
143
  session_id = gr.State(str(uuid.uuid4()))
144
  tts_buffer = gr.Textbox(visible=False)
145
- # State để kích hoạt render lại lịch sử
146
  vault_state = gr.State(load_vault())
147
 
148
  with gr.Row():
149
  # SIDEBAR
150
  with gr.Column(elem_classes="sidebar-node"):
151
  gr.HTML("<div class='logo-text'>NODEAI GENESIS</div>")
152
- gr.LoginButton(value="Đăng nhập")
153
  new_btn = gr.Button("+ NEW WORKSPACE", elem_classes="orange-btn")
154
 
155
- gr.Markdown("### 🕒 Lịch sử trò chuyện")
156
 
157
- # SỬA LỖI RENDER TẠI ĐÂY
158
  @gr.render(inputs=[vault_state])
159
  def render_history(current_vault, profile: gr.OAuthProfile | None):
160
  username = get_username(profile)
@@ -167,12 +157,15 @@ with gr.Blocks(title="NodeAI Genesis v32.6") as demo:
167
 
168
  # MAIN
169
  with gr.Column(elem_classes="chat-main"):
170
- chatbot = gr.Chatbot(show_label=False, type="messages")
 
171
  audio_out = gr.Audio(autoplay=True, visible=False)
172
 
173
  with gr.Row(elem_classes="input-fixed-bottom"):
174
  msg_input = gr.MultimodalTextbox(
175
- show_label=False, placeholder="Hỏi Master Phúc...", container=False
 
 
176
  )
177
 
178
  # EVENT HANDLERS
@@ -181,13 +174,13 @@ with gr.Blocks(title="NodeAI Genesis v32.6") as demo:
181
  msg_input.submit(
182
  chat_fn, [msg_input, chatbot, session_id], [msg_input, chatbot, session_id, tts_buffer]
183
  ).then(
184
- update_vault, None, vault_state # Cập nhật lại state để sidebar render lại
185
  ).then(
186
- tts_engine, tts_buffer, audio_out
 
187
  )
188
 
189
  new_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
190
 
191
  if __name__ == "__main__":
192
- # CHUYỂN CSS VÀO ĐÂY ĐỂ FIX WARNING
193
  demo.launch(css=custom_css)
 
4
  import uuid
5
  import json
6
 
7
+ # 1. CẤU HÌNH HỆ THỐNG
8
  HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
9
  client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
10
  DB_FILE = "nodeai_user_vault.json"
 
20
  with open(DB_FILE, "w", encoding="utf-8") as f:
21
  json.dump(vault_data, f, ensure_ascii=False, indent=4)
22
 
23
+ # 2. GIAO DIỆN SIÊU CẤP (GLASSMORPHISM DIAMOND)
24
  custom_css = """
25
  @import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@800&family=Plus+Jakarta+Sans:wght@400;600&display=swap');
26
 
27
  .gradio-container {
28
+ background: radial-gradient(circle at 50% 0%, #0f172a 0%, #020617 100%) !important;
29
  font-family: 'Plus Jakarta Sans', sans-serif !important;
30
  }
31
 
32
+ /* Sidebar Kính Mờ */
33
  .sidebar-node {
34
  position: fixed !important;
35
  left: 0 !important; top: 0 !important;
36
  width: 280px !important; height: 100vh !important;
37
+ background: rgba(15, 23, 42, 0.6) !important;
38
+ backdrop-filter: blur(20px) !important;
39
+ border-right: 1px solid rgba(255, 255, 255, 0.1) !important;
40
  z-index: 1000 !important;
41
  padding: 25px !important;
42
  }
43
 
44
  .logo-text {
45
  font-family: 'Orbitron', sans-serif !important;
46
+ background: linear-gradient(90deg, #38bdf8, #818cf8);
47
  -webkit-background-clip: text;
48
  -webkit-text-fill-color: transparent;
49
+ font-size: 22px; text-align: center; margin-bottom: 25px;
50
  }
51
 
52
  .orange-btn {
53
+ background: linear-gradient(135deg, #f97316 0%, #fb923c 100%) !important;
54
  border: none !important; color: white !important;
55
  font-weight: 600 !important; border-radius: 12px !important;
56
+ box-shadow: 0 4px 15px rgba(249, 115, 22, 0.3) !important;
57
  }
58
 
59
+ /* Chat Main Area */
60
  .chat-main {
61
  margin-left: 280px !important;
62
  width: calc(100% - 280px) !important;
 
67
 
68
  .gr-chatbot {
69
  background: transparent !important; border: none !important;
70
+ height: 75vh !important; margin-bottom: 110px !important;
71
  }
72
 
73
+ /* Input Area Fixed - Không bao giờ tràn */
74
  .input-fixed-bottom {
75
  position: fixed !important;
76
+ bottom: 25px !important;
77
  left: 50% !important;
78
  transform: translateX(calc(-50% + 140px)) !important;
79
+ width: calc(100% - 340px) !important;
80
  max-width: 800px !important;
81
+ background: rgba(30, 41, 59, 0.8) !important;
82
+ backdrop-filter: blur(10px) !important;
83
+ border: 1px solid rgba(56, 189, 248, 0.3) !important;
84
+ border-radius: 20px !important;
85
  padding: 5px 15px !important;
86
  z-index: 2000 !important;
87
  box-sizing: border-box !important;
 
92
  @media screen and (max-width: 768px) {
93
  .sidebar-node { display: none !important; }
94
  .chat-main { margin-left: 0 !important; width: 100% !important; }
95
+ .input-fixed-bottom { transform: translateX(-50%) !important; width: 92% !important; }
96
  }
97
  """
98
 
 
109
  if username not in vault: vault[username] = {}
110
 
111
  if session_id not in vault[username]:
112
+ vault[username][session_id] = {"title": user_text[:20] + "...", "history": []}
113
 
114
  api_messages = [{"role": "system", "content": f"Master: {username}. Bạn là NodeAI Genesis."}]
115
  for msg in history: api_messages.append({"role": msg["role"], "content": msg["content"]})
 
126
  final_h = new_h + [{"role": "assistant", "content": ans}]
127
  vault[username][session_id]["history"] = final_h
128
  save_vault(vault)
 
129
  yield {"text": ""}, final_h, session_id, ans
130
  except Exception as e:
131
  yield {"text": ""}, history + [{"role": "assistant", "content": str(e)}], session_id, ""
132
 
 
 
 
 
 
 
 
133
  # 4. GIAO DIỆN
134
+ with gr.Blocks(title="NodeAI Genesis v32.7") as demo:
135
  session_id = gr.State(str(uuid.uuid4()))
136
  tts_buffer = gr.Textbox(visible=False)
 
137
  vault_state = gr.State(load_vault())
138
 
139
  with gr.Row():
140
  # SIDEBAR
141
  with gr.Column(elem_classes="sidebar-node"):
142
  gr.HTML("<div class='logo-text'>NODEAI GENESIS</div>")
143
+ gr.LoginButton(value="Xác thực Nick")
144
  new_btn = gr.Button("+ NEW WORKSPACE", elem_classes="orange-btn")
145
 
146
+ gr.Markdown("### 🕒 Lịch sử Chat")
147
 
 
148
  @gr.render(inputs=[vault_state])
149
  def render_history(current_vault, profile: gr.OAuthProfile | None):
150
  username = get_username(profile)
 
157
 
158
  # MAIN
159
  with gr.Column(elem_classes="chat-main"):
160
+ # Đã xóa type="messages" để fix lỗi runtime
161
+ chatbot = gr.Chatbot(show_label=False)
162
  audio_out = gr.Audio(autoplay=True, visible=False)
163
 
164
  with gr.Row(elem_classes="input-fixed-bottom"):
165
  msg_input = gr.MultimodalTextbox(
166
+ show_label=False,
167
+ placeholder="Hỏi Master Phúc bất cứ điều gì...",
168
+ container=False
169
  )
170
 
171
  # EVENT HANDLERS
 
174
  msg_input.submit(
175
  chat_fn, [msg_input, chatbot, session_id], [msg_input, chatbot, session_id, tts_buffer]
176
  ).then(
177
+ update_vault, None, vault_state
178
  ).then(
179
+ lambda x: f"https://translate.google.com/translate_tts?ie=UTF-8&tl=vi&client=tw-ob&q={x[:200]}" if x else None,
180
+ tts_buffer, audio_out
181
  )
182
 
183
  new_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
184
 
185
  if __name__ == "__main__":
 
186
  demo.launch(css=custom_css)