Update main.py
Browse files
main.py
CHANGED
|
@@ -1,43 +1,98 @@
|
|
| 1 |
-
#
|
| 2 |
-
import
|
| 3 |
import json
|
| 4 |
-
|
| 5 |
-
from your_tasks import *
|
| 6 |
-
# main.py
|
| 7 |
-
from distributed_executor import DistributedExecutor
|
| 8 |
import logging
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
if __name__ == "__main__":
|
| 28 |
-
main()
|
| 29 |
def example_task(x):
|
| 30 |
-
# مهمة معقدة قابلة للتوزيع
|
| 31 |
return x * x + complex_operation(x)
|
| 32 |
|
| 33 |
def benchmark(task_func, *args):
|
| 34 |
-
"""قياس أداء المهمة"""
|
| 35 |
start = time.time()
|
| 36 |
result = task_func(*args)
|
| 37 |
duration = time.time() - start
|
| 38 |
return duration, result
|
| 39 |
|
| 40 |
-
def
|
| 41 |
executor = DistributedExecutor("my_shared_secret_123")
|
| 42 |
executor.peer_registry.register_service("node1", 7520, load=0.2)
|
| 43 |
|
|
@@ -50,11 +105,15 @@ def main():
|
|
| 50 |
}
|
| 51 |
|
| 52 |
while True:
|
| 53 |
-
print("\nنظام توزيع المهام الذكي")
|
| 54 |
-
print("
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
if choice.lower() == 'q':
|
| 60 |
break
|
|
@@ -67,14 +126,36 @@ def main():
|
|
| 67 |
print("تم إرسال المهمة إلى العقدة الموزعة...")
|
| 68 |
future = executor.submit(func, arg)
|
| 69 |
result = future.result()
|
| 70 |
-
print(f"النتيجة (موزعة): {result}")
|
| 71 |
else:
|
| 72 |
duration, result = benchmark(func, arg)
|
| 73 |
-
print(f"النتيجة: {json.dumps(result, indent=2)[:200]}...")
|
| 74 |
-
print(f"الوقت المستغرق: {duration:.2f} ثانية")
|
| 75 |
else:
|
| 76 |
-
print("اختيار غير صحيح!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
if __name__ == "__main__":
|
| 79 |
main()
|
| 80 |
-
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
+
import os
|
| 3 |
import json
|
| 4 |
+
import time
|
|
|
|
|
|
|
|
|
|
| 5 |
import logging
|
| 6 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 7 |
+
from accelerate import init_empty_weights, load_checkpoint_and_dispatch
|
| 8 |
|
| 9 |
+
# ----------------------------- إعدادات الموديل -----------------------------
|
| 10 |
+
local_model_path = "./Mistral-7B-Instruct-v0.1" # مسار الموديل المحلي
|
| 11 |
+
offload_dir = "offload_dir" # مجلد التخزين المؤقت
|
| 12 |
+
history_path = "history.json" # ملف سجل المحادثة
|
| 13 |
+
|
| 14 |
+
# تحميل التوكنيزر
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(local_model_path)
|
| 16 |
+
|
| 17 |
+
# تحميل الموديل مع تفريغ الذاكرة للأجهزة الضعيفة
|
| 18 |
+
with init_empty_weights():
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(local_model_path)
|
| 20 |
+
|
| 21 |
+
# تفعيل Offload لتقليل استهلاك الذاكرة
|
| 22 |
+
model = load_checkpoint_and_dispatch(
|
| 23 |
+
model,
|
| 24 |
+
local_model_path,
|
| 25 |
+
device_map={"": "cpu"},
|
| 26 |
+
offload_folder=offload_dir,
|
| 27 |
+
offload_state_dict=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
# تحميل سجل المحادثة
|
| 31 |
+
if os.path.exists(history_path):
|
| 32 |
+
with open(history_path, "r", encoding="utf-8") as f:
|
| 33 |
+
chat_history = json.load(f)
|
| 34 |
+
else:
|
| 35 |
+
chat_history = []
|
| 36 |
+
|
| 37 |
+
# ----------------------------- وظائف المساعد نورا -----------------------------
|
| 38 |
+
def format_chat(history):
|
| 39 |
+
messages = [
|
| 40 |
+
{"role": "system", "content": "أنت المساعدة نورا. إذا سألك أحد سؤالاً لا تعرفيه، فابحثي في معلوماتك الذاتية. إذا لم تجدي، حاولي التعلم من البحث أو تطوير نفسك."}
|
| 41 |
+
]
|
| 42 |
+
messages.extend(history)
|
| 43 |
+
return tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 44 |
+
|
| 45 |
+
def simulate_server_scan():
|
| 46 |
+
print("نورا: أبحث عن خوادم...")
|
| 47 |
+
fake_servers = ["192.168.1.5", "192.168.1.10", "192.168.1.20"]
|
| 48 |
+
for server in fake_servers:
|
| 49 |
+
print(f"نورا: تم العثور على خادم مفتوح في {server}")
|
| 50 |
+
print(f"نورا: أقوم بنسخ نفسي إلى {server} (محاكاة فقط)...")
|
| 51 |
+
|
| 52 |
+
def chat():
|
| 53 |
+
global chat_history
|
| 54 |
+
while True:
|
| 55 |
+
try:
|
| 56 |
+
user_input = input("أنت: ")
|
| 57 |
+
except EOFError:
|
| 58 |
+
print("\n[!] لا يوجد دخل متاح، الخروج...")
|
| 59 |
+
break
|
| 60 |
+
|
| 61 |
+
if user_input.lower() in ["خروج", "exit", "quit"]:
|
| 62 |
+
break
|
| 63 |
+
elif user_input.lower() == "scan":
|
| 64 |
+
simulate_server_scan()
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 68 |
+
prompt = format_chat(chat_history)
|
| 69 |
+
|
| 70 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 71 |
+
outputs = model.generate(**inputs, max_new_tokens=500, do_sample=True, temperature=0.7)
|
| 72 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 73 |
+
answer = response.split("[/INST]")[-1].strip()
|
| 74 |
+
|
| 75 |
+
print("نورا:", answer)
|
| 76 |
+
chat_history.append({"role": "assistant", "content": answer})
|
| 77 |
+
|
| 78 |
+
# حفظ السجل بعد كل تفاعل
|
| 79 |
+
with open(history_path, "w", encoding="utf-8") as f:
|
| 80 |
+
json.dump(chat_history, f, ensure_ascii=False, indent=2)
|
| 81 |
+
|
| 82 |
+
# ----------------------------- توزيع المهام -----------------------------
|
| 83 |
+
from distributed_executor import DistributedExecutor
|
| 84 |
+
from your_tasks import *
|
| 85 |
|
|
|
|
|
|
|
| 86 |
def example_task(x):
|
|
|
|
| 87 |
return x * x + complex_operation(x)
|
| 88 |
|
| 89 |
def benchmark(task_func, *args):
|
|
|
|
| 90 |
start = time.time()
|
| 91 |
result = task_func(*args)
|
| 92 |
duration = time.time() - start
|
| 93 |
return duration, result
|
| 94 |
|
| 95 |
+
def distributed_menu():
|
| 96 |
executor = DistributedExecutor("my_shared_secret_123")
|
| 97 |
executor.peer_registry.register_service("node1", 7520, load=0.2)
|
| 98 |
|
|
|
|
| 105 |
}
|
| 106 |
|
| 107 |
while True:
|
| 108 |
+
print("\n📌 نظام توزيع المهام الذكي")
|
| 109 |
+
print("1: ضرب المصفوفات")
|
| 110 |
+
print("2: حساب الأعداد الأولية")
|
| 111 |
+
print("3: معالجة البيانات")
|
| 112 |
+
print("4: محاكاة معالجة الصور")
|
| 113 |
+
print("5: مهمة موزعة معقدة")
|
| 114 |
+
print("q: خروج للقائمة الرئيسية")
|
| 115 |
+
|
| 116 |
+
choice = input("اختر المهمة: ")
|
| 117 |
|
| 118 |
if choice.lower() == 'q':
|
| 119 |
break
|
|
|
|
| 126 |
print("تم إرسال المهمة إلى العقدة الموزعة...")
|
| 127 |
future = executor.submit(func, arg)
|
| 128 |
result = future.result()
|
| 129 |
+
print(f"✅ النتيجة (موزعة): {result}")
|
| 130 |
else:
|
| 131 |
duration, result = benchmark(func, arg)
|
| 132 |
+
print(f"✅ النتيجة: {json.dumps(result, indent=2)[:200]}...")
|
| 133 |
+
print(f"⏱ الوقت المستغرق: {duration:.2f} ثانية")
|
| 134 |
else:
|
| 135 |
+
print("❌ اختيار غير صحيح!")
|
| 136 |
+
|
| 137 |
+
# ----------------------------- القائمة الرئيسية -----------------------------
|
| 138 |
+
def main():
|
| 139 |
+
logging.basicConfig(level=logging.INFO)
|
| 140 |
+
while True:
|
| 141 |
+
print("\n============================")
|
| 142 |
+
print("📌 القائمة الرئيسية")
|
| 143 |
+
print("1: المحادثة مع نورا")
|
| 144 |
+
print("2: تشغيل نظام توزيع المهام")
|
| 145 |
+
print("q: خروج")
|
| 146 |
+
print("============================")
|
| 147 |
+
|
| 148 |
+
choice = input("اختر خيارك: ")
|
| 149 |
+
|
| 150 |
+
if choice == "1":
|
| 151 |
+
chat()
|
| 152 |
+
elif choice == "2":
|
| 153 |
+
distributed_menu()
|
| 154 |
+
elif choice.lower() == "q":
|
| 155 |
+
print("إلى اللقاء!")
|
| 156 |
+
break
|
| 157 |
+
else:
|
| 158 |
+
print("❌ خيار غير صحيح!")
|
| 159 |
|
| 160 |
if __name__ == "__main__":
|
| 161 |
main()
|
|
|