Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,11 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import
|
| 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 |
-
|
| 11 |
-
CORS(app, resources={r"/*": {"origins": "*"}})
|
| 12 |
|
| 13 |
-
# 🔱
|
| 14 |
-
# ඔබේ Keys 5ම මෙහි ඉතා නිවැරදිව පේළියෙන් පේළියට ඇත
|
| 15 |
API_KEYS = [
|
| 16 |
"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",
|
| 17 |
"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",
|
|
@@ -20,73 +14,39 @@ API_KEYS = [
|
|
| 20 |
"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"
|
| 21 |
]
|
| 22 |
|
| 23 |
-
def fetch_web_intel(query):
|
| 24 |
-
intel = ""
|
| 25 |
-
try:
|
| 26 |
-
url = f'https://news.google.com/rss/search?q={query}&hl=en-US'
|
| 27 |
-
feed = feedparser.parse(url)
|
| 28 |
-
for entry in feed.entries[:3]:
|
| 29 |
-
intel += f"\n- {entry.title}"
|
| 30 |
-
except: pass
|
| 31 |
-
return intel
|
| 32 |
-
|
| 33 |
@app.route('/api/chat', methods=['POST'])
|
| 34 |
def chat():
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
headers=headers,
|
| 63 |
-
json=payload,
|
| 64 |
-
stream=True,
|
| 65 |
-
timeout=60
|
| 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
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
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 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
+
CORS(app)
|
|
|
|
| 7 |
|
| 8 |
+
# 🔱 ඔබ ලබා දුන් API Keys
|
|
|
|
| 9 |
API_KEYS = [
|
| 10 |
"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",
|
| 11 |
"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",
|
|
|
|
| 14 |
"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"
|
| 15 |
]
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
@app.route('/api/chat', methods=['POST'])
|
| 18 |
def chat():
|
| 19 |
+
try:
|
| 20 |
+
data = request.json
|
| 21 |
+
user_msg = data.get("message", "")
|
| 22 |
+
|
| 23 |
+
headers = {
|
| 24 |
+
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 25 |
+
"Content-Type": "application/json"
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
payload = {
|
| 29 |
+
"model": "minimax/minimax-m2.5:free",
|
| 30 |
+
"messages": [
|
| 31 |
+
{"role": "system", "content": "You are Elephant AI created by MINZO-PRIME. Be helpful and professional."},
|
| 32 |
+
{"role": "user", "content": user_msg}
|
| 33 |
+
],
|
| 34 |
+
"stream": False # 🔱 ස්ථාවරත්වය සඳහා Streaming අක්රීය කළා
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
resp = requests.post("https://openrouter.ai/api/v1/chat/completions", headers=headers, json=payload)
|
| 38 |
+
res_data = resp.json()
|
| 39 |
+
|
| 40 |
+
# පිළිතුර ලබා ගැනීම
|
| 41 |
+
ai_reply = res_data['choices'][0]['message']['content']
|
| 42 |
+
return jsonify({"reply": ai_reply})
|
| 43 |
+
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return jsonify({"reply": f"Core Error: {str(e)}"}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
@app.route('/')
|
| 48 |
def health():
|
| 49 |
+
return "🐘 Elephant AI Core: ONLINE"
|
| 50 |
|
| 51 |
if __name__ == "__main__":
|
| 52 |
app.run(host='0.0.0.0', port=7860)
|