Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
<style>
|
| 9 |
-
:root {
|
| 10 |
-
--bg-dark: #0d0d0e;
|
| 11 |
-
--sidebar-bg: #050505;
|
| 12 |
-
--accent: #00f2ff;
|
| 13 |
-
--text: #ececf1;
|
| 14 |
-
--border: rgba(255, 255, 255, 0.1);
|
| 15 |
-
}
|
| 16 |
-
|
| 17 |
-
* { box-sizing: border-box; margin: 0; padding: 0; }
|
| 18 |
-
body { background: var(--bg-dark); color: var(--text); font-family: 'Segoe UI', sans-serif; display: flex; height: 100vh; }
|
| 19 |
-
|
| 20 |
-
/* 🔱 Sidebar */
|
| 21 |
-
.sidebar { width: 260px; background: var(--sidebar-bg); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 15px; }
|
| 22 |
-
.new-btn { border: 1px solid var(--border); padding: 10px; border-radius: 5px; text-align: center; cursor: pointer; margin-bottom: 20px; font-size: 0.9rem; transition: 0.3s; }
|
| 23 |
-
.new-btn:hover { background: rgba(255,255,255,0.05); }
|
| 24 |
-
|
| 25 |
-
/* 🔱 Main Chat */
|
| 26 |
-
.main { flex: 1; display: flex; flex-direction: column; position: relative; }
|
| 27 |
-
header { padding: 15px; text-align: center; border-bottom: 1px solid var(--border); font-weight: bold; letter-spacing: 2px; color: var(--accent); }
|
| 28 |
-
#chat-container { flex: 1; overflow-y: auto; padding-bottom: 120px; scroll-behavior: smooth; }
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
.avatar { width: 35px; height: 35px; border-radius: 4px; display: flex; align-items: center; justify-content: center; font-weight: bold; flex-shrink: 0; }
|
| 33 |
-
.row.user .avatar { background: #5436da; }
|
| 34 |
-
.row.ai .avatar { background: var(--accent); color: #000; }
|
| 35 |
-
.content { line-height: 1.6; font-size: 1rem; white-space: pre-wrap; }
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
.dot:nth-child(2) { animation-delay: 0.2s; }
|
| 42 |
-
.dot:nth-child(3) { animation-delay: 0.4s; }
|
| 43 |
-
@keyframes blink { 0%, 100% { opacity: 0.3; } 50% { opacity: 1; } }
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
<div class="main">
|
| 60 |
-
<header>🐘 ELEPHANT AI CORE</header>
|
| 61 |
-
<div id="chat-container"></div>
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
<div class="input-wrap">
|
| 69 |
-
<div class="input-box">
|
| 70 |
-
<textarea id="userInput" rows="1" placeholder="Type your command..." oninput="this.style.height='';this.style.height=this.scrollHeight+'px'"></textarea>
|
| 71 |
-
<i class="fa fa-paper-plane send-btn" onclick="execute()"></i>
|
| 72 |
-
</div>
|
| 73 |
-
</div>
|
| 74 |
-
</div>
|
| 75 |
-
|
| 76 |
-
<script>
|
| 77 |
-
// 🔱 වැදගත්: ඔබේ Hugging Face URL එක මෙතනට දාන්න
|
| 78 |
-
const API_URL = "https://minzo456-elephant-ai-core.hf.space/api/chat";
|
| 79 |
-
|
| 80 |
-
async function execute() {
|
| 81 |
-
const input = document.getElementById('userInput');
|
| 82 |
-
const text = input.value.trim();
|
| 83 |
-
if(!text) return;
|
| 84 |
-
|
| 85 |
-
input.value = '';
|
| 86 |
-
input.style.height = 'auto';
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
headers
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
const { done, value } = await reader.read();
|
| 112 |
-
if (done) break;
|
| 113 |
-
aiContentDiv.innerText += decoder.decode(value);
|
| 114 |
-
document.getElementById('chat-container').scrollTop = 99999;
|
| 115 |
-
}
|
| 116 |
-
} catch (e) {
|
| 117 |
-
thinking.style.display = 'none';
|
| 118 |
-
appendRow('ai', 'E', "Connection Error. Check Space Status.");
|
| 119 |
-
}
|
| 120 |
-
}
|
| 121 |
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
const row = document.createElement('div');
|
| 125 |
-
row.className = `row ${role}`;
|
| 126 |
-
row.innerHTML = `<div class="avatar">${icon}</div><div class="content">${text}</div>`;
|
| 127 |
-
container.appendChild(row);
|
| 128 |
-
return row.querySelector('.content');
|
| 129 |
-
}
|
| 130 |
-
</script>
|
| 131 |
-
</body>
|
| 132 |
-
</html>
|
|
|
|
| 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, resources={r"/*": {"origins": "*"}})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# 🔱 SECURE KEY LOADING & CLEANING
|
| 13 |
+
RAW_KEYS = os.getenv("OR_KEYS", "")
|
| 14 |
+
# New lines හෝ spaces තිබුණොත් ඒවා පිරිසිදු කර Keys වෙන් කරගනී
|
| 15 |
+
API_KEYS = [k.strip() for k in RAW_KEYS.replace("\n", ",").split(",") if k.strip()]
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
def fetch_web_intel(query):
|
| 18 |
+
intel = ""
|
| 19 |
+
try:
|
| 20 |
+
url = f'https://news.google.com/rss/search?q={query}&hl=en-US'
|
| 21 |
+
feed = feedparser.parse(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'])
|
| 28 |
+
def chat():
|
| 29 |
+
if not API_KEYS:
|
| 30 |
+
return "Error: No API Keys found in Secrets.", 500
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
try:
|
| 33 |
+
data = request.json
|
| 34 |
+
user_msg = data.get("message", "")
|
| 35 |
+
web_data = fetch_web_intel(user_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
headers = {
|
| 38 |
+
"Authorization": f"Bearer {random.choice(API_KEYS)}",
|
| 39 |
+
"Content-Type": "application/json"
|
| 40 |
+
}
|
| 41 |
|
| 42 |
+
payload = {
|
| 43 |
+
"model": "minimax/minimax-m2.5:free",
|
| 44 |
+
"messages": [
|
| 45 |
+
{"role": "system", "content": f"You are Elephant AI by MINZO-PRIME. Use this web data if relevant: {web_data}"},
|
| 46 |
+
{"role": "user", "content": user_msg}
|
| 47 |
+
],
|
| 48 |
+
"stream": True
|
| 49 |
+
}
|
| 50 |
|
| 51 |
+
def generate():
|
| 52 |
+
resp = requests.post(
|
| 53 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 54 |
+
headers=headers,
|
| 55 |
+
json=payload,
|
| 56 |
+
stream=True,
|
| 57 |
+
timeout=60
|
| 58 |
+
)
|
| 59 |
+
for line in resp.iter_lines():
|
| 60 |
+
if line:
|
| 61 |
+
decoded = line.decode('utf-8').replace("data: ", "")
|
| 62 |
+
if "[DONE]" in decoded:
|
| 63 |
+
break
|
| 64 |
+
try:
|
| 65 |
+
chunk = json.loads(decoded)
|
| 66 |
+
content = chunk['choices'][0]['delta'].get('content', '')
|
| 67 |
+
yield content
|
| 68 |
+
except:
|
| 69 |
+
continue
|
| 70 |
|
| 71 |
+
return Response(stream_with_context(generate()), mimetype='text/plain')
|
| 72 |
+
|
| 73 |
+
except Exception as e:
|
| 74 |
+
return str(e), 500
|
| 75 |
+
|
| 76 |
+
@app.route('/')
|
| 77 |
+
def health():
|
| 78 |
+
return "🐘 Elephant AI Core is Online. Port: 7860"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
app.run(host='0.0.0.0', port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|