├── LLM Flask inference example ├── app.py └── templates │ └── index.html └── README.md /LLM Flask inference example/app.py: -------------------------------------------------------------------------------- 1 | from flask import Flask, request, jsonify, render_template 2 | import subprocess 3 | import os 4 | 5 | app = Flask(__name__) 6 | 7 | # تعديل المسار ليناسب مجلد المشروع الخاص بك 8 | MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 9 | "models/Llama3-8B-1.58-100B-tokens/ggml-model-i2_s.gguf") 10 | 11 | def generate_response(prompt): 12 | try: 13 | cmd = f'python run_inference.py -m "{MODEL_PATH}" -p "{prompt}"' 14 | result = subprocess.run( 15 | cmd, 16 | shell=True, 17 | capture_output=True, 18 | text=True, 19 | timeout=60 20 | ) 21 | print(f"Command output: {result.stdout}") # للتصحيح 22 | print(f"Command errors: {result.stderr}") # للتصحيح 23 | 24 | if result.returncode == 0: 25 | return result.stdout.strip() 26 | else: 27 | return f"Error: {result.stderr}" 28 | except subprocess.TimeoutExpired: 29 | return "Error: Request timed out" 30 | except Exception as e: 31 | return f"Error: {str(e)}" 32 | 33 | @app.route('/') 34 | def home(): 35 | return render_template('index.html') 36 | 37 | @app.route('/generate', methods=['POST']) 38 | def generate(): 39 | data = request.json 40 | prompt = data.get('prompt', '') 41 | 42 | if not prompt: 43 | return jsonify({'error': 'No prompt provided'}), 400 44 | 45 | response = generate_response(prompt) 46 | return jsonify({'response': response}) 47 | 48 | if __name__ == '__main__': 49 | print(f"Using model at: {MODEL_PATH}") # للتأكد من المسار 50 | app.run(debug=True, host='0.0.0.0', port=5000) 51 | 52 | -------------------------------------------------------------------------------- /LLM Flask inference example/templates/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | BitNet LLM Interface 5 | 39 | 40 | 41 |
42 |

BitNet LLM Interface

43 |
44 | 45 |
46 |
47 | 48 | Processing... 49 |
50 |
51 |

Response:

52 |
53 |
54 |
55 | 56 | 88 | 89 | 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## إعداد وتشغيل BitNet على WSL2 2 | ## شاهد الشرح الكامل على يوتيوب! 3 | 4 | لمشاهدة شرح مفصل وعملي لكافة الخطوات، تفضل بزيارة قناتنا على يوتيوب: 5 | 6 | [![شاهد الفيديو](https://img.youtube.com/vi/4-HJOt53LMw/0.jpg)](https://youtu.be/4-HJOt53LMw) 7 | 8 | لا تنسَ الاشتراك في القناة وتفعيل جرس التنبيهات ليصلك كل جديد! ❤️ 9 | 10 | ### الخطوة 1: تثبيت WSL2 11 | 12 | - تفعيل ميزات Windows: 13 | ```powershell 14 | dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart 15 | dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart 16 | ``` 17 | - إعادة تشغيل الجهاز. 18 | - تثبيت WSL2 Linux kernel: [تحميل من موقع مايكروسوفت](https://docs.microsoft.com/en-us/windows/wsl/install-manual#step-4---download-the-linux-kernel-update-package) 19 | - تعيين WSL2 كإصدار افتراضي: 20 | ```powershell 21 | wsl --set-default-version 2 22 | ``` 23 | - تثبيت توزيعة Linux (Ubuntu) من Microsoft Store. 24 | - التحقق من التثبيت: 25 | ```powershell 26 | wsl --version 27 | wsl --list --verbose 28 | ``` 29 | - إعداد Ubuntu: إنشاء اسم مستخدم وكلمة مرور. 30 | 31 | 32 | ### الخطوة 2: إعداد الذاكرة 33 | 34 | - إنشاء `.wslconfig` في `C:\Users\YourUsername\.wslconfig`: 35 | ``` 36 | [wsl2] 37 | memory=12GB 38 | swap=16GB 39 | processors=4 40 | ``` 41 | - إعداد SWAP في Ubuntu: 42 | ```bash 43 | free -h 44 | sudo fallocate -l 16G /swapfile 45 | sudo chmod 600 /swapfile 46 | sudo mkswap /swapfile 47 | sudo swapon /swapfile 48 | ``` 49 | - جعل SWAP دائمة: إضافة ` /swapfile none swap sw 0 0` لـ `/etc/fstab`. 50 | 51 | 52 | ### الخطوة 3: تثبيت الأدوات 53 | 54 | ```bash 55 | sudo apt update 56 | sudo apt upgrade -y 57 | sudo apt install build-essential pkg-config cmake git python3 python3-pip ninja-build -y 58 | wget https://apt.llvm.org/llvm.sh 59 | chmod +x llvm.sh 60 | sudo ./llvm.sh 18 -- --install-dir /usr/lib/llvm-18 61 | echo 'export PATH="/usr/lib/llvm-18/bin:$PATH"' >> ~/.bashrc 62 | source ~/.bashrc 63 | clang --version 64 | ``` 65 | 66 | 67 | ### الخطوة 4: إعداد Conda 68 | 69 | ```bash 70 | wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh 71 | chmod +x Miniconda3-latest-Linux-x86_64.sh 72 | bash Miniconda3-latest-Linux-x86_64.sh -b -p $HOME/miniconda3 73 | echo 'export PATH="$HOME/miniconda3/bin:$PATH"' >> ~/.bashrc 74 | source ~/.bashrc 75 | conda init bash 76 | # إعادة فتح Terminal 77 | conda create -n bitnet-cpp python=3.9 -y 78 | conda activate bitnet-cpp 79 | ``` 80 | 81 | 82 | ### الخطوة 5: تثبيت BitNet 83 | 84 | ```bash 85 | git clone --recursive https://github.com/microsoft/BitNet.git 86 | cd BitNet 87 | pip install -r requirements.txt 88 | ``` 89 | 90 | 91 | ### الخطوة 6: بناء وتشغيل BitNet 92 | 93 | - نموذج صغير: 94 | ```bash 95 | python setup_env.py --hf-repo 1bitLLM/bitnet_b1_58-large -q i2_s 96 | python run_inference.py -m models/bitnet_b1_58-large/ggml-model-i2_s.gguf -p "What is the capital of France?" 97 | ``` 98 | 99 | - نموذج 3B: 100 | ```bash 101 | python setup_env.py --hf-repo 1bitLLM/bitnet_b1_58-3B -q i2_s 102 | python run_inference.py -m models/bitnet_b1_58-3B/ggml-model-i2_s.gguf -p "What is the capital of France?" 103 | ``` 104 | 105 | - نموذج Llama3-8B: 106 | ```bash 107 | sudo sync && sudo echo 3 | sudo tee /proc/sys/vm/drop_caches 108 | free -h 109 | # تعديل .wslconfig إذا لزم الأمر 110 | python setup_env.py --hf-repo HF1BitLLM/Llama3-8B-1.58-100B-tokens -q i2_s 111 | python run_inference.py -m models/Llama3-8B-1.58-100B-tokens/ggml-model-i2_s.gguf -p "Name three types of pets that people commonly keep at home:\nAnswer:" 112 | ``` 113 | - تفعيل بيئة Conda قبل التشغيل: `conda activate bitnet-cpp` 114 | - التأكد من المسار: `cd BitNet` 115 | 116 | ## شاهد الشرح الكامل على يوتيوب! 117 | 118 | لمشاهدة شرح مفصل وعملي لكافة الخطوات، تفضل بزيارة قناتنا على يوتيوب: 119 | 120 | [![شاهد الفيديو](https://img.youtube.com/vi/4-HJOt53LMw/0.jpg)](https://youtu.be/4-HJOt53LMw) 121 | 122 | لا تنسَ الاشتراك في القناة وتفعيل جرس التنبيهات ليصلك كل جديد! ❤️ 123 | 124 | --------------------------------------------------------------------------------