Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,21 +16,53 @@ except:
|
|
| 16 |
|
| 17 |
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
|
| 18 |
|
| 19 |
-
# 2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
custom_css = """
|
| 21 |
-
/* Ẩn triệt để
|
| 22 |
-
footer
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
/* Sidebar
|
| 27 |
.sidebar-node {
|
| 28 |
background: rgba(15, 23, 42, 0.8) !important;
|
| 29 |
border-right: 1px solid #38bdf833;
|
| 30 |
padding: 20px;
|
| 31 |
}
|
| 32 |
|
| 33 |
-
/* Logo phong cách Sci-fi */
|
| 34 |
.logo-area {
|
| 35 |
font-family: 'Orbitron', sans-serif;
|
| 36 |
background: linear-gradient(90deg, #38bdf8, #818cf8);
|
|
@@ -42,37 +74,18 @@ footer { display: none !important; }
|
|
| 42 |
margin-bottom: 20px;
|
| 43 |
}
|
| 44 |
|
| 45 |
-
/* Dòng cảnh báo tùy chỉnh của Master */
|
| 46 |
.custom-warning {
|
| 47 |
text-align: center;
|
| 48 |
-
color: #
|
| 49 |
font-size: 12px;
|
| 50 |
-
margin-top:
|
| 51 |
-
margin-bottom:
|
| 52 |
font-style: italic;
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
/* Ẩn thanh header của Hugging Face khi nhúng */
|
| 56 |
-
#header {
|
| 57 |
-
display: none !important;
|
| 58 |
-
}
|
| 59 |
-
|
| 60 |
-
/* Ép ứng dụng tràn viền thực sự */
|
| 61 |
-
.gradio-container {
|
| 62 |
-
max-width: 100% !important;
|
| 63 |
-
padding: 0 !important;
|
| 64 |
-
margin: 0 !important;
|
| 65 |
-
}
|
| 66 |
-
|
| 67 |
-
footer, .built-with, .gr-pro, .footer {
|
| 68 |
-
display: none !important;
|
| 69 |
-
visibility: hidden !important;
|
| 70 |
-
height: 0 !important;
|
| 71 |
-
padding: 0 !important;
|
| 72 |
}
|
| 73 |
"""
|
| 74 |
|
| 75 |
-
#
|
| 76 |
def get_db_history(profile: gr.OAuthProfile | None):
|
| 77 |
if not supabase or not profile: return []
|
| 78 |
try:
|
|
@@ -85,7 +98,6 @@ def load_selected_chat(evt: gr.SelectData, db_data):
|
|
| 85 |
selected_item = db_data[evt.index]
|
| 86 |
return selected_item['history'], selected_item['session_id']
|
| 87 |
|
| 88 |
-
# 4. ENGINE XỬ LÝ AI
|
| 89 |
def chat_engine(message, history, session_id, profile: gr.OAuthProfile | None):
|
| 90 |
text = message.get("text", "").strip()
|
| 91 |
if not text:
|
|
@@ -93,7 +105,7 @@ def chat_engine(message, history, session_id, profile: gr.OAuthProfile | None):
|
|
| 93 |
return
|
| 94 |
|
| 95 |
user_name = profile.username if profile else "Guest"
|
| 96 |
-
messages = [{"role": "system", "content": f"Bạn là NodeAI Genesis. Master: {user_name}.
|
| 97 |
|
| 98 |
for msg in history:
|
| 99 |
messages.append({"role": msg["role"], "content": msg["content"]})
|
|
@@ -119,15 +131,15 @@ def chat_engine(message, history, session_id, profile: gr.OAuthProfile | None):
|
|
| 119 |
}
|
| 120 |
supabase.table("chats").upsert(data, on_conflict="session_id").execute()
|
| 121 |
except Exception as e:
|
| 122 |
-
yield gr.update(), new_history + [{"role": "assistant", "content": f"Lỗi
|
| 123 |
|
| 124 |
-
# 5. XÂY DỰNG
|
| 125 |
-
with gr.Blocks() as demo:
|
| 126 |
session_id = gr.State(str(uuid.uuid4()))
|
| 127 |
db_state = gr.State([])
|
| 128 |
|
| 129 |
with gr.Row():
|
| 130 |
-
# Sidebar
|
| 131 |
with gr.Column(scale=1, elem_classes="sidebar-node"):
|
| 132 |
gr.HTML("<div class='logo-area'>NODEAI GENESIS</div>")
|
| 133 |
gr.LoginButton(value="Đăng nhập Hugging Face", size="sm")
|
|
@@ -141,9 +153,9 @@ with gr.Blocks() as demo:
|
|
| 141 |
type="index"
|
| 142 |
)
|
| 143 |
|
| 144 |
-
#
|
| 145 |
with gr.Column(scale=4):
|
| 146 |
-
chatbot = gr.Chatbot(show_label=False, height="
|
| 147 |
|
| 148 |
# Dòng cảnh báo Master yêu cầu
|
| 149 |
gr.HTML("<div class='custom-warning'>"
|
|
@@ -152,24 +164,21 @@ with gr.Blocks() as demo:
|
|
| 152 |
|
| 153 |
msg_input = gr.MultimodalTextbox(
|
| 154 |
show_label=False,
|
| 155 |
-
placeholder="
|
| 156 |
container=False
|
| 157 |
)
|
| 158 |
|
| 159 |
-
# 6.
|
| 160 |
def refresh_data(profile: gr.OAuthProfile | None):
|
| 161 |
if not profile: return [], gr.Dataset(samples=[])
|
| 162 |
data = get_db_history(profile)
|
| 163 |
samples = [[item['title']] for item in data]
|
| 164 |
return data, gr.Dataset(samples=samples)
|
| 165 |
|
| 166 |
-
# Load dữ liệu khi mở trang
|
| 167 |
demo.load(refresh_data, None, [db_state, history_list])
|
| 168 |
|
| 169 |
-
# Chọn chat cũ
|
| 170 |
history_list.select(fn=load_selected_chat, inputs=[db_state], outputs=[chatbot, session_id])
|
| 171 |
|
| 172 |
-
# Gửi tin nhắn & Tải lại sidebar
|
| 173 |
msg_input.submit(
|
| 174 |
chat_engine,
|
| 175 |
[msg_input, chatbot, session_id],
|
|
@@ -178,8 +187,7 @@ with gr.Blocks() as demo:
|
|
| 178 |
refresh_data, None, [db_state, history_list]
|
| 179 |
)
|
| 180 |
|
| 181 |
-
# Nút chat mới
|
| 182 |
new_chat_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
|
| 183 |
|
| 184 |
if __name__ == "__main__":
|
| 185 |
-
demo.launch(css=custom_css)
|
|
|
|
| 16 |
|
| 17 |
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
|
| 18 |
|
| 19 |
+
# 2. JAVASCRIPT "DIỆT" TẬN GỐC (Xóa thành phần ngay khi nó vừa xuất hiện)
|
| 20 |
+
head_js = """
|
| 21 |
+
<script>
|
| 22 |
+
const observer = new MutationObserver(() => {
|
| 23 |
+
// Danh sách các thành phần cần xóa bỏ
|
| 24 |
+
const targets = [
|
| 25 |
+
'footer',
|
| 26 |
+
'.built-with',
|
| 27 |
+
'header',
|
| 28 |
+
'#header',
|
| 29 |
+
'.share-button',
|
| 30 |
+
'.gr-pro',
|
| 31 |
+
'#component-10' // ID thường thấy của footer trong Gradio 6
|
| 32 |
+
];
|
| 33 |
+
|
| 34 |
+
targets.forEach(selector => {
|
| 35 |
+
const el = document.querySelector(selector);
|
| 36 |
+
if (el) el.remove();
|
| 37 |
+
});
|
| 38 |
+
});
|
| 39 |
+
|
| 40 |
+
observer.observe(document.body, { childList: true, subtree: true });
|
| 41 |
+
</script>
|
| 42 |
+
"""
|
| 43 |
+
|
| 44 |
+
# 3. GIAO DIỆN CSS (Ẩn trước khi JS kịp xóa)
|
| 45 |
custom_css = """
|
| 46 |
+
/* Ẩn triệt để footer và header */
|
| 47 |
+
footer, .built-with, header, #header, .share-button {
|
| 48 |
+
display: none !important;
|
| 49 |
+
visibility: hidden !important;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
/* Cấu hình tràn viền hoàn toàn */
|
| 53 |
+
.gradio-container {
|
| 54 |
+
max-width: 100% !important;
|
| 55 |
+
padding: 0 !important;
|
| 56 |
+
background: radial-gradient(circle at 50% 0%, #0f172a 0%, #020617 100%) !important;
|
| 57 |
+
}
|
| 58 |
|
| 59 |
+
/* Sidebar & UI phong cách NodeAI */
|
| 60 |
.sidebar-node {
|
| 61 |
background: rgba(15, 23, 42, 0.8) !important;
|
| 62 |
border-right: 1px solid #38bdf833;
|
| 63 |
padding: 20px;
|
| 64 |
}
|
| 65 |
|
|
|
|
| 66 |
.logo-area {
|
| 67 |
font-family: 'Orbitron', sans-serif;
|
| 68 |
background: linear-gradient(90deg, #38bdf8, #818cf8);
|
|
|
|
| 74 |
margin-bottom: 20px;
|
| 75 |
}
|
| 76 |
|
|
|
|
| 77 |
.custom-warning {
|
| 78 |
text-align: center;
|
| 79 |
+
color: #94a3b8;
|
| 80 |
font-size: 12px;
|
| 81 |
+
margin-top: 8px;
|
| 82 |
+
margin-bottom: 8px;
|
| 83 |
font-style: italic;
|
| 84 |
+
font-family: sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
}
|
| 86 |
"""
|
| 87 |
|
| 88 |
+
# 4. LOGIC XỬ LÝ DỮ LIỆU
|
| 89 |
def get_db_history(profile: gr.OAuthProfile | None):
|
| 90 |
if not supabase or not profile: return []
|
| 91 |
try:
|
|
|
|
| 98 |
selected_item = db_data[evt.index]
|
| 99 |
return selected_item['history'], selected_item['session_id']
|
| 100 |
|
|
|
|
| 101 |
def chat_engine(message, history, session_id, profile: gr.OAuthProfile | None):
|
| 102 |
text = message.get("text", "").strip()
|
| 103 |
if not text:
|
|
|
|
| 105 |
return
|
| 106 |
|
| 107 |
user_name = profile.username if profile else "Guest"
|
| 108 |
+
messages = [{"role": "system", "content": f"Bạn là NodeAI Genesis. Master: {user_name}. Phản hồi bằng tiếng Việt."}]
|
| 109 |
|
| 110 |
for msg in history:
|
| 111 |
messages.append({"role": msg["role"], "content": msg["content"]})
|
|
|
|
| 131 |
}
|
| 132 |
supabase.table("chats").upsert(data, on_conflict="session_id").execute()
|
| 133 |
except Exception as e:
|
| 134 |
+
yield gr.update(), new_history + [{"role": "assistant", "content": f"Lỗi: {str(e)}"}], session_id
|
| 135 |
|
| 136 |
+
# 5. XÂY DỰNG UI VỚI HEAD SCRIPT
|
| 137 |
+
with gr.Blocks(head=head_js) as demo:
|
| 138 |
session_id = gr.State(str(uuid.uuid4()))
|
| 139 |
db_state = gr.State([])
|
| 140 |
|
| 141 |
with gr.Row():
|
| 142 |
+
# Sidebar
|
| 143 |
with gr.Column(scale=1, elem_classes="sidebar-node"):
|
| 144 |
gr.HTML("<div class='logo-area'>NODEAI GENESIS</div>")
|
| 145 |
gr.LoginButton(value="Đăng nhập Hugging Face", size="sm")
|
|
|
|
| 153 |
type="index"
|
| 154 |
)
|
| 155 |
|
| 156 |
+
# Main Chat Area
|
| 157 |
with gr.Column(scale=4):
|
| 158 |
+
chatbot = gr.Chatbot(show_label=False, height="76vh")
|
| 159 |
|
| 160 |
# Dòng cảnh báo Master yêu cầu
|
| 161 |
gr.HTML("<div class='custom-warning'>"
|
|
|
|
| 164 |
|
| 165 |
msg_input = gr.MultimodalTextbox(
|
| 166 |
show_label=False,
|
| 167 |
+
placeholder="Hỏi NodeAI hoặc dán code Roblox...",
|
| 168 |
container=False
|
| 169 |
)
|
| 170 |
|
| 171 |
+
# 6. ĐIỀU KHIỂN SỰ KIỆN
|
| 172 |
def refresh_data(profile: gr.OAuthProfile | None):
|
| 173 |
if not profile: return [], gr.Dataset(samples=[])
|
| 174 |
data = get_db_history(profile)
|
| 175 |
samples = [[item['title']] for item in data]
|
| 176 |
return data, gr.Dataset(samples=samples)
|
| 177 |
|
|
|
|
| 178 |
demo.load(refresh_data, None, [db_state, history_list])
|
| 179 |
|
|
|
|
| 180 |
history_list.select(fn=load_selected_chat, inputs=[db_state], outputs=[chatbot, session_id])
|
| 181 |
|
|
|
|
| 182 |
msg_input.submit(
|
| 183 |
chat_engine,
|
| 184 |
[msg_input, chatbot, session_id],
|
|
|
|
| 187 |
refresh_data, None, [db_state, history_list]
|
| 188 |
)
|
| 189 |
|
|
|
|
| 190 |
new_chat_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
|
| 191 |
|
| 192 |
if __name__ == "__main__":
|
| 193 |
+
demo.queue().launch(css=custom_css)
|