Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,18 @@ from flask import Flask, request, Response, stream_with_context
|
|
| 7 |
from flask_cors import CORS
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
|
|
|
| 10 |
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 11 |
|
| 12 |
-
# 🔱
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def fetch_web_intel(query):
|
| 18 |
intel = ""
|
|
@@ -26,29 +32,31 @@ def fetch_web_intel(query):
|
|
| 26 |
|
| 27 |
@app.route('/api/chat', methods=['POST'])
|
| 28 |
def chat():
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
| 50 |
|
| 51 |
-
|
|
|
|
| 52 |
resp = requests.post(
|
| 53 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 54 |
headers=headers,
|
|
@@ -58,24 +66,27 @@ def chat():
|
|
| 58 |
)
|
| 59 |
for line in resp.iter_lines():
|
| 60 |
if line:
|
| 61 |
-
decoded = line.decode('utf-8')
|
| 62 |
-
if "
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
continue
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
|
|
|
| 75 |
|
| 76 |
@app.route('/')
|
| 77 |
def health():
|
| 78 |
-
return "🐘 Elephant AI Core
|
| 79 |
|
| 80 |
if __name__ == "__main__":
|
| 81 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 7 |
from flask_cors import CORS
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
+
# 🔱 සියලුම Origins වලට අවසර ලබා දීම (CORS Fix)
|
| 11 |
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 12 |
|
| 13 |
+
# 🔱 API KEYS DIRECT INTEGRATION
|
| 14 |
+
# ඔබේ Keys 5ම මෙහි ඉතා නිවැරදිව පේළියෙන් පේළියට ඇත
|
| 15 |
+
API_KEYS = [
|
| 16 |
+
"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",
|
| 17 |
+
"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",
|
| 18 |
+
"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",
|
| 19 |
+
"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",
|
| 20 |
+
"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"
|
| 21 |
+
]
|
| 22 |
|
| 23 |
def fetch_web_intel(query):
|
| 24 |
intel = ""
|
|
|
|
| 32 |
|
| 33 |
@app.route('/api/chat', methods=['POST'])
|
| 34 |
def chat():
|
| 35 |
+
data = request.json
|
| 36 |
+
user_msg = data.get("message", "")
|
| 37 |
+
web_data = fetch_web_intel(user_msg)
|
| 38 |
+
|
| 39 |
+
# 🔱 Random Key Rotation
|
| 40 |
+
selected_key = random.choice(API_KEYS)
|
| 41 |
+
|
| 42 |
+
headers = {
|
| 43 |
+
"Authorization": f"Bearer {selected_key}",
|
| 44 |
+
"Content-Type": "application/json",
|
| 45 |
+
"HTTP-Referer": "https://huggingface.co/spaces/minzo456/Elephant-AI-Core",
|
| 46 |
+
"X-Title": "Elephant AI Core"
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
payload = {
|
| 50 |
+
"model": "minimax/minimax-m2.5:free",
|
| 51 |
+
"messages": [
|
| 52 |
+
{"role": "system", "content": f"You are Elephant AI, a professional assistant for Commander MINZO-PRIME. Web Data: {web_data}"},
|
| 53 |
+
{"role": "user", "content": user_msg}
|
| 54 |
+
],
|
| 55 |
+
"stream": True
|
| 56 |
+
}
|
| 57 |
|
| 58 |
+
def generate():
|
| 59 |
+
try:
|
| 60 |
resp = requests.post(
|
| 61 |
"https://openrouter.ai/api/v1/chat/completions",
|
| 62 |
headers=headers,
|
|
|
|
| 66 |
)
|
| 67 |
for line in resp.iter_lines():
|
| 68 |
if line:
|
| 69 |
+
decoded = line.decode('utf-8')
|
| 70 |
+
if decoded.startswith("data: "):
|
| 71 |
+
data_str = decoded[6:]
|
| 72 |
+
if data_str.strip() == "[DONE]": break
|
| 73 |
+
try:
|
| 74 |
+
chunk = json.loads(data_str)
|
| 75 |
+
content = chunk['choices'][0]['delta'].get('content', '')
|
| 76 |
+
if content: yield content
|
| 77 |
+
except: continue
|
| 78 |
+
except Exception as e:
|
| 79 |
+
yield f"Core Connectivity Error: {str(e)}"
|
| 80 |
|
| 81 |
+
response = Response(stream_with_context(generate()), mimetype='text/plain')
|
| 82 |
+
# 🔱 Buffering වැළැක්වීමට අවශ්ය Headers
|
| 83 |
+
response.headers['X-Accel-Buffering'] = 'no'
|
| 84 |
+
response.headers['Cache-Control'] = 'no-cache'
|
| 85 |
+
return response
|
| 86 |
|
| 87 |
@app.route('/')
|
| 88 |
def health():
|
| 89 |
+
return "🐘 Elephant AI Core Status: ACTIVE | Authorized: MINZO-PRIME"
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
app.run(host='0.0.0.0', port=7860)
|