minzo456 commited on
Commit
b81ddb0
·
verified ·
1 Parent(s): 0aac2c7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -23
app.py CHANGED
@@ -20,27 +20,26 @@ API_KEYS = [
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:
@@ -48,6 +47,11 @@ def upload_media(b64):
48
  return res.json()['data']['url']
49
  except: return None
50
 
 
 
 
 
 
51
  @app.route('/api/chat', methods=['POST'])
52
  def chat():
53
  try:
@@ -56,12 +60,12 @@ def chat():
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"
@@ -69,37 +73,35 @@ def chat():
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
@@ -112,7 +114,7 @@ def chat():
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)
@@ -120,13 +122,13 @@ def chat():
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)
 
20
  "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"
21
  ]
22
 
23
+ # 🧠 දිගුකාලීන මතකය (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.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
  return "".join(lines[-20:])
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
  results = list(ddgs.text(query, region='wt-wt', max_results=4))
41
  return "\n".join([f"• {r['title']}: {r['body']}" for r in results])
42
+ except: return "Web Retrieval Mode: Offline"
43
 
44
  def upload_media(b64):
45
  try:
 
47
  return res.json()['data']['url']
48
  except: return None
49
 
50
+ # 🔱 Hugging Face Health Check (404 Error එක මඟහරවා ගැනීමට)
51
+ @app.route('/')
52
+ def health_check():
53
+ return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME | Node: Stable"
54
+
55
  @app.route('/api/chat', methods=['POST'])
56
  def chat():
57
  try:
 
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
  current_memory = load_core_memory()
74
  img_url = upload_media(img_b64) if img_b64 else None
75
 
76
+ # 🔱 Sovereign Model Selection (2026 Engine Upgrades)
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-NODE"
81
  web_intel_data = "Image analysis protocol engaged."
82
  else:
 
83
  model_map = {
84
+ "prime": "nvidia/nemotron-3-super-120b-a12b:free", # 120B High-Reasoning
85
+ "nexus": "z-ai/glm-4.5-air:free" # 2025 Flagship Air MoE
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"WEB_INTEL:\n{web_intel_data}\n"
98
+ "INSTRUCTION: Use the memory to personalize responses. Be precise and sovereign."
 
99
  )
100
 
101
  headers = {
102
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
103
  "Content-Type": "application/json",
104
+ "X-Title": "Elephant AI Sovereign"
105
  }
106
 
107
  # Content Assembly
 
114
  {"role": "system", "content": system_prompt},
115
  {"role": "user", "content": u_content}
116
  ],
117
+ "temperature": 0.4
118
  }
119
 
120
  resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=70)
 
122
 
123
  if 'choices' in resp_data:
124
  ai_reply = resp_data['choices'][0]['message']['content']
125
+ status_tag = "🔱 [Intel Sync Complete]\n\n" if mem_status == "SYNCHRONIZED" else ""
126
  return jsonify({"reply": status_tag + ai_reply})
127
  else:
128
  return jsonify({"reply": f"Node Error: {resp_data.get('error', {}).get('message', 'Core Overload')}"}), 400
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)