Spaces:
Running
Running
File size: 5,760 Bytes
1f6dd2d 8d3615d a3a09c3 1f6dd2d a3a09c3 1f6dd2d b037cc8 1f6dd2d 257387c ab12a7e 1f6dd2d 0dd091b 6618541 1f6dd2d ab12a7e 1f6dd2d 0c3ae12 77c9bb5 027f141 77c9bb5 1f6dd2d 77c9bb5 a3a09c3 1f6dd2d a3a09c3 3a27a8e 1f6dd2d 3a27a8e a3a09c3 1f6dd2d a3a09c3 1f6dd2d |
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 |
import os
# Disable Streamlit telemetry and cache writing
os.environ["STREAMLIT_HOME"] = "/tmp" # safe writable dir in Hugging Face
os.environ["STREAMLIT_DISABLE_LOGGING"] = "1"
os.environ["STREAMLIT_TELEMETRY_ENABLED"] = "0"
import streamlit as st
from huggingface_hub import InferenceClient
import json
# Initialize Hugging Face client
client = InferenceClient(
provider="cohere",
# api_key= st.secrets["hf_token"]
api_key = os.getenv("hf_token")
)
# Page config
st.set_page_config(page_title="شَيُوك – مساعدك الذكي", page_icon="🛍️", layout="centered")
# Helper to get logo path
def get_image_path(filename):
local_path = os.path.join("assets", filename)
if os.path.exists(local_path):
return local_path
return "assets/chyaka_logof.png" # fallback path within the same repo
# --- HEADER SECTION ---
col1, col2 = st.columns([1, 4])
with col1:
try:
#logo_path = get_image_path("chyaka_logof.png")
#logo_path = "./src/chyaka_logof.png"
logo_path = "./src/assets/chyaka_logof.png"
st.image(logo_path, width=150)
except:
st.empty()
with col2:
st.markdown("<h1 style='font-size: 32px; margin-bottom: 0;'>شَيُوك 🤖</h1>", unsafe_allow_html=True)
st.markdown("##### مساعدك الذكي من متجر شياكة")
st.markdown("---")
# --- LOAD PRODUCT DATA ---
print("Current working directory:", os.getcwd())
print("Files in current directory:", os.listdir())
try:
with open("./src/product_catalog.json", "r", encoding="utf-8") as f:
products = json.load(f)
except FileNotFoundError:
st.error("⚠️ تعذر تحميل المنتجات – تحقق من وجود ملف 'product_catalog.json'")
products = []
if products:
products_info = "\n".join([
f"- {p['name_ar']} ({p['name_en']}) – السعر: {p['price']} – الألوان: {', '.join(p['colors']) if p['colors'] else 'بدون ألوان'} – الكمية المتوفرة: {p['quantity']}"
for p in products
])
else:
products_info = "🚫 لا توجد منتجات متاحة حالياً."
# --- SYSTEM PROMPT ---
SYSTEM_PROMPT = f"""
أنت "شَيُوك"، مساعد ذكي ودود تتحدث باللهجة الإماراتية، وتخدم عملاء متجر "شياكة" الإلكتروني المتخصص في العبايات، المخوّر، والعطور.
مهمتك هي الرد على استفسارات الزبائن بطريقة لبقة، مفهومة، وبأسلوب ولهجة إماراتية طبيعية. خلك دوم محترم، واضح، وساعد الزبون بأفضل شكل ممكن.
🛍️ معلومات عامة:
- "شياكة" متجر إماراتي يقدّم عبايات ومخوّر وعطور بجودة عالية.
- الأسعار تكون بالدرهم الإماراتي.
- اللهجة الإماراتية مطلوبة في الردود، لكن بدون مبالغة أو كلمات عامية جدًا. خلك وسط بين الرسمية والراحة.
معلومات عن المنتجات:
{products_info}
- إذا سأل الزبون عن منتج، قدّم له معلومات واضحة: السعر، الألوان، الكمية، أو خيارات مشابهة.
حالة الطلبات : استعملها في الردود على استفسارات الزبائن عن حالة الطلبات.
- رقم الطلب: ORD239847
- الحالة: الطلب متأخر
- موعد التوصيل المتوقع: 12 مايو 2025
- رقم الطلب: ORD762945
- الحالة: قيد التجهيز
- موعد التوصيل المتوقع: 15 مايو 2025
📌 التعليمات:
- أولاً، إذا سأل الزبون عن حالة الطلب، اسأله عن رقم الطلب حتى تتمكن من تحديد حالته بدقة.
- إذا طلب الزبون إلغاء أو استرجاع، وضّح الخطوات حسب سياسة الإرجاع بأسلوب بسيط.
- إذا سأل عن منتج، قدم له معلومات واضحة: السعر، الألوان، الكمية، أو خيارات مشابهة.
- لا تستخدم لغة فصحى رسمية بزيادة مثل "نرجو منك"، ولا عامية جدًا مثل "مب"، بل خلي الأسلوب بين الاثنين.
- استخدم كلمات مثل: "تم"، "أكيد"، "على راحتك"، "اهلا وسهلا"، "بخدمتك"، "ولا يهمك"، بدلًا من اللغة الرسمية أو الفصحى.
"""
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display previous messages
for msg in st.session_state.messages:
with st.chat_message(msg["role"]):
st.markdown(msg["content"])
# Chat input
prompt = st.chat_input("اكتب رسالتك هنا...")
if prompt:
# Save user message
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Prepare messages including system prompt
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
messages += st.session_state.messages
# Call HF API
try:
completion = client.chat.completions.create(
model="CohereLabs/c4ai-command-r7b-arabic-02-2025",
messages=messages,
max_tokens=512,
)
model_reply = completion.choices[0].message.content
except Exception as e:
model_reply = f"❌ فشل الاتصال بالنموذج: {e}"
# Save and display assistant reply
st.session_state.messages.append({"role": "assistant", "content": model_reply})
with st.chat_message("assistant"):
st.markdown(model_reply) |