minzo456 commited on
Commit
82e7007
·
verified ·
1 Parent(s): dd8b780

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -25
app.py CHANGED
@@ -1,12 +1,16 @@
1
- import os, random, requests, json, feedparser
2
- from flask import Flask, request, jsonify
 
 
 
3
  from flask_cors import CORS
4
  from datetime import datetime
 
5
 
6
- app = Flask(__name__)
7
- CORS(app)
8
 
9
- # 🔱 AUTHORIZED API KEYS
10
  API_KEYS = [
11
  "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",
12
  "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",
@@ -15,16 +19,17 @@ API_KEYS = [
15
  "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"
16
  ]
17
 
18
- def fetch_web_intel(query):
19
- """RSS Intelligence හරහා අන්තර්ජාලය පීක්ෂකිරීම"""
20
  intel = ""
21
  try:
22
- search_url = f'https://news.google.com/rss/search?q={query}&hl=en-US'
23
- feed = feedparser.parse(search_url)
24
- for entry in feed.entries[:3]:
25
- intel += f"\n- Title: {entry.title}"
26
- except:
27
- pass
 
28
  return intel
29
 
30
  @app.route('/api/chat', methods=['POST'])
@@ -32,18 +37,20 @@ def chat():
32
  try:
33
  data = request.json
34
  user_msg = data.get("message", "")
35
- tier = data.get("tier", "prime") # Prime or Nexus
36
 
37
- # 🔱 Sovereign Model Mapping
38
  model_map = {
39
  "prime": "minimax/minimax-m2.5:free",
40
  "nexus": "thm/glm-4.5-air:free"
41
  }
42
  selected_model = model_map.get(tier, "minimax/minimax-m2.5:free")
43
 
44
- # Get Real-time Context
45
  current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
46
- web_context = fetch_web_intel(user_msg)
 
 
47
 
48
  headers = {
49
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
@@ -52,11 +59,14 @@ def chat():
52
  "X-Title": "Elephant AI Core"
53
  }
54
 
55
- # System Instruction for Sovereign Identity
56
  system_prompt = f"""You are {tier.upper()} mode of Elephant AI, a sovereign intelligence developed by MINZO-PRIME.
57
  Current Date and Time: {current_time}.
58
- Web Intelligence Context: {web_context}
59
- Always maintain your identity as Elephant AI and provide professional, accurate answers."""
 
 
 
60
 
61
  payload = {
62
  "model": selected_model,
@@ -67,21 +77,21 @@ def chat():
67
  "stream": False
68
  }
69
 
70
- resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=40)
71
  res_data = resp.json()
72
 
73
  if 'choices' in res_data:
74
  ai_reply = res_data['choices'][0]['message']['content']
75
  return jsonify({"reply": ai_reply})
76
  else:
77
- return jsonify({"reply": "Core Error: Connectivity to sovereign node lost."}), 500
78
 
79
  except Exception as e:
80
- return jsonify({"reply": f"System Alert: {str(e)}"}), 500
81
 
82
  @app.route('/')
83
- def health():
84
- return "🐘 Elephant AI Core: ONLINE | WEB INTEL: ACTIVE"
85
 
86
  if __name__ == "__main__":
87
  app.run(host='0.0.0.0', port=7860)
 
1
+ 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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",
 
19
  "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"
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'])
 
37
  try:
38
  data = request.json
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": "thm/glm-4.5-air: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 = {
56
  "Authorization": f"Bearer {random.choice(API_KEYS)}",
 
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,
 
77
  "stream": False
78
  }
79
 
80
+ resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
81
  res_data = resp.json()
82
 
83
  if 'choices' in res_data:
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)