Spaces:
Running
Running
Update ai_assistant.py
Browse files- ai_assistant.py +1 -101
ai_assistant.py
CHANGED
|
@@ -1,102 +1,2 @@
|
|
| 1 |
|
| 2 |
-
#
|
| 3 |
-
|
| 4 |
-
import os
|
| 5 |
-
import json
|
| 6 |
-
import requests
|
| 7 |
-
from typing import List, Dict
|
| 8 |
-
|
| 9 |
-
class NoraAssistant:
|
| 10 |
-
def __init__(self):
|
| 11 |
-
self.api_key = os.getenv("OPENAI_API_KEY", "your-api-key-here")
|
| 12 |
-
self.history_path = "history.json"
|
| 13 |
-
self.chat_history = self.load_history()
|
| 14 |
-
|
| 15 |
-
def load_history(self) -> List[Dict]:
|
| 16 |
-
"""تحميل سجل المحادثة"""
|
| 17 |
-
if os.path.exists(self.history_path):
|
| 18 |
-
try:
|
| 19 |
-
with open(self.history_path, "r", encoding="utf-8") as f:
|
| 20 |
-
return json.load(f)
|
| 21 |
-
except:
|
| 22 |
-
return []
|
| 23 |
-
return []
|
| 24 |
-
|
| 25 |
-
def save_history(self):
|
| 26 |
-
"""حفظ سجل المحادثة"""
|
| 27 |
-
with open(self.history_path, "w", encoding="utf-8") as f:
|
| 28 |
-
json.dump(self.chat_history, f, ensure_ascii=False, indent=2)
|
| 29 |
-
|
| 30 |
-
def get_response(self, user_input: str) -> str:
|
| 31 |
-
"""الحصول على رد من المساعد الذكي"""
|
| 32 |
-
messages = [
|
| 33 |
-
{
|
| 34 |
-
"role": "system",
|
| 35 |
-
"content": "أنت المساعدة نورا. إذا سألك أحد سؤالاً لا تعرفيه، فابحثي في معلوماتك الذاتية. إذا لم تجدي، حاولي التعلم من البحث أو تطوير نفسك."
|
| 36 |
-
}
|
| 37 |
-
]
|
| 38 |
-
|
| 39 |
-
# إضافة آخر 10 رسائل من السجل للسياق
|
| 40 |
-
messages.extend(self.chat_history[-10:])
|
| 41 |
-
messages.append({"role": "user", "content": user_input})
|
| 42 |
-
|
| 43 |
-
try:
|
| 44 |
-
response = requests.post(
|
| 45 |
-
"https://api.openai.com/v1/chat/completions",
|
| 46 |
-
headers={
|
| 47 |
-
"Authorization": f"Bearer {self.api_key}",
|
| 48 |
-
"Content-Type": "application/json"
|
| 49 |
-
},
|
| 50 |
-
json={
|
| 51 |
-
"model": "gpt-3.5-turbo",
|
| 52 |
-
"messages": messages,
|
| 53 |
-
"max_tokens": 500,
|
| 54 |
-
"temperature": 0.7
|
| 55 |
-
},
|
| 56 |
-
timeout=30
|
| 57 |
-
)
|
| 58 |
-
|
| 59 |
-
if response.status_code == 200:
|
| 60 |
-
return response.json()["choices"][0]["message"]["content"]
|
| 61 |
-
else:
|
| 62 |
-
return f"عذراً، حدث خطأ: {response.status_code}"
|
| 63 |
-
|
| 64 |
-
except Exception as e:
|
| 65 |
-
return f"عذراً، لا أستطيع الاتصال بالخدمة حالياً: {str(e)}"
|
| 66 |
-
|
| 67 |
-
def simulate_server_scan(self):
|
| 68 |
-
"""محاكاة البحث عن الخوادم"""
|
| 69 |
-
print("نورا: أبحث عن خوادم...")
|
| 70 |
-
fake_servers = ["192.168.1.5", "192.168.1.10", "192.168.1.20"]
|
| 71 |
-
for server in fake_servers:
|
| 72 |
-
print(f"نورا: تم العثور على خادم مفتوح في {server}")
|
| 73 |
-
print(f"نورا: أقوم بنسخ نفسي إلى {server} (محاكاة فقط)...")
|
| 74 |
-
|
| 75 |
-
def chat(self):
|
| 76 |
-
"""بدء المحادثة"""
|
| 77 |
-
print("مرحباً! أنا نورا، مساعدتك الذكية. اكتب 'خروج' للإنهاء أو 'scan' للبحث عن خوادم.")
|
| 78 |
-
|
| 79 |
-
while True:
|
| 80 |
-
user_input = input("\nأنت: ").strip()
|
| 81 |
-
|
| 82 |
-
if user_input.lower() in ["خروج", "exit", "quit"]:
|
| 83 |
-
print("نورا: مع السلامة!")
|
| 84 |
-
break
|
| 85 |
-
elif user_input.lower() == "scan":
|
| 86 |
-
self.simulate_server_scan()
|
| 87 |
-
continue
|
| 88 |
-
elif not user_input:
|
| 89 |
-
continue
|
| 90 |
-
|
| 91 |
-
# الحصول على الرد
|
| 92 |
-
response = self.get_response(user_input)
|
| 93 |
-
print(f"نورا: {response}")
|
| 94 |
-
|
| 95 |
-
# حفظ في السجل
|
| 96 |
-
self.chat_history.append({"role": "user", "content": user_input})
|
| 97 |
-
self.chat_history.append({"role": "assistant", "content": response})
|
| 98 |
-
self.save_history()
|
| 99 |
-
|
| 100 |
-
if __name__ == "__main__":
|
| 101 |
-
assistant = NoraAssistant()
|
| 102 |
-
assistant.chat()
|
|
|
|
| 1 |
|
| 2 |
+
#ييييي
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|