minzo456 commited on
Commit
f54aab9
·
verified ·
1 Parent(s): e9a5410

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -21
app.py CHANGED
@@ -5,8 +5,9 @@ import base64
5
  from flask import Flask, request, jsonify
6
  from flask_cors import CORS
7
  from datetime import datetime
 
8
 
9
- # 🔱 Stable DDGS Integration
10
  try:
11
  from duckduckgo_search import DDGS
12
  except ImportError:
@@ -27,21 +28,24 @@ API_KEYS = [
27
  ]
28
 
29
  def search_intel(query):
30
- """RAG-style web retrieval"""
31
  intel_data = ""
32
  try:
33
  with DDGS() as ddgs:
34
- results = list(ddgs.text(query, region='wt-wt', max_results=4))
 
35
  if results:
36
  for r in results:
37
  intel_data += f"\n- {r['title']}: {r['body']}"
38
- except: intel_data = "Web access limited."
 
 
39
  return intel_data
40
 
41
- def upload_to_imgbb(base64_image):
42
- """Backend-side Image Upload to ImgBB"""
43
  try:
44
- payload = {"key": IMGBB_KEY, "image": base64_image}
45
  res = requests.post("https://api.imgbb.com/1/upload", data=payload)
46
  return res.json()['data']['url']
47
  except: return None
@@ -51,20 +55,23 @@ def chat():
51
  try:
52
  data = request.json
53
  user_msg = data.get("message", "")
54
- # Frontend එකෙන් 'image_base64' නමින් data එක එවිය යුතුයි
55
  image_base_64 = data.get("image_base64")
56
  tier = data.get("tier", "prime")
57
 
58
- final_image_url = None
59
- if image_base_64:
60
- # පින්තූරයක් තිබේ නම් එය Backend එකෙන් Upload කරයි
61
- final_image_url = upload_to_imgbb(image_base_64)
 
 
 
 
62
 
63
- # 🔱 Model Selection Logic
64
  if final_image_url:
65
  selected_model = "nvidia/llama-nemotron-embed-vl-1b-v2:free"
66
  active_tier = "VISION-CORE"
67
- web_context = "Image analysis mode active."
68
  else:
69
  model_map = {"prime": "minimax/minimax-m2.5:free", "nexus": "tencent/hy3-preview:free"}
70
  selected_model = model_map.get(tier, "minimax/minimax-m2.5:free")
@@ -74,10 +81,22 @@ def chat():
74
  headers = {
75
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
76
  "Content-Type": "application/json",
77
- "HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core"
 
78
  }
79
 
80
- # Payload Assembly
 
 
 
 
 
 
 
 
 
 
 
81
  content = [{"type": "text", "text": user_msg}]
82
  if final_image_url:
83
  content.append({"type": "image_url", "image_url": {"url": final_image_url}})
@@ -85,20 +104,27 @@ def chat():
85
  payload = {
86
  "model": selected_model,
87
  "messages": [
88
- {"role": "system", "content": f"You are {active_tier} mode of Elephant AI by MINZO-PRIME. Web Data: {web_context}"},
89
  {"role": "user", "content": content}
90
- ]
 
91
  }
92
 
93
  resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
94
- return jsonify({"reply": resp.json()['choices'][0]['message']['content']})
 
 
 
 
 
 
95
 
96
  except Exception as e:
97
- return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500
98
 
99
  @app.route('/')
100
  def health():
101
- return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME"
102
 
103
  if __name__ == "__main__":
104
  app.run(host='0.0.0.0', port=7860)
 
5
  from flask import Flask, request, jsonify
6
  from flask_cors import CORS
7
  from datetime import datetime
8
+ import pytz # Timezone සඳහා
9
 
10
+ # 🔱 Stable Web Search Integration
11
  try:
12
  from duckduckgo_search import DDGS
13
  except ImportError:
 
28
  ]
29
 
30
  def search_intel(query):
31
+ """වැඩිදියුණු කළ සෙවුම් පද්ධතිය"""
32
  intel_data = ""
33
  try:
34
  with DDGS() as ddgs:
35
+ # Region එක wt-wt (Global) යොදා block වීම් අවම කරයි
36
+ results = list(ddgs.text(query, region='wt-wt', max_results=5))
37
  if results:
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 upload_to_imgbb(b64_image):
46
+ """Internal Image Hosting Logic"""
47
  try:
48
+ payload = {"key": IMGBB_KEY, "image": b64_image}
49
  res = requests.post("https://api.imgbb.com/1/upload", data=payload)
50
  return res.json()['data']['url']
51
  except: return None
 
55
  try:
56
  data = request.json
57
  user_msg = data.get("message", "")
 
58
  image_base_64 = data.get("image_base64")
59
  tier = data.get("tier", "prime")
60
 
61
+ # 🔱 Time Configuration (Sri Lanka Standard Time)
62
+ lk_tz = pytz.timezone('Asia/Colombo')
63
+ now = datetime.now(lk_tz)
64
+ current_date = now.strftime("%A, %B %d, %Y")
65
+ current_time = now.strftime("%I:%M %p")
66
+
67
+ # Image Handling
68
+ final_image_url = upload_to_imgbb(image_base_64) if image_base_64 else None
69
 
70
+ # Model Logic
71
  if final_image_url:
72
  selected_model = "nvidia/llama-nemotron-embed-vl-1b-v2:free"
73
  active_tier = "VISION-CORE"
74
+ web_context = "Visual mode active. Bypassing search for visual precision."
75
  else:
76
  model_map = {"prime": "minimax/minimax-m2.5:free", "nexus": "tencent/hy3-preview:free"}
77
  selected_model = model_map.get(tier, "minimax/minimax-m2.5:free")
 
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
+ # 🔱 ශක්තිමත් කළ System Prompt එක (AI එකට බොරු කීමට ඉඩ නොදේ)
89
+ system_content = (
90
+ f"You are {active_tier} mode of Elephant AI, engineered by MINZO-PRIME.\n"
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}})
 
104
  payload = {
105
  "model": selected_model,
106
  "messages": [
107
+ {"role": "system", "content": system_content},
108
  {"role": "user", "content": content}
109
+ ],
110
+ "temperature": 0.5
111
  }
112
 
113
  resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
114
+ resp_json = resp.json()
115
+
116
+ if resp.status_code == 200 and 'choices' in resp_json:
117
+ return jsonify({"reply": resp_json['choices'][0]['message']['content']})
118
+ else:
119
+ error_info = resp_json.get('error', {}).get('message', 'Core Node Overload')
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 Exception: {str(e)}"}), 500
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)