Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -27,19 +27,28 @@ def sync_intel_to_memory(data):
|
|
| 27 |
|
| 28 |
def load_core_memory():
|
| 29 |
if os.path.exists(MEMORY_FILE):
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
return "Initial memory banks empty."
|
| 34 |
|
| 35 |
-
# 🌍 ව
|
| 36 |
def get_web_intel(query):
|
|
|
|
| 37 |
try:
|
| 38 |
from duckduckgo_search import DDGS
|
|
|
|
|
|
|
| 39 |
with DDGS() as ddgs:
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
def upload_media(b64):
|
| 45 |
try:
|
|
@@ -47,10 +56,10 @@ def upload_media(b64):
|
|
| 47 |
return res.json()['data']['url']
|
| 48 |
except: return None
|
| 49 |
|
| 50 |
-
# 🔱 Hugging Face Health Check
|
| 51 |
@app.route('/')
|
| 52 |
def health_check():
|
| 53 |
-
return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME |
|
| 54 |
|
| 55 |
@app.route('/api/chat', methods=['POST'])
|
| 56 |
def chat():
|
|
@@ -60,12 +69,12 @@ def chat():
|
|
| 60 |
img_b64 = data.get("image_base64")
|
| 61 |
tier = data.get("tier", "prime")
|
| 62 |
|
| 63 |
-
# Time Configuration
|
| 64 |
sl_tz = pytz.timezone('Asia/Colombo')
|
| 65 |
now = datetime.now(sl_tz)
|
| 66 |
|
| 67 |
# Self-Learning Detection
|
| 68 |
-
if any(x in user_msg.lower() for x in ["remember", "මතක තබාගන්න"]):
|
| 69 |
sync_intel_to_memory(user_msg)
|
| 70 |
mem_status = "SYNCHRONIZED"
|
| 71 |
else: mem_status = "READ_ONLY"
|
|
@@ -73,35 +82,35 @@ def chat():
|
|
| 73 |
current_memory = load_core_memory()
|
| 74 |
img_url = upload_media(img_b64) if img_b64 else None
|
| 75 |
|
| 76 |
-
# 🔱
|
| 77 |
if img_url:
|
| 78 |
-
# Gemma 4: Google's 2026 flagship multimodal free model
|
| 79 |
selected_model = "google/gemma-4-31b-it:free"
|
| 80 |
-
active_tier = "VISION-
|
| 81 |
-
web_intel_data = "
|
| 82 |
else:
|
| 83 |
model_map = {
|
| 84 |
-
"prime": "nvidia/nemotron-3-super-120b-a12b:free",
|
| 85 |
-
"nexus": "z-ai/glm-4.5-air:free"
|
| 86 |
}
|
| 87 |
selected_model = model_map.get(tier, "nvidia/nemotron-3-super-120b-a12b:free")
|
| 88 |
active_tier = tier.upper()
|
|
|
|
| 89 |
web_intel_data = get_web_intel(user_msg)
|
| 90 |
|
| 91 |
# 🛡️ පද්ධති ප්රොටොකෝලය
|
| 92 |
system_prompt = (
|
| 93 |
-
f"SYSTEM_IDENTITY: {active_tier} mode of Elephant AI.\n"
|
| 94 |
-
f"OPERATOR: MINZO-PRIME / Specialist Team.\n"
|
| 95 |
f"CHRONOS: {now.strftime('%A, %B %d, %Y | %I:%M %p')}\n"
|
|
|
|
| 96 |
f"CORE_MEMORY_BANKS:\n{current_memory}\n"
|
| 97 |
-
f"
|
| 98 |
-
"INSTRUCTION: Use
|
| 99 |
)
|
| 100 |
|
| 101 |
headers = {
|
| 102 |
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 103 |
"Content-Type": "application/json",
|
| 104 |
-
"
|
| 105 |
}
|
| 106 |
|
| 107 |
# Content Assembly
|
|
@@ -117,18 +126,18 @@ def chat():
|
|
| 117 |
"temperature": 0.4
|
| 118 |
}
|
| 119 |
|
| 120 |
-
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=
|
| 121 |
resp_data = resp.json()
|
| 122 |
|
| 123 |
if 'choices' in resp_data:
|
| 124 |
ai_reply = resp_data['choices'][0]['message']['content']
|
| 125 |
-
status_tag = "🔱 [Intel
|
| 126 |
return jsonify({"reply": status_tag + ai_reply})
|
| 127 |
else:
|
| 128 |
-
return jsonify({"reply": f"
|
| 129 |
|
| 130 |
except Exception as e:
|
| 131 |
-
return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500
|
| 132 |
|
| 133 |
if __name__ == "__main__":
|
| 134 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 27 |
|
| 28 |
def load_core_memory():
|
| 29 |
if os.path.exists(MEMORY_FILE):
|
| 30 |
+
try:
|
| 31 |
+
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
|
| 32 |
+
lines = f.readlines()
|
| 33 |
+
return "".join(lines[-20:])
|
| 34 |
+
except: return "Memory access error."
|
| 35 |
return "Initial memory banks empty."
|
| 36 |
|
| 37 |
+
# 🌍 සජීවී සෙවුම් පද්ධතිය (Improved & Stable)
|
| 38 |
def get_web_intel(query):
|
| 39 |
+
"""Hugging Face IP blocks මඟහරවා ගැනීමට 'lite' backend එක භාවිතා කරයි"""
|
| 40 |
try:
|
| 41 |
from duckduckgo_search import DDGS
|
| 42 |
+
# සෙවුම් පදය සරල කර (Trim) භාවිතා කරයි
|
| 43 |
+
clean_query = query[:60]
|
| 44 |
with DDGS() as ddgs:
|
| 45 |
+
# backend='lite' යනු shared IPs සඳහා වඩාත් සාර්ථක ක්රමයයි
|
| 46 |
+
results = list(ddgs.text(clean_query, region='wt-wt', max_results=3, backend='lite'))
|
| 47 |
+
if results:
|
| 48 |
+
return "\n".join([f"• {r['title']}: {r['body']}" for r in results])
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Web Search Log: {e}")
|
| 51 |
+
return "Web Retrieval: Throttled or Offline. Proceeding with Internal Core Data."
|
| 52 |
|
| 53 |
def upload_media(b64):
|
| 54 |
try:
|
|
|
|
| 56 |
return res.json()['data']['url']
|
| 57 |
except: return None
|
| 58 |
|
| 59 |
+
# 🔱 Hugging Face Health Check
|
| 60 |
@app.route('/')
|
| 61 |
def health_check():
|
| 62 |
+
return f"🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME | {datetime.now().strftime('%H:%M:%S')}"
|
| 63 |
|
| 64 |
@app.route('/api/chat', methods=['POST'])
|
| 65 |
def chat():
|
|
|
|
| 69 |
img_b64 = data.get("image_base64")
|
| 70 |
tier = data.get("tier", "prime")
|
| 71 |
|
| 72 |
+
# Time Configuration (Sri Lanka)
|
| 73 |
sl_tz = pytz.timezone('Asia/Colombo')
|
| 74 |
now = datetime.now(sl_tz)
|
| 75 |
|
| 76 |
# Self-Learning Detection
|
| 77 |
+
if any(x in user_msg.lower() for x in ["remember", "මතක තබාගන්න", "mthka thba"]):
|
| 78 |
sync_intel_to_memory(user_msg)
|
| 79 |
mem_status = "SYNCHRONIZED"
|
| 80 |
else: mem_status = "READ_ONLY"
|
|
|
|
| 82 |
current_memory = load_core_memory()
|
| 83 |
img_url = upload_media(img_b64) if img_b64 else None
|
| 84 |
|
| 85 |
+
# 🔱 Model Management
|
| 86 |
if img_url:
|
|
|
|
| 87 |
selected_model = "google/gemma-4-31b-it:free"
|
| 88 |
+
active_tier = "VISION-CORE"
|
| 89 |
+
web_intel_data = "Visual input engaged. Bypassing search."
|
| 90 |
else:
|
| 91 |
model_map = {
|
| 92 |
+
"prime": "nvidia/nemotron-3-super-120b-a12b:free",
|
| 93 |
+
"nexus": "z-ai/glm-4.5-air:free"
|
| 94 |
}
|
| 95 |
selected_model = model_map.get(tier, "nvidia/nemotron-3-super-120b-a12b:free")
|
| 96 |
active_tier = tier.upper()
|
| 97 |
+
# සජීවී සෙවුම සිදු කිරීම
|
| 98 |
web_intel_data = get_web_intel(user_msg)
|
| 99 |
|
| 100 |
# 🛡️ පද්ධති ප්රොටොකෝලය
|
| 101 |
system_prompt = (
|
| 102 |
+
f"SYSTEM_IDENTITY: {active_tier} mode of Elephant AI by MINZO-PRIME.\n"
|
|
|
|
| 103 |
f"CHRONOS: {now.strftime('%A, %B %d, %Y | %I:%M %p')}\n"
|
| 104 |
+
f"STRICT_LOCATION: Sri Lanka.\n"
|
| 105 |
f"CORE_MEMORY_BANKS:\n{current_memory}\n"
|
| 106 |
+
f"LIVE_WEB_INTEL:\n{web_intel_data}\n"
|
| 107 |
+
"INSTRUCTION: Use both memory and web intel to provide authoritative answers."
|
| 108 |
)
|
| 109 |
|
| 110 |
headers = {
|
| 111 |
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 112 |
"Content-Type": "application/json",
|
| 113 |
+
"HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core"
|
| 114 |
}
|
| 115 |
|
| 116 |
# Content Assembly
|
|
|
|
| 126 |
"temperature": 0.4
|
| 127 |
}
|
| 128 |
|
| 129 |
+
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=75)
|
| 130 |
resp_data = resp.json()
|
| 131 |
|
| 132 |
if 'choices' in resp_data:
|
| 133 |
ai_reply = resp_data['choices'][0]['message']['content']
|
| 134 |
+
status_tag = "🔱 [Intel Synchronized to Memory]\n\n" if mem_status == "SYNCHRONIZED" else ""
|
| 135 |
return jsonify({"reply": status_tag + ai_reply})
|
| 136 |
else:
|
| 137 |
+
return jsonify({"reply": f"Core Exception: {resp_data.get('error', {}).get('message', 'Unstable Connection')}"}), 400
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
+
return jsonify({"reply": f"Sovereign Core Disruption: {str(e)}"}), 500
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
| 143 |
app.run(host='0.0.0.0', port=7860)
|