Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +38 -155
src/streamlit_app.py
CHANGED
|
@@ -3,172 +3,55 @@ from openai import OpenAI
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
# إعداد الصفحة
|
| 6 |
-
st.set_page_config(
|
| 7 |
-
page_title="مساعد AI الذكي",
|
| 8 |
-
page_icon="🤖",
|
| 9 |
-
layout="centered"
|
| 10 |
-
)
|
| 11 |
|
| 12 |
-
# العنوان
|
| 13 |
-
st.title("
|
| 14 |
st.markdown("---")
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
st.header("⚙️ الإعدادات")
|
| 19 |
-
api_key = st.text_input(
|
| 20 |
-
"مفتاح API الخاص بك",
|
| 21 |
-
type="password",
|
| 22 |
-
value="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", # مفتاحك
|
| 23 |
-
help="مفتاح API من NVIDIA"
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# اختيار النموذج
|
| 27 |
-
model_options = {
|
| 28 |
-
"stepfun-ai/step-3.5-flash": "🚀 Step 3.5 Flash (سريع)",
|
| 29 |
-
"nvidia/llama-3.1-nemotron-70b-instruct": "🧠 Nemotron 70B (قوي)",
|
| 30 |
-
"meta/llama-3.3-70b-instruct": "📚 Llama 3.3 70B"
|
| 31 |
-
}
|
| 32 |
-
|
| 33 |
-
selected_model = st.selectbox(
|
| 34 |
-
"اختر النموذج",
|
| 35 |
-
options=list(model_options.keys()),
|
| 36 |
-
format_func=lambda x: model_options[x],
|
| 37 |
-
index=0 # Step 3.5 Flash هو الافتراضي
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
# إعدادات إضافية
|
| 41 |
-
temperature = st.slider(
|
| 42 |
-
"درجة الإبداع",
|
| 43 |
-
min_value=0.0,
|
| 44 |
-
max_value=1.0,
|
| 45 |
-
value=0.6,
|
| 46 |
-
step=0.1,
|
| 47 |
-
help="قيمة أعلى = إجابات أكثر إبداعاً"
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
max_tokens = st.slider(
|
| 51 |
-
"الحد الأقصى للكلمات",
|
| 52 |
-
min_value=100,
|
| 53 |
-
max_value=2000,
|
| 54 |
-
value=1000,
|
| 55 |
-
step=100
|
| 56 |
-
)
|
| 57 |
-
|
| 58 |
-
st.markdown("---")
|
| 59 |
-
st.markdown("### 📝 معلومات")
|
| 60 |
-
st.markdown(f"**النموذج الحالي:** {selected_model}")
|
| 61 |
-
st.markdown("**السرعة:** 40 طلب/دقيقة كحد أقصى")
|
| 62 |
-
|
| 63 |
-
# زر مسح المحادثة
|
| 64 |
-
if st.button("🗑️ مسح المحادثة", use_container_width=True):
|
| 65 |
-
st.session_state.messages = [
|
| 66 |
-
{"role": "system", "content": "أنت مساعد ذكي ومفيد. أجب باللغة العربية بشكل دقيق ومفيد."}
|
| 67 |
-
]
|
| 68 |
-
st.rerun()
|
| 69 |
-
|
| 70 |
-
# التحقق من وجود المفتاح
|
| 71 |
-
if not api_key or api_key == "":
|
| 72 |
-
st.warning("⚠️ الرجاء إدخال مفتاح API في الشريط الجانبي")
|
| 73 |
-
st.stop()
|
| 74 |
|
| 75 |
# تهيئة العميل
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
api_key=api_key
|
| 81 |
-
)
|
| 82 |
-
|
| 83 |
-
try:
|
| 84 |
-
client = get_client()
|
| 85 |
-
except Exception as e:
|
| 86 |
-
st.error(f"❌ فشل الاتصال بالـ API: {str(e)}")
|
| 87 |
-
st.stop()
|
| 88 |
|
| 89 |
-
# تهيئة
|
| 90 |
if "messages" not in st.session_state:
|
| 91 |
-
st.session_state.messages = [
|
| 92 |
-
{"role": "system", "content": "أنت مساعد ذكي ومفيد. أجب باللغة العربية بشكل دقيق ومفيد."}
|
| 93 |
-
]
|
| 94 |
|
| 95 |
# عرض المحادثة السابقة
|
| 96 |
-
for
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 105 |
-
with st.chat_message("user"):
|
| 106 |
-
st.markdown(prompt)
|
| 107 |
|
| 108 |
-
#
|
| 109 |
with st.chat_message("assistant"):
|
| 110 |
-
|
| 111 |
-
|
| 112 |
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
temperature=temperature,
|
| 121 |
-
max_tokens=max_tokens,
|
| 122 |
-
stream=True
|
| 123 |
-
)
|
| 124 |
-
|
| 125 |
-
# معالجة الـ streaming بشكل آمن
|
| 126 |
-
response_received = False
|
| 127 |
-
try:
|
| 128 |
-
for chunk in completion:
|
| 129 |
-
# التحقق من وجود محتوى في القطعة
|
| 130 |
-
if (chunk.choices and
|
| 131 |
-
len(chunk.choices) > 0 and
|
| 132 |
-
hasattr(chunk.choices[0], 'delta') and
|
| 133 |
-
chunk.choices[0].delta and
|
| 134 |
-
hasattr(chunk.choices[0].delta, 'content') and
|
| 135 |
-
chunk.choices[0].delta.content is not None):
|
| 136 |
-
|
| 137 |
-
content = chunk.choices[0].delta.content
|
| 138 |
-
full_response += content
|
| 139 |
-
message_placeholder.markdown(full_response + "▌")
|
| 140 |
-
time.sleep(0.005) # تأثير كتابي سلس
|
| 141 |
-
response_received = True
|
| 142 |
-
|
| 143 |
-
except Exception as stream_error:
|
| 144 |
-
# تسجيل الخطأ ولكن لا نوقفه
|
| 145 |
-
print(f"⚠️ تحذير التدفق: {stream_error}")
|
| 146 |
-
|
| 147 |
-
# بعد انتهاء التدفق، اعرض الرد الكامل
|
| 148 |
-
if full_response:
|
| 149 |
-
message_placeholder.markdown(full_response)
|
| 150 |
-
elif not response_received:
|
| 151 |
-
# إذا لم يتم استلام أي رد
|
| 152 |
-
error_msg = "⚠️ لم أتمكن من الحصول على رد. قد يكون النموذج غير متاح حالياً."
|
| 153 |
-
message_placeholder.markdown(error_msg)
|
| 154 |
-
full_response = error_msg
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
st.session_state.messages.append({"role": "assistant", "content":
|
| 165 |
-
|
| 166 |
-
# عرض إحصائيات بسيطة في الأسفل
|
| 167 |
-
st.markdown("---")
|
| 168 |
-
col1, col2, col3 = st.columns(3)
|
| 169 |
-
with col1:
|
| 170 |
-
st.caption(f"💬 عدد الرسائل: {len([m for m in st.session_state.messages if m['role'] != 'system'])}")
|
| 171 |
-
with col2:
|
| 172 |
-
st.caption(f"🤖 النموذج: {selected_model.split('/')[-1]}")
|
| 173 |
-
with col3:
|
| 174 |
-
st.caption(f"🎨 الإبداع: {temperature}")
|
|
|
|
| 3 |
import time
|
| 4 |
|
| 5 |
# إعداد الصفحة
|
| 6 |
+
st.set_page_config(page_title="Llama 3.3 70B", page_icon="🦙")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
# العنوان
|
| 9 |
+
st.title("🦙 Llama 3.3 70B")
|
| 10 |
st.markdown("---")
|
| 11 |
|
| 12 |
+
# مفتاح API (ضع مفتاحك هنا مباشرة)
|
| 13 |
+
API_KEY = "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"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# تهيئة العميل
|
| 16 |
+
client = OpenAI(
|
| 17 |
+
base_url="https://integrate.api.nvidia.com/v1",
|
| 18 |
+
api_key=API_KEY
|
| 19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
# تهيئة المحادثة
|
| 22 |
if "messages" not in st.session_state:
|
| 23 |
+
st.session_state.messages = []
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# عرض المحادثة السابقة
|
| 26 |
+
for msg in st.session_state.messages:
|
| 27 |
+
with st.chat_message(msg["role"]):
|
| 28 |
+
st.markdown(msg["content"])
|
| 29 |
+
|
| 30 |
+
# مربع الإدخال
|
| 31 |
+
if prompt := st.chat_input("اكتب سؤالك..."):
|
| 32 |
+
# عرض سؤال المستخدم
|
| 33 |
+
st.chat_message("user").markdown(prompt)
|
| 34 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# رد المساعد
|
| 37 |
with st.chat_message("assistant"):
|
| 38 |
+
msg_placeholder = st.empty()
|
| 39 |
+
full = ""
|
| 40 |
|
| 41 |
+
# استدعاء API (نموذج واحد فقط: Llama 3.3 70B)
|
| 42 |
+
completion = client.chat.completions.create(
|
| 43 |
+
model="meta/llama-3.3-70b-instruct", # ✅ النموذج الوحيد
|
| 44 |
+
messages=[{"role": "user", "content": prompt}],
|
| 45 |
+
temperature=0.5,
|
| 46 |
+
stream=True
|
| 47 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
# تجميع الرد
|
| 50 |
+
for chunk in completion:
|
| 51 |
+
if chunk.choices and chunk.choices[0].delta.content:
|
| 52 |
+
full += chunk.choices[0].delta.content
|
| 53 |
+
msg_placeholder.markdown(full + "▌")
|
| 54 |
+
time.sleep(0.01)
|
| 55 |
+
|
| 56 |
+
msg_placeholder.markdown(full)
|
| 57 |
+
st.session_state.messages.append({"role": "assistant", "content": full})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|