Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
import os, random, requests, json, feedparser
|
| 2 |
from flask import Flask, request, jsonify
|
| 3 |
from flask_cors import CORS
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
CORS(app)
|
| 7 |
|
| 8 |
-
# 🔱 AUTHORIZED API KEYS
|
| 9 |
API_KEYS = [
|
| 10 |
"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",
|
| 11 |
"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",
|
|
@@ -15,17 +15,13 @@ API_KEYS = [
|
|
| 15 |
]
|
| 16 |
|
| 17 |
def fetch_web_intel(query):
|
| 18 |
-
"""අන්තර්ජාලයෙන් නවතම තොරතුරු ලබා ගැනීම (RSS Intelligence)"""
|
| 19 |
intel = ""
|
| 20 |
try:
|
| 21 |
-
# Google News RSS හරහා සෙවීම
|
| 22 |
search_url = f'https://news.google.com/rss/search?q={query}&hl=en-US'
|
| 23 |
feed = feedparser.parse(search_url)
|
| 24 |
-
# මුල් ලිපි 3ක තොරතුරු එකතු කිරීම
|
| 25 |
for entry in feed.entries[:3]:
|
| 26 |
-
intel += f"\n-
|
| 27 |
-
except
|
| 28 |
-
print(f"Web Intel Error: {e}")
|
| 29 |
return intel
|
| 30 |
|
| 31 |
@app.route('/api/chat', methods=['POST'])
|
|
@@ -34,8 +30,9 @@ def chat():
|
|
| 34 |
data = request.json
|
| 35 |
user_msg = data.get("message", "")
|
| 36 |
|
| 37 |
-
# 1. 🔱
|
| 38 |
-
|
|
|
|
| 39 |
web_context = fetch_web_intel(user_msg)
|
| 40 |
|
| 41 |
headers = {
|
|
@@ -45,11 +42,11 @@ def chat():
|
|
| 45 |
"X-Title": "Elephant AI Core"
|
| 46 |
}
|
| 47 |
|
| 48 |
-
# 2. 🔱
|
| 49 |
-
system_prompt = f"""You are Elephant AI
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
payload = {
|
| 55 |
"model": "minimax/minimax-m2.5:free",
|
|
@@ -60,27 +57,18 @@ def chat():
|
|
| 60 |
"stream": False
|
| 61 |
}
|
| 62 |
|
| 63 |
-
resp = requests.post(
|
| 64 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
| 65 |
-
headers=headers,
|
| 66 |
-
json=payload,
|
| 67 |
-
timeout=45
|
| 68 |
-
)
|
| 69 |
-
|
| 70 |
res_data = resp.json()
|
| 71 |
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
return jsonify({"reply": ai_reply})
|
| 75 |
-
else:
|
| 76 |
-
return jsonify({"reply": "Core Error: Failed to retrieve intelligence from OpenRouter."}), 500
|
| 77 |
|
| 78 |
except Exception as e:
|
| 79 |
-
return jsonify({"reply": f"Internal
|
| 80 |
|
| 81 |
@app.route('/')
|
| 82 |
def health():
|
| 83 |
-
return "🐘 Elephant AI Core: ONLINE
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
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 |
API_KEYS = [
|
| 10 |
"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",
|
| 11 |
"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",
|
|
|
|
| 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: pass
|
|
|
|
| 25 |
return intel
|
| 26 |
|
| 27 |
@app.route('/api/chat', methods=['POST'])
|
|
|
|
| 30 |
data = request.json
|
| 31 |
user_msg = data.get("message", "")
|
| 32 |
|
| 33 |
+
# 1. 🔱 සැබෑ දිනය සහ වේලාව ලබා ගැනීම
|
| 34 |
+
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 35 |
+
|
| 36 |
web_context = fetch_web_intel(user_msg)
|
| 37 |
|
| 38 |
headers = {
|
|
|
|
| 42 |
"X-Title": "Elephant AI Core"
|
| 43 |
}
|
| 44 |
|
| 45 |
+
# 2. 🔱 AI එකට වර්තමාන දිනය සහ තොරතුරු ලබා දීම
|
| 46 |
+
system_prompt = f"""You are Elephant AI by MINZO-PRIME.
|
| 47 |
+
Current Date and Time: {current_time}.
|
| 48 |
+
Real-time Web Context: {web_context}
|
| 49 |
+
If the user asks about the date or time, use the information provided above."""
|
| 50 |
|
| 51 |
payload = {
|
| 52 |
"model": "minimax/minimax-m2.5:free",
|
|
|
|
| 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 |
+
ai_reply = res_data['choices'][0]['message']['content']
|
| 64 |
+
return jsonify({"reply": ai_reply})
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
except Exception as e:
|
| 67 |
+
return jsonify({"reply": f"Internal Error: {str(e)}"}), 500
|
| 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)
|