Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,36 @@
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
import requests
|
|
|
|
|
|
|
| 4 |
from flask import Flask, request, Response, stream_with_context
|
| 5 |
from flask_cors import CORS
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
-
# 🔱 ලෝකයේ ඕනෑම තැනක සිට එන HTML එකකට සම්බන්ධ වීමට ඉඩ දීම
|
| 9 |
CORS(app)
|
| 10 |
|
| 11 |
-
#
|
| 12 |
RAW_KEYS = os.getenv("OR_KEYS", "")
|
| 13 |
-
API_KEYS = RAW_KEYS.split(",") if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
@app.route('/api/chat', methods=['POST'])
|
| 16 |
def chat():
|
|
@@ -20,7 +40,9 @@ def chat():
|
|
| 20 |
user_data = request.json
|
| 21 |
user_msg = user_data.get("message", "")
|
| 22 |
|
| 23 |
-
#
|
|
|
|
|
|
|
| 24 |
current_key = random.choice(API_KEYS)
|
| 25 |
|
| 26 |
headers = {
|
|
@@ -28,32 +50,51 @@ def chat():
|
|
| 28 |
"Content-Type": "application/json"
|
| 29 |
}
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
payload = {
|
| 32 |
-
"model":
|
| 33 |
-
"messages": [
|
|
|
|
|
|
|
|
|
|
| 34 |
"stream": True
|
| 35 |
}
|
| 36 |
|
| 37 |
def generate():
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
return Response(stream_with_context(generate()), mimetype='text/plain')
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if __name__ == "__main__":
|
| 58 |
-
# HF Spaces සඳහා අනිවාර්යයෙන්ම port 7860 විය යුතුයි
|
| 59 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import random
|
| 3 |
import requests
|
| 4 |
+
import json
|
| 5 |
+
import feedparser
|
| 6 |
from flask import Flask, request, Response, stream_with_context
|
| 7 |
from flask_cors import CORS
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
|
|
|
| 10 |
CORS(app)
|
| 11 |
|
| 12 |
+
# 🔱 SECURE KEY LOADING & CLEANING
|
| 13 |
RAW_KEYS = os.getenv("OR_KEYS", "")
|
| 14 |
+
API_KEYS = [k.strip() for k in RAW_KEYS.replace("\n", ",").split(",") if k.strip()]
|
| 15 |
+
|
| 16 |
+
MODEL_NAME = "minimax/minimax-m2.5:free"
|
| 17 |
+
|
| 18 |
+
# 🔱 WEB SEARCH & INTEL SYSTEM (RSS & Google News Engine)
|
| 19 |
+
def fetch_web_intel(query):
|
| 20 |
+
intel = ""
|
| 21 |
+
# RSS News Feeds (Security, Tech & Global News)
|
| 22 |
+
RSS_FEEDS = [
|
| 23 |
+
f'https://news.google.com/rss/search?q={query}&hl=en-US&gl=US&ceid=US:en',
|
| 24 |
+
'https://feeds.feedburner.com/TheHackersNews'
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
for url in RSS_FEEDS:
|
| 28 |
+
try:
|
| 29 |
+
feed = feedparser.parse(url)
|
| 30 |
+
for entry in feed.entries[:3]: # පළමු ප්රතිඵල 3 බැගින්
|
| 31 |
+
intel += f"\n- Title: {entry.title}\n Source: {entry.link}\n"
|
| 32 |
+
except: pass
|
| 33 |
+
return intel
|
| 34 |
|
| 35 |
@app.route('/api/chat', methods=['POST'])
|
| 36 |
def chat():
|
|
|
|
| 40 |
user_data = request.json
|
| 41 |
user_msg = user_data.get("message", "")
|
| 42 |
|
| 43 |
+
# පද්ධතිය සජීවීව අන්තර්ජාලයේ තොරතුරු සෙවීම
|
| 44 |
+
web_data = fetch_web_intel(user_msg)
|
| 45 |
+
|
| 46 |
current_key = random.choice(API_KEYS)
|
| 47 |
|
| 48 |
headers = {
|
|
|
|
| 50 |
"Content-Type": "application/json"
|
| 51 |
}
|
| 52 |
|
| 53 |
+
# System Prompt එක Elephant AI ලෙස සකස් කිරීම
|
| 54 |
+
system_instruction = (
|
| 55 |
+
"You are Elephant AI, a powerful real-time technical assistant created by Commander MINZO-PRIME. "
|
| 56 |
+
"Use the provided web research data to give accurate, up-to-date answers. "
|
| 57 |
+
f"Live Web Data: {web_data if web_data else 'No live data found, use internal knowledge.'}"
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
payload = {
|
| 61 |
+
"model": MODEL_NAME,
|
| 62 |
+
"messages": [
|
| 63 |
+
{"role": "system", "content": system_instruction},
|
| 64 |
+
{"role": "user", "content": user_msg}
|
| 65 |
+
],
|
| 66 |
"stream": True
|
| 67 |
}
|
| 68 |
|
| 69 |
def generate():
|
| 70 |
+
try:
|
| 71 |
+
resp = requests.post(
|
| 72 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 73 |
+
headers=headers,
|
| 74 |
+
json=payload,
|
| 75 |
+
stream=True,
|
| 76 |
+
timeout=40
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
for line in resp.iter_lines():
|
| 80 |
+
if line:
|
| 81 |
+
decoded = line.decode('utf-8')
|
| 82 |
+
if decoded.startswith("data: "):
|
| 83 |
+
data_str = decoded[6:]
|
| 84 |
+
if data_str == "[DONE]": break
|
| 85 |
+
try:
|
| 86 |
+
chunk = json.loads(data_str)
|
| 87 |
+
content = chunk['choices'][0]['delta'].get('content', '')
|
| 88 |
+
yield content
|
| 89 |
+
except: pass
|
| 90 |
+
except Exception as e:
|
| 91 |
+
yield f"\n[Elephant Core Error: {str(e)}]"
|
| 92 |
|
| 93 |
return Response(stream_with_context(generate()), mimetype='text/plain')
|
| 94 |
|
| 95 |
+
@app.route('/')
|
| 96 |
+
def health():
|
| 97 |
+
return f"🐘 Elephant AI (Web-Search Enabled) is Online. Keys Active: {len(API_KEYS)}"
|
| 98 |
+
|
| 99 |
if __name__ == "__main__":
|
|
|
|
| 100 |
app.run(host='0.0.0.0', port=7860)
|