Spaces:
Running
Running
File size: 18,421 Bytes
c268c45 bb7225b e222dff c268c45 634ebde c268c45 a54671e d68fdb5 a54671e c4d7d0b f998e73 c4d7d0b 634ebde c4d7d0b 6fd0d30 c4d7d0b 634ebde c4d7d0b c268c45 aa09783 0b4fb1b c268c45 634ebde d478d08 a54671e c268c45 a54671e c268c45 a54671e c268c45 a54671e c268c45 a54671e 634ebde a54671e c268c45 a54671e c4d7d0b a54671e c4d7d0b a54671e c4d7d0b a54671e 634ebde a54671e c268c45 a54671e a0b689b 23bda7a a0b689b a54671e a0b689b 5e070cc 4130c44 a0b689b 5e070cc 3f6ac4f d68fdb5 3f6ac4f 5e070cc 3f6ac4f 30afb86 5e070cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 |
import time
import json
import requests
import streamlit as st
import os
from urllib.parse import urlencode, urlparse, parse_qs
st.set_page_config(page_title="ViBidLQA - Trợ lý AI hỗ trợ hỏi đáp luật Việt Nam", page_icon="./app/static/ai.jpg", layout="centered", initial_sidebar_state="collapsed")
# ==== MÔI TRƯỜNG OAuth ====
FB_APP_ID = os.getenv("FB_APP_ID")
FB_APP_SECRET = os.getenv("FB_APP_SECRET")
FB_REDIRECT_URI = os.getenv("FB_REDIRECT_URI")
FB_CLIENT_URL = os.getenv("FB_CLIENT_URL", "https://www.facebook.com")
FB_API_URL = os.getenv("FB_API_URL", "https://graph.facebook.com")
FB_BACKEND_URL = os.getenv("FB_BACKEND_URL")
# ==== MODULE URL ====
routing_response_module = st.secrets["ViBidLQA_Routing_Module"]
retrieval_module = st.secrets["ViBidLQA_Retrieval_Module"]
reranker_module = st.secrets["ViBidLQA_Rerank_Module"]
abs_QA_module = st.secrets["ViBidLQA_AQA_Module"]
url_api_question_classify_model = f"{routing_response_module}/query_classify"
url_api_unrelated_question_response_model = f"{routing_response_module}/response_unrelated_question"
url_api_introduce_system_model = f"{routing_response_module}/about_me"
url_api_retrieval_model = f"{retrieval_module}/search"
url_api_reranker_model = f"{reranker_module}/rerank"
url_api_generation_model = f"{abs_QA_module}/answer"
# # ========= FLASK APP ===============
# flask_app = Flask(__name__)
# # Dùng để lưu tin nhắn nhận được trong session
# if "messages" not in st.session_state:
# st.session_state.messages = []
# # Gửi tin nhắn tới người dùng
# def send_message(recipient_id, message):
# url = f"{FB_API_URL}/me/messages?access_token={FB_PAGE_ACCESS_TOKEN}"
# payload = {
# "recipient": {"id": recipient_id},
# "message": {"text": message}
# }
# response = requests.post(url, json=payload)
# return response.ok
# # Xử lý GET và POST từ Facebook Webhook
# @flask_app.route("/webhook", methods=["GET", "POST"])
# def webhook():
# if request.method == "GET":
# if request.args.get("hub.verify_token") == FB_VERIFY_TOKEN:
# return request.args.get("hub.challenge")
# return "Verification token mismatch", 403
# if request.method == "POST":
# data = request.get_json()
# if "entry" in data:
# for entry in data["entry"]:
# for event in entry["messaging"]:
# sender_id = event["sender"]["id"]
# message_text = event.get("message", {}).get("text", "")
# if message_text:
# # Lưu vào session_state
# st.session_state.messages.append(
# {"sender_id": sender_id, "text": message_text}
# )
# # Gửi trả lời mặc định
# send_message(sender_id, "Cảm ơn bạn đã nhắn tin!")
# return "OK", 200
# # Chạy Flask trong luồng riêng
# def run_flask():
# flask_app.run(host="0.0.0.0", port=5000)
# threading.Thread(target=run_flask, daemon=True).start()
# ========== STREAMLIT UI ==========
with open("./static/styles.css") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
# ==== GIAO DIỆN CHÍNH - TABS ====
tab1, tab2 = st.tabs(["🤖 ViBidLQA Chatbot", "🔐 Facebook OAuth"])
# =============================
# TAB 1: VIBIDLQA CHATBOT
# =============================
with tab1:
if 'messages' not in st.session_state:
st.session_state.messages = [{'role': 'assistant', 'content': "Xin chào. Tôi là trợ lý AI văn bản luật Đấu thầu Việt Nam được phát triển bởi Nguyễn Trường Phúc và các cộng sự. Rất vui khi được hỗ trợ bạn trong các vấn đề pháp lý tại Việt Nam!"}]
st.markdown(f"""
<div class=logo_area>
<img src="./app/static/ai.jpg"/>
</div>
""", unsafe_allow_html=True)
st.markdown("<h2 style='text-align: center;'>ViBidLQA</h2>", unsafe_allow_html=True)
def classify_question(question):
data = {
"question": question
}
response = requests.post(url_api_question_classify_model, json=data)
if response.status_code == 200:
print(response)
return response
else:
return f"Lỗi: {response.status_code} - {response.text}"
def introduce_system(question):
data = {
"question": question
}
response = requests.post(url_api_introduce_system_model, json=data, stream=True)
if response.status_code == 200:
return response
else:
return f"Lỗi: {response.status_code} - {response.text}"
def response_unrelated_question(question):
data = {
"question": question
}
response = requests.post(url_api_unrelated_question_response_model, json=data, stream=True)
if response.status_code == 200:
return response
else:
return f"Lỗi: {response.status_code} - {response.text}"
def retrieve_context(question, top_k=10):
data = {
"query": question,
"top_k": top_k
}
response = requests.post(url_api_retrieval_model, json=data)
if response.status_code == 200:
results = response.json()["results"]
return results
else:
return f"Lỗi tại Retrieval Module: {response.status_code} - {response.text}"
def rerank_context(url_rerank_module, question, relevant_docs, top_k=5):
data = {
"question": question,
"relevant_docs": relevant_docs,
"top_k": top_k
}
response = requests.post(url_rerank_module, json=data)
if response.status_code == 200:
results = response.json()["reranked_docs"]
return results
else:
return f"Lỗi tại Rerank module: {response.status_code} - {response.text}"
def get_abstractive_answer(question):
retrieved_context = retrieve_context(question=question)
retrieved_context = [item['text'] for item in retrieved_context]
reranked_context = rerank_context(url_rerank_module=url_api_reranker_model,
question=question,
relevant_docs=retrieved_context,
top_k=5)[0]
data = {
"context": reranked_context,
"question": question
}
response = requests.post(url_api_generation_model, json=data, stream=True)
if response.status_code == 200:
return response
else:
return f"Lỗi: {response.status_code} - {response.text}"
def generate_text_effect(answer):
words = answer.split()
for i in range(len(words)):
time.sleep(0.03)
yield " ".join(words[:i+1])
for message in st.session_state.messages:
if message['role'] == 'assistant':
avatar_class = "assistant-avatar"
message_class = "assistant-message"
avatar = './app/static/ai.jpg'
else:
avatar_class = ""
message_class = "user-message"
avatar = ''
st.markdown(f"""
<div class="{message_class}">
<img src="{avatar}" class="{avatar_class}" />
<div class="stMarkdown">{message['content']}</div>
</div>
""", unsafe_allow_html=True)
if prompt := st.chat_input(placeholder='Tôi có thể giúp được gì cho bạn?'):
st.markdown(f"""
<div class="user-message">
<div class="stMarkdown">{prompt}</div>
</div>
""", unsafe_allow_html=True)
st.session_state.messages.append({'role': 'user', 'content': prompt})
message_placeholder = st.empty()
full_response = ""
classify_result = classify_question(question=prompt).json()
print(f"The type of user query: {classify_result}")
if classify_result == "BIDDING_RELATED":
abs_answer = get_abstractive_answer(question=prompt)
if isinstance(abs_answer, str):
full_response = abs_answer
message_placeholder.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.jpg" class="assistant-avatar" />
<div class="stMarkdown">{full_response}</div>
</div>
""", unsafe_allow_html=True)
else:
full_response = ""
for line in abs_answer.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_str = line[6:]
if data_str == '[DONE]':
break
try:
data = json.loads(data_str)
token = data.get('token', '')
full_response += token
message_placeholder.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.jpg" class="assistant-avatar" />
<div class="stMarkdown">{full_response}●</div>
</div>
""", unsafe_allow_html=True)
except json.JSONDecodeError:
pass
elif classify_result == "ABOUT_CHATBOT":
answer = introduce_system(question=prompt)
if isinstance(answer, str):
full_response = answer
message_placeholder.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.jpg" class="assistant-avatar" />
<div class="stMarkdown">{full_response}</div>
</div>
""", unsafe_allow_html=True)
else:
full_response = ""
for line in answer.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_str = line[6:]
if data_str == '[DONE]':
break
try:
data = json.loads(data_str)
token = data.get('token', '')
full_response += token
message_placeholder.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.jpg" class="assistant-avatar" />
<div class="stMarkdown">{full_response}●</div>
</div>
""", unsafe_allow_html=True)
except json.JSONDecodeError:
pass
else:
answer = response_unrelated_question(question=prompt)
if isinstance(answer, str):
full_response = answer
message_placeholder.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.jpg" class="assistant-avatar" />
<div class="stMarkdown">{full_response}</div>
</div>
""", unsafe_allow_html=True)
else:
full_response = ""
for line in answer.iter_lines():
if line:
line = line.decode('utf-8')
if line.startswith('data: '):
data_str = line[6:]
if data_str == '[DONE]':
break
try:
data = json.loads(data_str)
token = data.get('token', '')
full_response += token
message_placeholder.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.jpg" class="assistant-avatar" />
<div class="stMarkdown">{full_response}●</div>
</div>
""", unsafe_allow_html=True)
except json.JSONDecodeError:
pass
message_placeholder.markdown(f"""
<div class="assistant-message">
<img src="./app/static/ai.jpg" class="assistant-avatar" />
<div class="stMarkdown">
{full_response}
</div>
</div>
""", unsafe_allow_html=True)
st.session_state.messages.append({'role': 'assistant', 'content': full_response})
# =============================
# TAB 2: FACEBOOK OAUTH
# =============================
with tab2:
st.title("Facebook OAuth Integration")
# Định nghĩa hàm đăng ký webhook Facebook
def register_facebook_webhook(page_id: str, page_access_token: str):
try:
url = f"https://graph.facebook.com/v19.0/{page_id}/subscribed_apps"
params = {
"subscribed_fields": "messages,messaging_postbacks",
"access_token": page_access_token
}
response = requests.post(url, params=params)
response.raise_for_status()
data = response.json()
if data.get("success"):
return True, "Đăng ký webhook thành công."
else:
return False, f"Facebook trả về lỗi: {data}"
except requests.exceptions.RequestException as e:
return False, f"Lỗi khi gọi Facebook API: {e}"
if "token" not in st.session_state:
params = {
"client_id": FB_APP_ID,
"redirect_uri": FB_REDIRECT_URI,
"scope": "pages_show_list,pages_manage_metadata,pages_messaging",
}
auth_url = f"{FB_CLIENT_URL}/dialog/oauth?{urlencode(params)}"
st.markdown("### Step 1: Đăng nhập Facebook")
st.markdown(f"[Bấm vào đây để đăng nhập Facebook]({auth_url})")
query_params = st.query_params
if "code" in query_params:
code = query_params["code"]
try:
token_response = requests.get(f"{FB_API_URL}/oauth/access_token", params={
"client_id": FB_APP_ID,
"redirect_uri": FB_REDIRECT_URI,
"client_secret": FB_APP_SECRET,
"code": code,
})
token = token_response.json()["access_token"]
st.session_state.token = token
st.success("🎉 Lấy access token thành công!")
st.markdown("""
<script>
const url = new URL(window.location.href);
url.searchParams.delete("code");
window.location.href = url.pathname;
</script>
""", unsafe_allow_html=True)
# Lấy page
pages_response = requests.get(f"{FB_API_URL}/me/accounts", params={"access_token": token})
pages = pages_response.json().get("data", [])
st.session_state.pages = pages
st.markdown("### Danh sách các Page bạn quản lý:")
for page in pages:
st.json(page)
except Exception as e:
st.error(f"Lỗi khi trao đổi token: {e}")
if "pages" in st.session_state and st.session_state.pages:
st.markdown("### Step 3: Đăng ký Webhook cho các page")
selected_pages = st.multiselect(
"Chọn các page để đăng ký webhook:",
options=[f"{p['name']} ({p['id']})" for p in st.session_state.pages]
)
if st.button("Đăng ký Webhook"):
# selected_pages = st.session_state.selected_pages # Giả sử bạn có danh sách page đã chọn
for page in st.session_state.pages:
label = f"{page['name']} ({page['id']})"
if label in selected_pages:
page_id = page['id']
page_access_token = page['access_token']
response = requests.post(f"{FB_BACKEND_URL}/register-webhook", json={
"page_id": page_id,
"page_access_token": page_access_token
})
res_json = response.json()
if res_json["success"]:
st.success(f"✅ Đã đăng ký Webhook cho page: {page['name']}")
else:
st.warning(f"⚠️ Lỗi với page {page['name']}: {res_json['message']}")
if st.button("Hiển thị Thông tin Trang"):
for page in st.session_state.pages:
page_id = page['id']
page_name = page['name']
page_access_token = page['access_token']
# Hiển thị thông tin của từng page
st.write(f"**Page Name**: {page_name}")
st.write(f"**Page ID**: {page_id}")
st.write(f"**Page Access Token**: {page_access_token}")
st.write("---") # Dấu phân cách giữa các trang |