Spaces:
Sleeping
Sleeping
File size: 11,136 Bytes
f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f f63af94 8c3fd4f |
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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# =============================================================================
# كود Ollama لـ Hugging Face Space مع Ngrok
# نسخة محسّنة ومستقرة
# =============================================================================
import sys
import subprocess
import time
import os
import signal
import threading
import gradio as gr
# -----------------------------------------------------------------------------
# الجزء الأول: تثبيت المكتبات الضرورية و Ollama
# -----------------------------------------------------------------------------
print("✅ [الخطوة 1/5]: تثبيت المكتبات الضرورية و Ollama...")
sys.stdout.flush()
# تثبيت pyngrok
print(" - تثبيت pyngrok...", end="")
sys.stdout.flush()
try:
subprocess.run([sys.executable, '-m', 'pip', 'install', 'pyngrok', '-q'], check=True)
print(" تم.")
sys.stdout.flush()
except subprocess.CalledProcessError as e:
print(f"\n[❌ خطأ فادح]: فشل تثبيت pyngrok. رمز الخروج: {e.returncode}")
sys.exit(1)
from pyngrok import ngrok
# تثبيت Ollama
print(" - تثبيت Ollama بالطريقة الرسمية...")
sys.stdout.flush()
try:
install_command = "curl -fsSL https://ollama.com/install.sh | sh"
subprocess.run(install_command, shell=True, check=True, capture_output=True, text=True)
print(" - تم تثبيت Ollama بنجاح.")
sys.stdout.flush()
except subprocess.CalledProcessError as e:
print(f"\n[❌ خطأ فادح]: فشل تثبيت Ollama. رمز الخروج: {e.returncode}")
print(e.stderr)
sys.exit(1)
except Exception as e:
print(f"\n[❌ خطأ فادح]: حدث خطأ غير متوقع أثناء تثبيت Ollama. الخطأ: {e}")
sys.exit(1)
print("✅ [الخطوة 1/5]: تم تثبيت المكتبات و Ollama بنجاح!\n")
sys.stdout.flush()
# -----------------------------------------------------------------------------
# الجزء الثاني: إعداد Ngrok وتشغيل خادم Ollama
# -----------------------------------------------------------------------------
print("✅ [الخطوة 2/5]: إعداد Ngrok وتشغيل خادم Ollama...")
sys.stdout.flush()
# استخدم Hugging Face Secrets لتخزين التوكن بشكل آمن
NGROK_AUTH_TOKEN = os.getenv("NGROK_AUTH_TOKEN", "")
try:
if not NGROK_AUTH_TOKEN:
raise ValueError("⚠️ يرجى إضافة NGROK_AUTH_TOKEN في إعدادات Secrets للـ Space")
print(" - إعداد Ngrok Auth Token...", end="")
ngrok.set_auth_token(NGROK_AUTH_TOKEN)
print(" تم.")
sys.stdout.flush()
except Exception as e:
print(f"\n[❌ خطأ فادح]: فشل إعداد Ngrok Auth Token. الخطأ: {e}")
sys.exit(1)
# تشغيل خادم Ollama في الخلفية
print(" - تشغيل خادم Ollama في الخلفية...", end="")
sys.stdout.flush()
os.environ['OLLAMA_HOST'] = '0.0.0.0:11434'
ollama_serve_process = None
try:
ollama_serve_process = subprocess.Popen(
['ollama', 'serve'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
preexec_fn=os.setsid
)
print(" جاري البدء...", end="")
time.sleep(10)
check_process = subprocess.run(['ollama', 'list'], capture_output=True, text=True, timeout=20)
if check_process.returncode == 0:
print(" يعمل بنجاح.")
else:
print(f"\n - [❌] فشل تشغيل خادم Ollama بشكل صحيح.")
stderr_output = ollama_serve_process.stderr.read().decode('utf-8')
print(f" الخطأ من الخادم: {stderr_output.strip()}")
raise Exception("الخادم لم يبدأ بشكل صحيح.")
except Exception as e:
print(f"\n[❌ خطأ فادح]: فشل تشغيل خادم Ollama. الخطأ: {e}")
sys.exit(1)
print("✅ [الخطوة 2/5]: تم تشغيل خادم Ollama بنجاح.\n")
sys.stdout.flush()
# -----------------------------------------------------------------------------
# الجزء الثالث: تشغيل ngrok وإنشاء الرابط العام
# -----------------------------------------------------------------------------
print("✅ [الخطوة 3/5]: جاري تشغيل ngrok لتعريض Ollama API...")
sys.stdout.flush()
ngrok_tunnel = None
public_url_str = None
try:
print(" - إنشاء نفق ngrok...", end="")
ngrok_tunnel = ngrok.connect(11434, "http")
public_url_str = ngrok_tunnel.public_url
print(" تم.")
sys.stdout.flush()
print("\n🔗 الـ API متاح الآن على الرابط العام التالي:")
print(f" {public_url_str}")
print("\n")
sys.stdout.flush()
except Exception as e:
print(f"\n[❌ خطأ فادح]: حدث خطأ أثناء تشغيل ngrok. الخطأ: {e}")
sys.exit(1)
print("✅ [الخطوة 3/5]: تم تشغيل النفق بنجاح وتم عرض الرابط.\n")
sys.stdout.flush()
# -----------------------------------------------------------------------------
# الجزء الرابع: فحص مساحة التخزين وسحب النماذج
# -----------------------------------------------------------------------------
print("✅ [الخطوة 4/5]: فحص مساحة التخزين وسحب النماذج المطلوبة...")
sys.stdout.flush()
print(" - فحص مساحة التخزين المتاحة...")
subprocess.run(['df', '-h', '/'])
print("")
sys.stdout.flush()
models_to_pull = [
"hf.co/Mungert/VibeThinker-1.5B-GGUF:BF16",
"hf.co/Mungert/Qwen3-30B-A1.5B-High-Speed-GGUF:IQ3_M",
]
successfully_pulled = []
failed_to_pull = []
print(" - ستبدأ عملية سحب النماذج بشكل تسلسلي (واحد تلو الآخر) لضمان الاستقرار.")
print(" - ستظهر لك نسبة التقدم لكل نموذج بشكل مباشر. قد يستغرق هذا وقتًا طويلاً.\n")
sys.stdout.flush()
for model_name in models_to_pull:
print(f"--- [⏳] جاري سحب النموذج: {model_name} ---")
sys.stdout.flush()
try:
subprocess.run(
['ollama', 'pull', model_name],
check=True
)
print(f"--- [✔️] تم سحب النموذج {model_name} بنجاح ---\n")
successfully_pulled.append(model_name)
except subprocess.CalledProcessError as e:
print(f"--- [❌] فشل سحب النموذج {model_name}. رمز الخروج: {e.returncode} ---\n")
failed_to_pull.append(model_name)
except Exception as e:
print(f"--- [❌] حدث خطأ استثنائي عند سحب النموذج {model_name}. الخطأ: {e} ---\n")
failed_to_pull.append(model_name)
sys.stdout.flush()
print("✅ [الخطوة 4/5]: انتهت عملية سحب النماذج!\n")
print("--- ملخص عملية السحب ---")
if successfully_pulled:
print(f"✔️ نماذج تم سحبها بنجاح ({len(successfully_pulled)}): {', '.join(successfully_pulled)}")
if failed_to_pull:
print(f"❌ نماذج فشل سحبها ({len(failed_to_pull)}): {', '.join(failed_to_pull)}")
print("-------------------------\n")
sys.stdout.flush()
# -----------------------------------------------------------------------------
# الجزء الخامس: إنشاء واجهة Gradio
# -----------------------------------------------------------------------------
print("✅ [الخطوة 5/5]: إنشاء واجهة Gradio...")
def get_models_list():
"""الحصول على قائمة النماذج المثبتة"""
try:
result = subprocess.run(['ollama', 'list'], capture_output=True, text=True, timeout=10)
return result.stdout
except Exception as e:
return f"❌ خطأ في الحصول على قائمة النماذج: {e}"
# إنشاء محتوى تعليمات الاستخدام
instructions = f"""
# ✨ خادم Ollama جاهز للاستخدام ✨
## 🔗 رابط API العام:
```
{public_url_str}
```
## 📋 تعليمات الاستخدام في RikkaHub:
1. انسخ الرابط أعلاه
2. في RikkaHub، اذهب إلى إعدادات المزود (Provider Settings)
3. اختر نوع المزود 'OpenAI-Compatible'
4. في خانة 'Base URL'، الصق الرابط وأضف له /v1:
```
{public_url_str}/v1
```
5. في خانة 'Model'، اكتب اسم النموذج الذي تريد استخدامه
## 📊 قائمة النماذج المثبتة:
```
{get_models_list()}
```
## ⚠️ ملاحظات مهمة:
- يجب إبقاء هذا Space قيد التشغيل للحفاظ على الاتصال
- الرابط العام سيتغير إذا أعيد تشغيل Space
- استخدم النماذج الخفيفة للحصول على أداء أفضل
"""
# إنشاء واجهة Gradio
with gr.Blocks(title="Ollama Server on Hugging Face", theme=gr.themes.Soft()) as demo:
gr.Markdown(instructions)
with gr.Row():
refresh_btn = gr.Button("🔄 تحديث قائمة النماذج", size="sm")
models_output = gr.Textbox(
label="النماذج المثبتة حاليًا",
value=get_models_list(),
lines=10,
interactive=False
)
refresh_btn.click(fn=get_models_list, outputs=models_output)
gr.Markdown("""
---
### 💡 نصائح للاستخدام الأمثل:
- استخدم النماذج الصغيرة (1B-3B) للحصول على استجابة سريعة
- تأكد من استقرار الاتصال بالإنترنت
- راقب استخدام الذاكرة في Logs
""")
print("✅ تم إنشاء واجهة Gradio بنجاح!")
print("⏳ الخادم يعمل الآن ومتاح للاستخدام...")
sys.stdout.flush()
# إطلاق Gradio في thread منفصل للسماح بالتشغيل المستمر
if __name__ == "__main__":
try:
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True
)
except KeyboardInterrupt:
print('\n\n⏳ تم طلب الإيقاف. جاري إغلاق العمليات...')
finally:
if ollama_serve_process:
try:
pgid = os.getpgid(ollama_serve_process.pid)
os.killpg(pgid, signal.SIGTERM)
print(" - تم إيقاف خادم Ollama.")
except OSError:
print(" - ⚠️ لم يتم العثور على عملية خادم Ollama.")
if ngrok_tunnel:
try:
ngrok.disconnect(public_url_str)
print(" - تم إيقاف نفق ngrok.")
except Exception as e:
print(f" - ⚠️ فشل إيقاف ngrok: {e}")
try:
ngrok.kill()
print(" - تم إيقاف ngrok daemon.")
except Exception as e:
print(f" - ⚠️ فشل إيقاف ngrok daemon: {e}")
print('✅ تم إيقاف جميع العمليات بنجاح.') |