| import streamlit as st |
| from openai import OpenAI |
| import time |
|
|
| |
| st.set_page_config( |
| page_title="مساعد DeepSeek R1", |
| page_icon="🤖", |
| layout="centered" |
| ) |
|
|
| |
| st.title("🤖 مساعد DeepSeek R1") |
| st.markdown("---") |
|
|
| |
| with st.sidebar: |
| st.header("⚙️ الإعدادات") |
| api_key = st.text_input( |
| "مفتاح API الخاص بك", |
| type="password", |
| value="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", |
| help="يمكنك تغييره أو تركه كما هو" |
| ) |
| |
| st.markdown("---") |
| st.markdown("### 📝 معلومات") |
| st.markdown("هذا التطبيق يستخدم نموذج **DeepSeek-R1** عبر NVIDIA NIM.") |
| st.markdown("السرعة: 40 طلب/دقيقة كحد أقصى") |
|
|
| |
| if not api_key or api_key == "": |
| st.warning("⚠️ الرجاء إدخال مفتاح API في الشريط الجانبي") |
| st.stop() |
|
|
| |
| @st.cache_resource |
| def get_client(): |
| return OpenAI( |
| base_url="https://integrate.api.nvidia.com/v1", |
| api_key=api_key |
| ) |
|
|
| client = get_client() |
|
|
| |
| if "messages" not in st.session_state: |
| st.session_state.messages = [ |
| {"role": "system", "content": "أنت مساعد ذكي ومفيد. أجب باللغة العربية."} |
| ] |
|
|
| |
| for message in st.session_state.messages: |
| if message["role"] != "system": |
| with st.chat_message(message["role"]): |
| st.markdown(message["content"]) |
|
|
| |
| if prompt := st.chat_input("اكتب سؤالك هنا..."): |
| |
| st.session_state.messages.append({"role": "user", "content": prompt}) |
| with st.chat_message("user"): |
| st.markdown(prompt) |
| |
| |
| with st.chat_message("assistant"): |
| message_placeholder = st.empty() |
| full_response = "" |
| |
| try: |
| |
| with st.spinner("جارٍ التفكير..."): |
| |
| completion = client.chat.completions.create( |
| model="deepseek-ai/deepseek-r1", |
| messages=st.session_state.messages, |
| temperature=0.6, |
| stream=True |
| ) |
| |
| |
| for chunk in completion: |
| if chunk.choices[0].delta.content: |
| full_response += chunk.choices[0].delta.content |
| message_placeholder.markdown(full_response + "▌") |
| time.sleep(0.01) |
| |
| message_placeholder.markdown(full_response) |
| |
| except Exception as e: |
| st.error(f"حدث خطأ: {str(e)}") |
| full_response = f"⚠️ خطأ في الاتصال: {str(e)}" |
| message_placeholder.markdown(full_response) |
| |
| |
| st.session_state.messages.append({"role": "assistant", "content": full_response}) |
|
|
| |
| if st.sidebar.button("🗑️ مسح المحادثة"): |
| st.session_state.messages = [ |
| {"role": "system", "content": "أنت مساعد ذكي ومفيد. أجب باللغة العربية."} |
| ] |
| st.rerun() |