Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import os
|
|
| 4 |
import uuid
|
| 5 |
import json
|
| 6 |
|
| 7 |
-
# Import
|
| 8 |
try: from duckduckgo_search import DDGS
|
| 9 |
except: DDGS = None
|
| 10 |
try: from gtts import gTTS
|
|
@@ -14,7 +14,7 @@ except: gTTS = None
|
|
| 14 |
USER_ID, USER_PASS = "admin", "123456"
|
| 15 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 16 |
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
|
| 17 |
-
DB_FILE = "
|
| 18 |
|
| 19 |
def load_db():
|
| 20 |
if os.path.exists(DB_FILE):
|
|
@@ -27,7 +27,7 @@ def save_db(data):
|
|
| 27 |
with open(DB_FILE, "w", encoding="utf-8") as f:
|
| 28 |
json.dump(data, f, ensure_ascii=False, indent=4)
|
| 29 |
|
| 30 |
-
# 2. GUI DESIGN (
|
| 31 |
custom_css = """
|
| 32 |
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@700&family=Plus+Jakarta+Sans:wght@400;600&display=swap');
|
| 33 |
|
|
@@ -36,7 +36,6 @@ custom_css = """
|
|
| 36 |
font-family: 'Plus Jakarta Sans', sans-serif !important;
|
| 37 |
}
|
| 38 |
|
| 39 |
-
/* Sidebar giữ nguyên */
|
| 40 |
.sidebar-pro {
|
| 41 |
position: fixed !important;
|
| 42 |
left: 10px !important; top: 10px !important;
|
|
@@ -58,42 +57,41 @@ custom_css = """
|
|
| 58 |
text-align: center; margin-bottom: 25px;
|
| 59 |
}
|
| 60 |
|
| 61 |
-
/* Khu vực Chat - Không cho phép tràn ngang */
|
| 62 |
.chat-col {
|
| 63 |
margin-left: 300px !important;
|
| 64 |
width: calc(100% - 320px) !important;
|
| 65 |
height: 100vh !important;
|
| 66 |
-
position: relative !important;
|
| 67 |
}
|
| 68 |
|
| 69 |
-
.gr-chatbot {
|
| 70 |
-
background: transparent !important;
|
| 71 |
-
border: none !important;
|
| 72 |
-
margin-bottom: 120px !important; /* Tạo khoảng trống cho input */
|
| 73 |
-
}
|
| 74 |
|
| 75 |
-
/*
|
| 76 |
.control-panel-fixed {
|
| 77 |
position: fixed !important;
|
| 78 |
-
bottom:
|
| 79 |
left: 310px !important;
|
| 80 |
-
right:
|
| 81 |
-
background: rgba(20, 20,
|
| 82 |
-
backdrop-filter: blur(
|
| 83 |
-
border: 1px solid rgba(0, 242, 254, 0.
|
| 84 |
-
border-radius:
|
| 85 |
-
padding:
|
| 86 |
z-index: 9999 !important;
|
| 87 |
display: flex !important;
|
| 88 |
-
flex-direction: row !important;
|
| 89 |
align-items: center !important;
|
| 90 |
gap: 15px !important;
|
|
|
|
| 91 |
}
|
| 92 |
|
| 93 |
.input-box { flex: 1 !important; border: none !important; background: transparent !important; }
|
| 94 |
|
| 95 |
-
/*
|
| 96 |
-
.mic-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
footer { display: none !important; }
|
| 99 |
"""
|
|
@@ -109,14 +107,12 @@ def search_web(query):
|
|
| 109 |
|
| 110 |
def chat_fn(message, history, session_id, store):
|
| 111 |
user_text = message.get("text", "") if isinstance(message, dict) else message
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
if not user_text and not files: return {"text": ""}, history, session_id, store, None
|
| 115 |
|
| 116 |
if session_id not in store:
|
| 117 |
-
store[session_id] = {"title": user_text[:20] if user_text else "
|
| 118 |
|
| 119 |
-
web_info = search_web(user_text) if any(x in user_text.lower() for x in ["tìm", "tra", "web"
|
| 120 |
|
| 121 |
api_messages = [{"role": "system", "content": f"Bạn là NodeAI Genesis. Master: Trần Đình Phúc. Web: {web_info}"}]
|
| 122 |
for msg in history: api_messages.append({"role": msg["role"], "content": msg["content"]})
|
|
@@ -164,24 +160,23 @@ with gr.Blocks(title="NodeAI Genesis") as demo:
|
|
| 164 |
|
| 165 |
# MAIN CONTENT
|
| 166 |
with gr.Column(elem_classes="chat-col"):
|
| 167 |
-
chatbot = gr.Chatbot(show_label=False, height="
|
| 168 |
audio_out = gr.Audio(autoplay=True, visible=False)
|
| 169 |
|
| 170 |
-
# THANH
|
| 171 |
with gr.Row(elem_classes="control-panel-fixed"):
|
| 172 |
-
with gr.Column(scale=
|
| 173 |
msg_input = gr.MultimodalTextbox(
|
| 174 |
-
show_label=False, placeholder="
|
| 175 |
)
|
| 176 |
-
with gr.Column(scale=
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
gr.HTML("<p style='text-align:center; color:rgba(0, 242, 254, 0.1); font-size:10px;'>NODEAI GENESIS V30.6 • BY PHÚC</p>")
|
| 180 |
|
| 181 |
-
#
|
| 182 |
msg_input.submit(chat_fn, [msg_input, chatbot, session_id, chat_store], [msg_input, chatbot, session_id, chat_store, audio_out]).then(tts_engine, audio_out, audio_out)
|
| 183 |
|
| 184 |
-
def process_mic(path): return {"text": "
|
| 185 |
mic_btn.stop_recording(process_mic, mic_btn, msg_input)
|
| 186 |
|
| 187 |
new_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
|
|
|
|
| 4 |
import uuid
|
| 5 |
import json
|
| 6 |
|
| 7 |
+
# Import an toàn
|
| 8 |
try: from duckduckgo_search import DDGS
|
| 9 |
except: DDGS = None
|
| 10 |
try: from gtts import gTTS
|
|
|
|
| 14 |
USER_ID, USER_PASS = "admin", "123456"
|
| 15 |
HF_TOKEN = os.getenv("HUGGINGFACE_HUB_TOKEN")
|
| 16 |
client = InferenceClient(model="Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
|
| 17 |
+
DB_FILE = "nodeai_platinum_v3.json"
|
| 18 |
|
| 19 |
def load_db():
|
| 20 |
if os.path.exists(DB_FILE):
|
|
|
|
| 27 |
with open(DB_FILE, "w", encoding="utf-8") as f:
|
| 28 |
json.dump(data, f, ensure_ascii=False, indent=4)
|
| 29 |
|
| 30 |
+
# 2. GUI DESIGN (FIX LỖI LAYOUT & MIC RÕ RÀNG)
|
| 31 |
custom_css = """
|
| 32 |
@import url('https://fonts.googleapis.com/css2?family=Orbitron:wght@700&family=Plus+Jakarta+Sans:wght@400;600&display=swap');
|
| 33 |
|
|
|
|
| 36 |
font-family: 'Plus Jakarta Sans', sans-serif !important;
|
| 37 |
}
|
| 38 |
|
|
|
|
| 39 |
.sidebar-pro {
|
| 40 |
position: fixed !important;
|
| 41 |
left: 10px !important; top: 10px !important;
|
|
|
|
| 57 |
text-align: center; margin-bottom: 25px;
|
| 58 |
}
|
| 59 |
|
|
|
|
| 60 |
.chat-col {
|
| 61 |
margin-left: 300px !important;
|
| 62 |
width: calc(100% - 320px) !important;
|
| 63 |
height: 100vh !important;
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
+
.gr-chatbot { background: transparent !important; border: none !important; margin-bottom: 150px !important; }
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
/* THANH NHẬP LIỆU CỐ ĐỊNH - ĐÃ FIX LỖI DỊCH CHUYỂN */
|
| 69 |
.control-panel-fixed {
|
| 70 |
position: fixed !important;
|
| 71 |
+
bottom: 25px !important;
|
| 72 |
left: 310px !important;
|
| 73 |
+
right: 25px !important;
|
| 74 |
+
background: rgba(20, 20, 35, 0.9) !important;
|
| 75 |
+
backdrop-filter: blur(15px) !important;
|
| 76 |
+
border: 1px solid rgba(0, 242, 254, 0.4) !important;
|
| 77 |
+
border-radius: 35px !important;
|
| 78 |
+
padding: 12px 25px !important;
|
| 79 |
z-index: 9999 !important;
|
| 80 |
display: flex !important;
|
|
|
|
| 81 |
align-items: center !important;
|
| 82 |
gap: 15px !important;
|
| 83 |
+
box-shadow: 0 10px 40px rgba(0,0,0,0.5) !important;
|
| 84 |
}
|
| 85 |
|
| 86 |
.input-box { flex: 1 !important; border: none !important; background: transparent !important; }
|
| 87 |
|
| 88 |
+
/* NÚT MIC TO VÀ RÕ RÀNG */
|
| 89 |
+
.mic-container {
|
| 90 |
+
min-width: 150px !important;
|
| 91 |
+
background: rgba(255, 255, 255, 0.05) !important;
|
| 92 |
+
border-radius: 20px !important;
|
| 93 |
+
padding: 5px !important;
|
| 94 |
+
}
|
| 95 |
|
| 96 |
footer { display: none !important; }
|
| 97 |
"""
|
|
|
|
| 107 |
|
| 108 |
def chat_fn(message, history, session_id, store):
|
| 109 |
user_text = message.get("text", "") if isinstance(message, dict) else message
|
| 110 |
+
if not user_text and not message.get("files", []): return {"text": ""}, history, session_id, store, None
|
|
|
|
|
|
|
| 111 |
|
| 112 |
if session_id not in store:
|
| 113 |
+
store[session_id] = {"title": (user_text[:20] if user_text else "Audio Task"), "history": []}
|
| 114 |
|
| 115 |
+
web_info = search_web(user_text) if any(x in user_text.lower() for x in ["tìm", "tra", "web"]) else ""
|
| 116 |
|
| 117 |
api_messages = [{"role": "system", "content": f"Bạn là NodeAI Genesis. Master: Trần Đình Phúc. Web: {web_info}"}]
|
| 118 |
for msg in history: api_messages.append({"role": msg["role"], "content": msg["content"]})
|
|
|
|
| 160 |
|
| 161 |
# MAIN CONTENT
|
| 162 |
with gr.Column(elem_classes="chat-col"):
|
| 163 |
+
chatbot = gr.Chatbot(show_label=False, height="75vh")
|
| 164 |
audio_out = gr.Audio(autoplay=True, visible=False)
|
| 165 |
|
| 166 |
+
# THANH ĐIỀU KHIỂN
|
| 167 |
with gr.Row(elem_classes="control-panel-fixed"):
|
| 168 |
+
with gr.Column(scale=7):
|
| 169 |
msg_input = gr.MultimodalTextbox(
|
| 170 |
+
show_label=False, placeholder="Hỏi NodeAI hoặc gửi ảnh...", container=False, elem_classes="input-box"
|
| 171 |
)
|
| 172 |
+
with gr.Column(scale=3, elem_classes="mic-container"):
|
| 173 |
+
# ĐÂY LÀ MIC CỦA BẠN: Bấm vào đây để nói!
|
| 174 |
+
mic_btn = gr.Audio(sources=["microphone"], type="filepath", label="Ghi âm", show_label=True)
|
|
|
|
| 175 |
|
| 176 |
+
# Xử lý sự kiện
|
| 177 |
msg_input.submit(chat_fn, [msg_input, chatbot, session_id, chat_store], [msg_input, chatbot, session_id, chat_store, audio_out]).then(tts_engine, audio_out, audio_out)
|
| 178 |
|
| 179 |
+
def process_mic(path): return {"text": "Dịch và trả lời âm thanh này", "files": [path]}
|
| 180 |
mic_btn.stop_recording(process_mic, mic_btn, msg_input)
|
| 181 |
|
| 182 |
new_btn.click(lambda: ([], str(uuid.uuid4())), None, [chatbot, session_id])
|