Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,12 @@
|
|
| 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 |
API_KEYS = [
|
| 10 |
"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",
|
| 11 |
"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",
|
|
@@ -15,13 +16,15 @@ API_KEYS = [
|
|
| 15 |
]
|
| 16 |
|
| 17 |
def fetch_web_intel(query):
|
|
|
|
| 18 |
intel = ""
|
| 19 |
try:
|
| 20 |
search_url = f'https://news.google.com/rss/search?q={query}&hl=en-US'
|
| 21 |
feed = feedparser.parse(search_url)
|
| 22 |
for entry in feed.entries[:3]:
|
| 23 |
-
intel += f"\n- {entry.title}"
|
| 24 |
-
except:
|
|
|
|
| 25 |
return intel
|
| 26 |
|
| 27 |
@app.route('/api/chat', methods=['POST'])
|
|
@@ -29,27 +32,34 @@ def chat():
|
|
| 29 |
try:
|
| 30 |
data = request.json
|
| 31 |
user_msg = data.get("message", "")
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 35 |
-
|
| 36 |
web_context = fetch_web_intel(user_msg)
|
| 37 |
-
|
| 38 |
headers = {
|
| 39 |
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 40 |
"Content-Type": "application/json",
|
| 41 |
"HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core",
|
| 42 |
"X-Title": "Elephant AI Core"
|
| 43 |
}
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
system_prompt = f"""You are Elephant AI by MINZO-PRIME.
|
| 47 |
-
Current Date and Time: {current_time}.
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
payload = {
|
| 52 |
-
"model":
|
| 53 |
"messages": [
|
| 54 |
{"role": "system", "content": system_prompt},
|
| 55 |
{"role": "user", "content": user_msg}
|
|
@@ -57,18 +67,21 @@ def chat():
|
|
| 57 |
"stream": False
|
| 58 |
}
|
| 59 |
|
| 60 |
-
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
| 61 |
res_data = resp.json()
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
-
return jsonify({"reply": f"
|
| 68 |
|
| 69 |
@app.route('/')
|
| 70 |
def health():
|
| 71 |
-
return "🐘 Elephant AI Core: ONLINE"
|
| 72 |
|
| 73 |
if __name__ == "__main__":
|
| 74 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 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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",
|
|
|
|
| 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 |
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)}",
|
| 50 |
"Content-Type": "application/json",
|
| 51 |
"HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core",
|
| 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,
|
| 63 |
"messages": [
|
| 64 |
{"role": "system", "content": system_prompt},
|
| 65 |
{"role": "user", "content": user_msg}
|
|
|
|
| 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)
|