Spaces:
Paused
Paused
Update app.py
Browse files26 unmodified lines27282930303132313233343536353738394041424344404142454647484950515253541 unmodified line5657585059606153626364653 unmodified lines69707163727374757668777879801 unmodified line828384768586788780818889909184859293949596979899100101939410210310410597981061071081091101111121041131141151169 unmodified lines12612712812012913013113213312513413513612813713813913114014114214326 unmodified lines
def load_core_memory(): if os.path.exists(MEMORY_FILE): try: with open(MEMORY_FILE, "r", encoding="utf-8") as f: lines = f.readlines() return "".join(lines[-20:]) with open(MEMORY_FILE, "r", encoding="utf-8") as f: lines = f.readlines() return "".join(lines[-20:]) except: return "Memory access error." return "Initial memory banks empty."
# 🌍 වෙබ් සෙවුම් පද්ධතිය# 🌍 සජීවී සෙවුම් පද්ධතිය (Improved & Stable)def get_web_intel(query): """Hugging Face IP blocks මඟහරවා ගැනීමට 'lite' backend එක භාවිතා කරයි""" try: from duckduckgo_search import DDGS # සෙවුම් පදය සරල කර (Trim) භාවිතා කරයි clean_query = query[:60] with DDGS() as ddgs: results = list(ddgs.text(query, region='wt-wt', max_results=4)) return "\n".join([f"• {r['title']}: {r['body']}" for r in results]) except: return "Web Retrieval Mode: Offline" # backend='lite' යනු shared IPs සඳහා වඩාත් සාර්ථක ක්රමයයි results = list(ddgs.text(clean_query, region='wt-wt', max_results=3, backend='lite')) if results: return "\n".join([f"• {r['title']}: {r['body']}" for r in results]) except Exception as e: print(f"Web Search Log: {e}") return "Web Retrieval: Throttled or Offline. Proceeding with Internal Core Data."
def upload_media(b64): try:1 unmodified line return res.json()['data']['url'] except: return None
# 🔱 Hugging Face Health Check (404 Error එක මඟහරවා ගැනීමට)# 🔱 Hugging Face Health Check@app.route('/')def health_check(): return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME | Node: Stable" return f"🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME | {datetime.now().strftime('%H:%M:%S')}"
@app.route('/api/chat', methods=['POST'])def chat():3 unmodified lines img_b64 = data.get("image_base64") tier = data.get("tier", "prime")
# Time Configuration # Time Configuration (Sri Lanka) sl_tz = pytz.timezone('Asia/Colombo') now = datetime.now(sl_tz) # Self-Learning Detection if any(x in user_msg.lower() for x in ["remember", "මතක තබාගන්න"]): if any(x in user_msg.lower() for x in ["remember", "මතක තබාගන්න", "mthka thba"]): sync_intel_to_memory(user_msg) mem_status = "SYNCHRONIZED" else: mem_status = "READ_ONLY"1 unmodified line current_memory = load_core_memory() img_url = upload_media(img_b64) if img_b64 else None
# 🔱 Sovereign Model Selection (2026 Engine Upgrades) # 🔱 Model Management if img_url: # Gemma 4: Google's 2026 flagship multimodal free model selected_model = "google/gemma-4-31b-it:free" active_tier = "VISION-NODE" web_intel_data = "Image analysis protocol engaged." active_tier = "VISION-CORE" web_intel_data = "Visual input engaged. Bypassing search." else: model_map = { "prime": "nvidia/nemotron-3-super-120b-a12b:free", # 120B High-Reasoning "nexus": "z-ai/glm-4.5-air:free" # 2025 Flagship Air MoE "prime": "nvidia/nemotron-3-super-120b-a12b:free", "nexus": "z-ai/glm-4.5-air:free" } selected_model = model_map.get(tier, "nvidia/nemotron-3-super-120b-a12b:free") active_tier = tier.upper() # සජීවී සෙවුම සිදු කිරීම web_intel_data = get_web_intel(user_msg)
# 🛡️ පද්ධති ප්රොටොකෝලය system_prompt = ( f"SYSTEM_IDENTITY: {active_tier} mode of Elephant AI.\n" f"OPERATOR: MINZO-PRIME / Specialist Team.\n" f"SYSTEM_IDENTITY: {active_tier} mode of Elephant AI by MINZO-PRIME.\n" f"CHRONOS: {now.strftime('%A, %B %d, %Y | %I:%M %p')}\n" f"STRICT_LOCATION: Sri Lanka.\n" f"CORE_MEMORY_BANKS:\n{current_memory}\n" f"WEB_INTEL:\n{web_intel_data}\n" "INSTRUCTION: Use the memory to personalize responses. Be precise and sovereign." f"LIVE_WEB_INTEL:\n{web_intel_data}\n" "INSTRUCTION: Use both memory and web intel to provide authoritative answers." )
headers = { "Authorization": f"Bearer {random.choice(API_KEYS)}", "Content-Type": "application/json", "X-Title": "Elephant AI Sovereign" "HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core" }
# Content Assembly9 unmodified lines "temperature": 0.4 }
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=70) resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=75) resp_data = resp.json()
if 'choices' in resp_data: ai_reply = resp_data['choices'][0]['message']['content'] status_tag = "🔱 [Intel Sync Complete]\n\n" if mem_status == "SYNCHRONIZED" else "" status_tag = "🔱 [Intel Synchronized to Memory]\n\n" if mem_status == "SYNCHRONIZED" else "" return jsonify({"reply": status_tag + ai_reply}) else: return jsonify({"reply": f"Node Error: {resp_data.get('error', {}).get('message', 'Core Overload')}"}), 400 return jsonify({"reply": f"Core Exception: {resp_data.get('error', {}).get('message', 'Unstable Connection')}"}), 400
except Exception as e: return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500 return jsonify({"reply": f"Sovereign Core Disruption: {str(e)}"}), 500
if __name__ == "__main__": app.run(host='0.0.0.0', port=7860)app.py+35-26