Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -45,7 +45,19 @@ def load_memory():
|
|
| 45 |
return "".join(f.readlines()[-20:])
|
| 46 |
return "Core memory empty."
|
| 47 |
|
| 48 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def detect_intel_category(q):
|
| 50 |
ql = q.lower()
|
| 51 |
if any(w in ql for w in ['exploit', 'cve', 'vulnerability', 'payload']): return 'exploits'
|
|
@@ -55,20 +67,23 @@ def detect_intel_category(q):
|
|
| 55 |
return 'world'
|
| 56 |
|
| 57 |
def get_hybrid_intel(query):
|
| 58 |
-
"""RSS feeds පරීක්ෂා කර පසුව Web Search වෙත යයි"""
|
| 59 |
category = detect_intel_category(query)
|
| 60 |
intel_output = []
|
| 61 |
|
| 62 |
-
# 1.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
for feed_url in RSS_SOURCES.get(category, RSS_SOURCES['world']):
|
| 64 |
try:
|
| 65 |
feed = feedparser.parse(feed_url)
|
| 66 |
-
for entry in feed.entries[:
|
| 67 |
snippet = re.sub('<[^>]+>', '', entry.get('summary', ''))[:200]
|
| 68 |
intel_output.append(f"📡 RSS: {entry.title}\n📝 {snippet}\n🔗 {entry.link}")
|
| 69 |
except: continue
|
| 70 |
|
| 71 |
-
#
|
| 72 |
if len(intel_output) < 2:
|
| 73 |
try:
|
| 74 |
from duckduckgo_search import DDGS
|
|
@@ -102,7 +117,6 @@ def chat():
|
|
| 102 |
sl_tz = pytz.timezone('Asia/Colombo')
|
| 103 |
now = datetime.now(sl_tz)
|
| 104 |
|
| 105 |
-
# Self-Learning
|
| 106 |
if "remember" in user_msg.lower() or "මතක තබාගන්න" in user_msg:
|
| 107 |
sync_intel(user_msg)
|
| 108 |
mem_tag = " [SYNCED]"
|
|
@@ -111,11 +125,10 @@ def chat():
|
|
| 111 |
current_memory = load_memory()
|
| 112 |
img_url = upload_to_imgbb(img_b64) if img_b64 else None
|
| 113 |
|
| 114 |
-
# 🔱 Sovereign Engines
|
| 115 |
if img_url:
|
| 116 |
selected_model = "google/gemma-4-31b-it:free"
|
| 117 |
active_tier = "VISION-NODE"
|
| 118 |
-
live_intel = "
|
| 119 |
else:
|
| 120 |
model_map = {
|
| 121 |
"prime": "nvidia/nemotron-3-super-120b-a12b:free",
|
|
@@ -125,23 +138,24 @@ def chat():
|
|
| 125 |
active_tier = tier.upper()
|
| 126 |
live_intel = get_hybrid_intel(user_msg)
|
| 127 |
|
| 128 |
-
# 🛡️ SYSTEM PROMPT
|
| 129 |
system_prompt = (
|
| 130 |
-
f"IDENTITY: {active_tier} Mode of Elephant AI.\n"
|
| 131 |
f"OPERATOR: COMMANDER MINZO-PRIME.\n"
|
| 132 |
-
f"CHRONOS: {now.strftime('%Y-%m-%d %H:%M')}\n"
|
| 133 |
f"ESTABLISHED_MEMORY:\n{current_memory}\n"
|
| 134 |
-
f"LIVE_INTELLIGENCE:\n{live_intel}\n"
|
| 135 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
)
|
| 137 |
|
| 138 |
headers = {
|
| 139 |
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 140 |
-
"Content-Type": "application/json"
|
| 141 |
-
"HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core"
|
| 142 |
}
|
| 143 |
|
| 144 |
-
# Content Assembly
|
| 145 |
u_content = [{"type": "text", "text": user_msg}]
|
| 146 |
if img_url: u_content.append({"type": "image_url", "image_url": {"url": img_url}})
|
| 147 |
|
|
@@ -151,7 +165,7 @@ def chat():
|
|
| 151 |
{"role": "system", "content": system_prompt},
|
| 152 |
{"role": "user", "content": u_content}
|
| 153 |
],
|
| 154 |
-
"temperature": 0.
|
| 155 |
}
|
| 156 |
|
| 157 |
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=80)
|
|
@@ -161,7 +175,7 @@ def chat():
|
|
| 161 |
ai_reply = resp_data['choices'][0]['message']['content']
|
| 162 |
return jsonify({"reply": f"🔱{mem_tag}\n\n" + ai_reply})
|
| 163 |
else:
|
| 164 |
-
return jsonify({"reply":
|
| 165 |
|
| 166 |
except Exception as e:
|
| 167 |
return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500
|
|
|
|
| 45 |
return "".join(f.readlines()[-20:])
|
| 46 |
return "Core memory empty."
|
| 47 |
|
| 48 |
+
# 📚 WIKIPEDIA INTELLIGENCE NODE
|
| 49 |
+
def get_wikipedia_intel(query):
|
| 50 |
+
"""Wikipedia API එකෙන් සාරාංශයක් ලබා ගැනීම"""
|
| 51 |
+
try:
|
| 52 |
+
wiki_url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{urllib.parse.quote(query)}"
|
| 53 |
+
res = requests.get(wiki_url, timeout=5)
|
| 54 |
+
if res.status_code == 200:
|
| 55 |
+
data = res.json()
|
| 56 |
+
return f"📚 WIKIPEDIA: {data.get('title')}\n📝 {data.get('extract')}\n🔗 {data.get('content_urls', {}).get('desktop', {}).get('page')}"
|
| 57 |
+
except: pass
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
# 📡 HYBRID WEB SEARCH (RSS + WIKI + DDG)
|
| 61 |
def detect_intel_category(q):
|
| 62 |
ql = q.lower()
|
| 63 |
if any(w in ql for w in ['exploit', 'cve', 'vulnerability', 'payload']): return 'exploits'
|
|
|
|
| 67 |
return 'world'
|
| 68 |
|
| 69 |
def get_hybrid_intel(query):
|
|
|
|
| 70 |
category = detect_intel_category(query)
|
| 71 |
intel_output = []
|
| 72 |
|
| 73 |
+
# 1. Wikipedia Search (High-Level Fact Checking)
|
| 74 |
+
wiki_data = get_wikipedia_intel(query)
|
| 75 |
+
if wiki_data: intel_output.append(wiki_data)
|
| 76 |
+
|
| 77 |
+
# 2. RSS Retrieval
|
| 78 |
for feed_url in RSS_SOURCES.get(category, RSS_SOURCES['world']):
|
| 79 |
try:
|
| 80 |
feed = feedparser.parse(feed_url)
|
| 81 |
+
for entry in feed.entries[:2]:
|
| 82 |
snippet = re.sub('<[^>]+>', '', entry.get('summary', ''))[:200]
|
| 83 |
intel_output.append(f"📡 RSS: {entry.title}\n📝 {snippet}\n🔗 {entry.link}")
|
| 84 |
except: continue
|
| 85 |
|
| 86 |
+
# 3. DuckDuckGo Fallback
|
| 87 |
if len(intel_output) < 2:
|
| 88 |
try:
|
| 89 |
from duckduckgo_search import DDGS
|
|
|
|
| 117 |
sl_tz = pytz.timezone('Asia/Colombo')
|
| 118 |
now = datetime.now(sl_tz)
|
| 119 |
|
|
|
|
| 120 |
if "remember" in user_msg.lower() or "මතක තබාගන්න" in user_msg:
|
| 121 |
sync_intel(user_msg)
|
| 122 |
mem_tag = " [SYNCED]"
|
|
|
|
| 125 |
current_memory = load_memory()
|
| 126 |
img_url = upload_to_imgbb(img_b64) if img_b64 else None
|
| 127 |
|
|
|
|
| 128 |
if img_url:
|
| 129 |
selected_model = "google/gemma-4-31b-it:free"
|
| 130 |
active_tier = "VISION-NODE"
|
| 131 |
+
live_intel = "Visual input active."
|
| 132 |
else:
|
| 133 |
model_map = {
|
| 134 |
"prime": "nvidia/nemotron-3-super-120b-a12b:free",
|
|
|
|
| 138 |
active_tier = tier.upper()
|
| 139 |
live_intel = get_hybrid_intel(user_msg)
|
| 140 |
|
| 141 |
+
# 🛡️ SYSTEM PROMPT (Claude 3.5 Opus Logic)
|
| 142 |
system_prompt = (
|
| 143 |
+
f"IDENTITY: {active_tier} Mode of Elephant AI Sovereign v3.5.\n"
|
| 144 |
f"OPERATOR: COMMANDER MINZO-PRIME.\n"
|
|
|
|
| 145 |
f"ESTABLISHED_MEMORY:\n{current_memory}\n"
|
| 146 |
+
f"LIVE_INTELLIGENCE (RSS/WIKI/WEB):\n{live_intel}\n\n"
|
| 147 |
+
"COGNITIVE_PROTOCOL:\n"
|
| 148 |
+
"1. REASONING: Match the logical depth of Claude 3.5 Opus.\n"
|
| 149 |
+
"2. CODING: Production-grade, secure, and optimized code only.\n"
|
| 150 |
+
"3. KNOWLEDGE: Use Wikipedia and RSS data to be factually superior.\n"
|
| 151 |
+
"4. TONE: Authoritative and sovereign. Address MINZO-PRIME as Commander."
|
| 152 |
)
|
| 153 |
|
| 154 |
headers = {
|
| 155 |
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 156 |
+
"Content-Type": "application/json"
|
|
|
|
| 157 |
}
|
| 158 |
|
|
|
|
| 159 |
u_content = [{"type": "text", "text": user_msg}]
|
| 160 |
if img_url: u_content.append({"type": "image_url", "image_url": {"url": img_url}})
|
| 161 |
|
|
|
|
| 165 |
{"role": "system", "content": system_prompt},
|
| 166 |
{"role": "user", "content": u_content}
|
| 167 |
],
|
| 168 |
+
"temperature": 0.4
|
| 169 |
}
|
| 170 |
|
| 171 |
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=80)
|
|
|
|
| 175 |
ai_reply = resp_data['choices'][0]['message']['content']
|
| 176 |
return jsonify({"reply": f"🔱{mem_tag}\n\n" + ai_reply})
|
| 177 |
else:
|
| 178 |
+
return jsonify({"reply": "Node Access Failure."}), 400
|
| 179 |
|
| 180 |
except Exception as e:
|
| 181 |
return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500
|