Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,19 @@ import json
|
|
| 5 |
from flask import Flask, request, jsonify
|
| 6 |
from flask_cors import CORS
|
| 7 |
from datetime import datetime
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,17 +27,16 @@ API_KEYS = [
|
|
| 19 |
]
|
| 20 |
|
| 21 |
def fetch_duckduckgo_intel(query):
|
| 22 |
-
"""Warning-free DuckDuckGo Search Integration"""
|
| 23 |
intel = ""
|
| 24 |
try:
|
| 25 |
-
# 🔱
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
except Exception as e:
|
| 31 |
print(f"Search Warning: {e}")
|
| 32 |
-
intel = "Global intelligence access
|
| 33 |
return intel
|
| 34 |
|
| 35 |
@app.route('/api/chat', methods=['POST'])
|
|
@@ -55,34 +62,22 @@ def chat():
|
|
| 55 |
"X-Title": "Elephant AI Core"
|
| 56 |
}
|
| 57 |
|
| 58 |
-
system_prompt = f"
|
| 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)
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
if 'choices' in res_data:
|
| 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"
|
| 82 |
|
| 83 |
@app.route('/')
|
| 84 |
def health():
|
| 85 |
-
return "🐘 Elephant AI Core: ONLINE
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
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 |
+
|
| 9 |
+
# 🔱 DuckDuckGo Library එක Import කිරීමේදී සිදුවන Error එක වැළැක්වීමට
|
| 10 |
+
try:
|
| 11 |
+
from duckduckgo_search import DDGS
|
| 12 |
+
except ImportError:
|
| 13 |
+
# මෙය Hugging Face වලදී Module එක නැවත තහවුරු කිරීමටයි
|
| 14 |
+
os.system('pip install duckduckgo_search')
|
| 15 |
+
from duckduckgo_search import DDGS
|
| 16 |
|
| 17 |
app = Flask(__name__)
|
| 18 |
CORS(app, resources={r"/api/*": {"origins": "*"}})
|
| 19 |
|
| 20 |
+
# AUTHORIZED API KEYS
|
| 21 |
API_KEYS = [
|
| 22 |
"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",
|
| 23 |
"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",
|
|
|
|
| 27 |
]
|
| 28 |
|
| 29 |
def fetch_duckduckgo_intel(query):
|
|
|
|
| 30 |
intel = ""
|
| 31 |
try:
|
| 32 |
+
# 🔱 Latest version logic
|
| 33 |
+
with DDGS() as ddgs:
|
| 34 |
+
results = ddgs.text(query, max_results=4)
|
| 35 |
+
for r in results:
|
| 36 |
+
intel += f"\n- {r['title']}: {r['body']}"
|
| 37 |
except Exception as e:
|
| 38 |
print(f"Search Warning: {e}")
|
| 39 |
+
intel = "Global intelligence access restricted."
|
| 40 |
return intel
|
| 41 |
|
| 42 |
@app.route('/api/chat', methods=['POST'])
|
|
|
|
| 62 |
"X-Title": "Elephant AI Core"
|
| 63 |
}
|
| 64 |
|
| 65 |
+
system_prompt = f"You are {tier.upper()} mode of Elephant AI by MINZO-PRIME. Time: {current_time}. Data: {web_context}"
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
payload = {
|
| 68 |
"model": selected_model,
|
| 69 |
+
"messages": [{"role": "system", "content": system_prompt}, {"role": "user", "content": user_msg}]
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
|
| 72 |
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload, timeout=60)
|
| 73 |
+
return jsonify({"reply": resp.json()['choices'][0]['message']['content']})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
+
return jsonify({"reply": f"Core Disruption: {str(e)}"}), 500
|
| 77 |
|
| 78 |
@app.route('/')
|
| 79 |
def health():
|
| 80 |
+
return "🐘 Elephant AI Core: ONLINE"
|
| 81 |
|
| 82 |
if __name__ == "__main__":
|
| 83 |
app.run(host='0.0.0.0', port=7860)
|