Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -373,27 +373,128 @@ def create_qa_chain(_llm, _retriever):
|
|
| 373 |
|
| 374 |
# UI
|
| 375 |
def main():
|
| 376 |
-
|
| 377 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 378 |
|
| 379 |
try:
|
| 380 |
-
# تحميل المكونات
|
| 381 |
llm = load_model()
|
| 382 |
retriever = load_data()
|
| 383 |
qa_chain = create_qa_chain(llm, retriever)
|
| 384 |
|
| 385 |
-
# إدخال ال
|
| 386 |
-
user_query = st.text_area(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
st.markdown(f"**{response}**")
|
| 394 |
-
|
| 395 |
except Exception as e:
|
| 396 |
st.error(f"حدث خطأ: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 397 |
|
| 398 |
if __name__ == "__main__":
|
| 399 |
main()
|
|
|
|
|
|
| 373 |
|
| 374 |
# UI
|
| 375 |
def main():
|
| 376 |
+
# تخصيص مظهر الصفحة
|
| 377 |
+
st.set_page_config(
|
| 378 |
+
page_title="مساعد اتصالات المستقبل",
|
| 379 |
+
page_icon="📞",
|
| 380 |
+
layout="centered",
|
| 381 |
+
initial_sidebar_state="expanded"
|
| 382 |
+
)
|
| 383 |
+
|
| 384 |
+
# أسلوب CSS مخصص للدردشة
|
| 385 |
+
st.markdown("""
|
| 386 |
+
<style>
|
| 387 |
+
.chat-container {
|
| 388 |
+
direction: rtl;
|
| 389 |
+
display: flex;
|
| 390 |
+
flex-direction: column;
|
| 391 |
+
gap: 10px;
|
| 392 |
+
margin-bottom: 20px;
|
| 393 |
+
}
|
| 394 |
+
.user-message {
|
| 395 |
+
background-color: #E3F2FD;
|
| 396 |
+
padding: 12px 16px;
|
| 397 |
+
border-radius: 18px 18px 0 18px;
|
| 398 |
+
align-self: flex-end;
|
| 399 |
+
max-width: 80%;
|
| 400 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 401 |
+
}
|
| 402 |
+
.bot-message {
|
| 403 |
+
background-color: #f1f1f1;
|
| 404 |
+
padding: 12px 16px;
|
| 405 |
+
border-radius: 18px 18px 18px 0;
|
| 406 |
+
align-self: flex-start;
|
| 407 |
+
max-width: 80%;
|
| 408 |
+
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 409 |
+
}
|
| 410 |
+
.stTextInput input, .stTextArea textarea {
|
| 411 |
+
border: 2px solid #4CAF50 !important;
|
| 412 |
+
border-radius: 10px !important;
|
| 413 |
+
padding: 10px !important;
|
| 414 |
+
}
|
| 415 |
+
.stButton button {
|
| 416 |
+
background-color: #4CAF50 !important;
|
| 417 |
+
color: white !important;
|
| 418 |
+
border-radius: 10px !important;
|
| 419 |
+
padding: 10px 24px !important;
|
| 420 |
+
width: 100% !important;
|
| 421 |
+
font-weight: bold !important;
|
| 422 |
+
}
|
| 423 |
+
</style>
|
| 424 |
+
""", unsafe_allow_html=True)
|
| 425 |
+
|
| 426 |
+
# رأس الصفحة
|
| 427 |
+
st.markdown('<h1 style="color: #2E86C1; text-align:center">📞 مساعد اتصالات المستقبل</h1>', unsafe_allow_html=True)
|
| 428 |
+
st.markdown("---")
|
| 429 |
+
|
| 430 |
+
# تهيئة حالة الجلسة لتخزين المحادثة
|
| 431 |
+
if 'chat_history' not in st.session_state:
|
| 432 |
+
st.session_state.chat_history = []
|
| 433 |
+
|
| 434 |
+
# عرض تاريخ المحادثة
|
| 435 |
+
with st.container():
|
| 436 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
| 437 |
+
|
| 438 |
+
for message in st.session_state.chat_history:
|
| 439 |
+
if message['type'] == 'user':
|
| 440 |
+
st.markdown(f'<div class="user-message">👤 {message["content"]}</div>', unsafe_allow_html=True)
|
| 441 |
+
else:
|
| 442 |
+
st.markdown(f'<div class="bot-message">🤖 {message["content"]}</div>', unsafe_allow_html=True)
|
| 443 |
+
|
| 444 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
| 445 |
|
| 446 |
try:
|
| 447 |
+
# تحميل المكونات
|
| 448 |
llm = load_model()
|
| 449 |
retriever = load_data()
|
| 450 |
qa_chain = create_qa_chain(llm, retriever)
|
| 451 |
|
| 452 |
+
# قسم إدخال السؤال
|
| 453 |
+
user_query = st.text_area(
|
| 454 |
+
"اكتب رسالتك هنا:",
|
| 455 |
+
height=100,
|
| 456 |
+
placeholder="اطرح سؤالك عن خدماتنا...",
|
| 457 |
+
key="user_input"
|
| 458 |
+
)
|
| 459 |
+
|
| 460 |
+
col1, col2 = st.columns([1, 3])
|
| 461 |
+
with col1:
|
| 462 |
+
if st.button("إرسال", key="send_btn"):
|
| 463 |
+
if user_query.strip():
|
| 464 |
+
# إضافة سؤال المستخدم إلى التاريخ
|
| 465 |
+
st.session_state.chat_history.append({
|
| 466 |
+
'type': 'user',
|
| 467 |
+
'content': user_query
|
| 468 |
+
})
|
| 469 |
+
|
| 470 |
+
with st.spinner('جاري تحضير الإجابة...'):
|
| 471 |
+
# الحصول على الإجابة
|
| 472 |
+
response = qa_chain.run(user_query)
|
| 473 |
+
|
| 474 |
+
# إضافة إجابة البوت إلى التاريخ
|
| 475 |
+
st.session_state.chat_history.append({
|
| 476 |
+
'type': 'bot',
|
| 477 |
+
'content': response
|
| 478 |
+
})
|
| 479 |
+
|
| 480 |
+
# إعادة تحميل الصفحة لعرض المحادثة الجديدة
|
| 481 |
+
st.experimental_rerun()
|
| 482 |
+
else:
|
| 483 |
+
st.warning("الرجاء إدخال سؤال قبل الإرسال")
|
| 484 |
|
| 485 |
+
with col2:
|
| 486 |
+
if st.button("مسح المحادثة", key="clear_btn"):
|
| 487 |
+
st.session_state.chat_history = []
|
| 488 |
+
st.experimental_rerun()
|
| 489 |
+
|
|
|
|
|
|
|
| 490 |
except Exception as e:
|
| 491 |
st.error(f"حدث خطأ: {str(e)}")
|
| 492 |
+
st.session_state.chat_history.append({
|
| 493 |
+
'type': 'bot',
|
| 494 |
+
'content': "عذراً، حدث خطأ في النظام. يرجى المحاولة مرة أخرى."
|
| 495 |
+
})
|
| 496 |
+
st.experimental_rerun()
|
| 497 |
|
| 498 |
if __name__ == "__main__":
|
| 499 |
main()
|
| 500 |
+
|