minzo456 commited on
Commit
3bcfc7e
·
verified ·
1 Parent(s): 5227e44

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -29
app.py CHANGED
@@ -2,15 +2,14 @@ import os
2
  import random
3
  import requests
4
  import json
5
- from flask import Flask, request, jsonify, send_from_directory
6
  from flask_cors import CORS
7
  from datetime import datetime
8
- from duckduckgo_search import DDGS # 🔱 DuckDuckGo Search Engine
9
 
10
- app = Flask(__name__, static_folder='.')
11
- CORS(app, resources={r"/api/*": {"origins": "*"}}) # 🔱 External Hosting සදහා අවසර දීම
12
 
13
- # AUTHORIZED API KEYS
14
  API_KEYS = [
15
  "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",
16
  "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",
@@ -20,16 +19,17 @@ API_KEYS = [
20
  ]
21
 
22
  def fetch_duckduckgo_intel(query):
23
- """DuckDuckGo හරහා සජීවී වෙබ් තොරතුරු ලබා ගැනීම"""
24
  intel = ""
25
  try:
26
- with DDGS() as ddgs:
27
- results = [r for r in ddgs.text(query, max_results=4)]
28
- for r in results:
29
- intel += f"\n- {r['title']}: {r['body']}"
 
30
  except Exception as e:
31
- print(f"Search Error: {e}")
32
- intel = "Web intelligence currently unavailable."
33
  return intel
34
 
35
  @app.route('/api/chat', methods=['POST'])
@@ -39,17 +39,13 @@ def chat():
39
  user_msg = data.get("message", "")
40
  tier = data.get("tier", "prime")
41
 
42
- # Sovereign Model Mapping
43
  model_map = {
44
  "prime": "minimax/minimax-m2.5:free",
45
  "nexus": "tencent/hy3-preview:free"
46
  }
47
  selected_model = model_map.get(tier, "minimax/minimax-m2.5:free")
48
 
49
- # Get Real-time Metadata
50
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
51
-
52
- # 🔱 අන්තර්ජාලය පරීක්ෂා කිරීම
53
  web_context = fetch_duckduckgo_intel(user_msg)
54
 
55
  headers = {
@@ -59,22 +55,17 @@ def chat():
59
  "X-Title": "Elephant AI Core"
60
  }
61
 
62
- # System Instruction
63
  system_prompt = f"""You are {tier.upper()} mode of Elephant AI, a sovereign intelligence developed by MINZO-PRIME.
64
- Current Date and Time: {current_time}.
65
- Live Web Search Results (DuckDuckGo):
66
- {web_context}
67
-
68
- If the user asks about current events, news, or technical documentation, use the search results above to provide a precise answer.
69
- Always remain in character as Elephant AI."""
70
 
71
  payload = {
72
  "model": selected_model,
73
  "messages": [
74
  {"role": "system", "content": system_prompt},
75
  {"role": "user", "content": user_msg}
76
- ],
77
- "stream": False
78
  }
79
 
80
  resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
@@ -84,14 +75,14 @@ def chat():
84
  ai_reply = res_data['choices'][0]['message']['content']
85
  return jsonify({"reply": ai_reply})
86
  else:
87
- return jsonify({"reply": "Core Alert: Model relay failed. Check API balance or status."}), 500
88
 
89
  except Exception as e:
90
- return jsonify({"reply": f"Internal System Error: {str(e)}"}), 500
91
 
92
  @app.route('/')
93
- def index():
94
- return "🐘 Elephant AI Core API is Running | Web Search: DuckDuckGo Active"
95
 
96
  if __name__ == "__main__":
97
  app.run(host='0.0.0.0', port=7860)
 
2
  import random
3
  import requests
4
  import json
5
+ from flask import Flask, request, jsonify
6
  from flask_cors import CORS
7
  from datetime import datetime
8
+ from duckduckgo_search import DDGS # 🔱 Library එකේ import එක එලෙසම තබන්න
9
 
10
+ app = Flask(__name__)
11
+ CORS(app, resources={r"/api/*": {"origins": "*"}})
12
 
 
13
  API_KEYS = [
14
  "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",
15
  "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",
 
19
  ]
20
 
21
  def fetch_duckduckgo_intel(query):
22
+ """Warning-free DuckDuckGo Search Integration"""
23
  intel = ""
24
  try:
25
+ # 🔱 අලුත් library version එකට ගැලපෙන පරිදි සකස් කළා
26
+ ddgs = DDGS()
27
+ results = ddgs.text(query, max_results=4)
28
+ for r in results:
29
+ intel += f"\n- {r['title']}: {r['body']}"
30
  except Exception as e:
31
+ print(f"Search Warning: {e}")
32
+ intel = "Global intelligence access is restricted for this query."
33
  return intel
34
 
35
  @app.route('/api/chat', methods=['POST'])
 
39
  user_msg = data.get("message", "")
40
  tier = data.get("tier", "prime")
41
 
 
42
  model_map = {
43
  "prime": "minimax/minimax-m2.5:free",
44
  "nexus": "tencent/hy3-preview:free"
45
  }
46
  selected_model = model_map.get(tier, "minimax/minimax-m2.5:free")
47
 
 
48
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
 
 
49
  web_context = fetch_duckduckgo_intel(user_msg)
50
 
51
  headers = {
 
55
  "X-Title": "Elephant AI Core"
56
  }
57
 
 
58
  system_prompt = f"""You are {tier.upper()} mode of Elephant AI, a sovereign intelligence developed by MINZO-PRIME.
59
+ System Time: {current_time}.
60
+ Live Web Data: {web_context}
61
+ Provide strategic and highly accurate responses based on the available data."""
 
 
 
62
 
63
  payload = {
64
  "model": selected_model,
65
  "messages": [
66
  {"role": "system", "content": system_prompt},
67
  {"role": "user", "content": user_msg}
68
+ ]
 
69
  }
70
 
71
  resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
 
75
  ai_reply = res_data['choices'][0]['message']['content']
76
  return jsonify({"reply": ai_reply})
77
  else:
78
+ return jsonify({"reply": "Core Alert: Node response failed. Try Prime mode."}), 500
79
 
80
  except Exception as e:
81
+ return jsonify({"reply": f"Internal Core Disruption: {str(e)}"}), 500
82
 
83
  @app.route('/')
84
+ def health():
85
+ return "🐘 Elephant AI Core: ONLINE | Operator: MINZO-PRIME"
86
 
87
  if __name__ == "__main__":
88
  app.run(host='0.0.0.0', port=7860)