Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,24 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
import requests
|
| 4 |
-
import base64
|
| 5 |
from flask import Flask, request, jsonify
|
| 6 |
from flask_cors import CORS
|
| 7 |
from datetime import datetime
|
| 8 |
-
import pytz
|
| 9 |
-
|
| 10 |
-
# 🔱 Stable Web Search Integration
|
| 11 |
-
try:
|
| 12 |
-
from duckduckgo_search import DDGS
|
| 13 |
-
except ImportError:
|
| 14 |
-
os.system('pip install duckduckgo_search')
|
| 15 |
-
from duckduckgo_search import DDGS
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
| 18 |
CORS(app)
|
| 19 |
|
| 20 |
-
#
|
| 21 |
IMGBB_KEY = "c32f54279bcd7d4f766b5eac37fd7327"
|
|
|
|
| 22 |
API_KEYS = [
|
| 23 |
"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",
|
| 24 |
"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",
|
|
@@ -27,26 +20,31 @@ API_KEYS = [
|
|
| 27 |
"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"
|
| 28 |
]
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
try:
|
|
|
|
| 34 |
with DDGS() as ddgs:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
for r in results:
|
| 39 |
-
intel_data += f"\n- {r['title']}: {r['body']}"
|
| 40 |
-
else: intel_data = "No live updates found for this query."
|
| 41 |
-
except Exception as e:
|
| 42 |
-
intel_data = f"Web Retrieval currently throttled. Proceed with internal knowledge."
|
| 43 |
-
return intel_data
|
| 44 |
|
| 45 |
-
def
|
| 46 |
-
"""Internal Image Hosting Logic"""
|
| 47 |
try:
|
| 48 |
-
|
| 49 |
-
res = requests.post("https://api.imgbb.com/1/upload", data=payload)
|
| 50 |
return res.json()['data']['url']
|
| 51 |
except: return None
|
| 52 |
|
|
@@ -55,76 +53,80 @@ def chat():
|
|
| 55 |
try:
|
| 56 |
data = request.json
|
| 57 |
user_msg = data.get("message", "")
|
| 58 |
-
|
| 59 |
tier = data.get("tier", "prime")
|
| 60 |
|
| 61 |
-
#
|
| 62 |
-
|
| 63 |
-
now = datetime.now(
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
else:
|
| 76 |
-
|
| 77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
active_tier = tier.upper()
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
headers = {
|
| 82 |
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 83 |
"Content-Type": "application/json",
|
| 84 |
-
"HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core"
|
| 85 |
-
"X-Title": "Elephant AI Sovereign Node"
|
| 86 |
}
|
| 87 |
|
| 88 |
-
#
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
f"STRICT SYSTEM DATA:\n"
|
| 92 |
-
f"- Current Date: {current_date}\n"
|
| 93 |
-
f"- Current Time: {current_time}\n"
|
| 94 |
-
f"- Location: Sri Lanka\n"
|
| 95 |
-
f"LIVE WEB CONTEXT: {web_context}\n"
|
| 96 |
-
"INSTRUCTION: Always use the 'STRICT SYSTEM DATA' for time/date queries. "
|
| 97 |
-
"Never claim you don't know the date. Maintain your sovereign identity."
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
-
content = [{"type": "text", "text": user_msg}]
|
| 101 |
-
if final_image_url:
|
| 102 |
-
content.append({"type": "image_url", "image_url": {"url": final_image_url}})
|
| 103 |
|
| 104 |
payload = {
|
| 105 |
"model": selected_model,
|
| 106 |
"messages": [
|
| 107 |
-
{"role": "system", "content":
|
| 108 |
-
{"role": "user", "content":
|
| 109 |
],
|
| 110 |
-
"temperature": 0.
|
| 111 |
}
|
| 112 |
|
| 113 |
-
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=
|
| 114 |
-
|
| 115 |
|
| 116 |
-
if
|
| 117 |
-
|
|
|
|
|
|
|
| 118 |
else:
|
| 119 |
-
|
| 120 |
-
return jsonify({"reply": f"Core Disruption: {error_info}"}), resp.status_code
|
| 121 |
|
| 122 |
except Exception as e:
|
| 123 |
-
return jsonify({"reply": f"Sovereign Core
|
| 124 |
-
|
| 125 |
-
@app.route('/')
|
| 126 |
-
def health():
|
| 127 |
-
return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME | Node: Stable"
|
| 128 |
|
| 129 |
if __name__ == "__main__":
|
| 130 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
import requests
|
|
|
|
| 4 |
from flask import Flask, request, jsonify
|
| 5 |
from flask_cors import CORS
|
| 6 |
from datetime import datetime
|
| 7 |
+
import pytz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app)
|
| 11 |
|
| 12 |
+
# 🔱 පද්ධති වින්යාසය (Security & Memory)
|
| 13 |
IMGBB_KEY = "c32f54279bcd7d4f766b5eac37fd7327"
|
| 14 |
+
MEMORY_FILE = "elephant_core_brain.txt"
|
| 15 |
API_KEYS = [
|
| 16 |
"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",
|
| 17 |
"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",
|
|
|
|
| 20 |
"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"
|
| 21 |
]
|
| 22 |
|
| 23 |
+
# 🧠 දිගුකාලීන මතකය (Persistent Intelligence Sync)
|
| 24 |
+
def sync_intel_to_memory(data):
|
| 25 |
+
with open(MEMORY_FILE, "a", encoding="utf-8") as f:
|
| 26 |
+
f.write(f"|SYNC_DATETIME: {datetime.now().strftime('%Y-%m-%d %H:%M')}| {data}\n")
|
| 27 |
+
|
| 28 |
+
def load_core_memory():
|
| 29 |
+
if os.path.exists(MEMORY_FILE):
|
| 30 |
+
with open(MEMORY_FILE, "r", encoding="utf-8") as f:
|
| 31 |
+
lines = f.readlines()
|
| 32 |
+
# පද්ධතියේ ක්රියාකාරීත්වය වේගවත් කිරීමට අවසන් කරුණු 20 මතකයට ගනී
|
| 33 |
+
return "".join(lines[-20:])
|
| 34 |
+
return "Initial memory banks empty. Awaiting Specialist input."
|
| 35 |
+
|
| 36 |
+
# 🌍 වෙබ් සෙවුම් පද්ධතිය (Enhanced Web Search)
|
| 37 |
+
def get_web_intel(query):
|
| 38 |
try:
|
| 39 |
+
from duckduckgo_search import DDGS
|
| 40 |
with DDGS() as ddgs:
|
| 41 |
+
results = list(ddgs.text(query, region='wt-wt', max_results=4))
|
| 42 |
+
return "\n".join([f"• {r['title']}: {r['body']}" for r in results])
|
| 43 |
+
except: return "Web Retrieval Mode: Offline (Internal Knowledge Only)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
def upload_media(b64):
|
|
|
|
| 46 |
try:
|
| 47 |
+
res = requests.post("https://api.imgbb.com/1/upload", data={"key": IMGBB_KEY, "image": b64})
|
|
|
|
| 48 |
return res.json()['data']['url']
|
| 49 |
except: return None
|
| 50 |
|
|
|
|
| 53 |
try:
|
| 54 |
data = request.json
|
| 55 |
user_msg = data.get("message", "")
|
| 56 |
+
img_b64 = data.get("image_base64")
|
| 57 |
tier = data.get("tier", "prime")
|
| 58 |
|
| 59 |
+
# 🕒 Timezone Management (Sri Lanka Standard)
|
| 60 |
+
sl_tz = pytz.timezone('Asia/Colombo')
|
| 61 |
+
now = datetime.now(sl_tz)
|
| 62 |
+
|
| 63 |
+
# 🧠 Self-Learning logic (Command: Remember/මතක තබාගන්න)
|
| 64 |
+
if any(x in user_msg.lower() for x in ["remember", "මතක තබාගන්න", "mthka thba ganna"]):
|
| 65 |
+
sync_intel_to_memory(user_msg)
|
| 66 |
+
mem_status = "SYNCHRONIZED"
|
| 67 |
+
else: mem_status = "READ_ONLY"
|
| 68 |
+
|
| 69 |
+
current_memory = load_core_memory()
|
| 70 |
+
img_url = upload_media(img_b64) if img_b64 else None
|
| 71 |
+
|
| 72 |
+
# 🔱 Sovereign Model Selection (Dynamic Multi-Engine)
|
| 73 |
+
if img_url:
|
| 74 |
+
# Vision සඳහා දැනට තිබෙන හොඳම Free Model එක
|
| 75 |
+
selected_model = "google/gemini-2.0-flash-exp:free"
|
| 76 |
+
active_tier = "VISION-NODE"
|
| 77 |
+
web_intel_data = "Image analysis protocol engaged."
|
| 78 |
else:
|
| 79 |
+
# 🔱 ඔබ සොයාගත් අලුත්ම සහ ප්රබලම මොඩල් දෙක මෙතැනට!
|
| 80 |
+
model_map = {
|
| 81 |
+
"prime": "nvidia/nemotron-3-super-120b-a12b:free", # බලවත් 120B Model එක
|
| 82 |
+
"nexus": "z-ai/glm-4.5-air:free" # වේගවත් සහ Thinking AI එක
|
| 83 |
+
}
|
| 84 |
+
selected_model = model_map.get(tier, "nvidia/nemotron-3-super-120b-a12b:free")
|
| 85 |
active_tier = tier.upper()
|
| 86 |
+
web_intel_data = get_web_intel(user_msg)
|
| 87 |
+
|
| 88 |
+
# 🛡️ පද්ධති ප්රොටොකෝලය (Master Prompt)
|
| 89 |
+
system_prompt = (
|
| 90 |
+
f"SYSTEM_IDENTITY: {active_tier} mode of Elephant AI.\n"
|
| 91 |
+
f"OPERATOR: MINZO-PRIME / Specialist Team.\n"
|
| 92 |
+
f"CHRONOS: {now.strftime('%A, %B %d, %Y | %I:%M %p')}\n"
|
| 93 |
+
f"CORE_MEMORY_BANKS:\n{current_memory}\n"
|
| 94 |
+
f"REAL_TIME_WEB_INTEL:\n{web_intel_data}\n"
|
| 95 |
+
"INSTRUCTION: Use both Core Memory and Web Intel for maximum precision. "
|
| 96 |
+
"Maintain an authoritative, sovereign, and highly intelligent persona."
|
| 97 |
+
)
|
| 98 |
|
| 99 |
headers = {
|
| 100 |
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 101 |
"Content-Type": "application/json",
|
| 102 |
+
"HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core"
|
|
|
|
| 103 |
}
|
| 104 |
|
| 105 |
+
# Content Assembly
|
| 106 |
+
u_content = [{"type": "text", "text": user_msg}]
|
| 107 |
+
if img_url: u_content.append({"type": "image_url", "image_url": {"url": img_url}})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
payload = {
|
| 110 |
"model": selected_model,
|
| 111 |
"messages": [
|
| 112 |
+
{"role": "system", "content": system_prompt},
|
| 113 |
+
{"role": "user", "content": u_content}
|
| 114 |
],
|
| 115 |
+
"temperature": 0.4 # නිරවද්යතාවය (Accuracy) වැඩි කිරීමට
|
| 116 |
}
|
| 117 |
|
| 118 |
+
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=70)
|
| 119 |
+
resp_data = resp.json()
|
| 120 |
|
| 121 |
+
if 'choices' in resp_data:
|
| 122 |
+
ai_reply = resp_data['choices'][0]['message']['content']
|
| 123 |
+
status_tag = "🔱 [Intel Synchronized to Memory Bank]\n\n" if mem_status == "SYNCHRONIZED" else ""
|
| 124 |
+
return jsonify({"reply": status_tag + ai_reply})
|
| 125 |
else:
|
| 126 |
+
return jsonify({"reply": f"Node Error: {resp_data.get('error', {}).get('message', 'Core Overload')}"}), 400
|
|
|
|
| 127 |
|
| 128 |
except Exception as e:
|
| 129 |
+
return jsonify({"reply": f"Sovereign Core Disruption: {str(e)}"}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
if __name__ == "__main__":
|
| 132 |
app.run(host='0.0.0.0', port=7860)
|