Spaces:
Paused
Paused
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Elephant AI Pro | Professional Suite</title><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css"><style>:root {--bg-dark: #0d0d0e;--sidebar-bg: #050505;--accent: #00f2ff;--text: #ececf1;--border: rgba(255, 255, 255, 0.1);}* { box-sizing: border-box; margin: 0; padding: 0; }body { background: var(--bg-dark); color: var(--text); font-family: 'Segoe UI', sans-serif; display: flex; height: 100vh; }/* 🔱 Sidebar */.sidebar { width: 260px; background: var(--sidebar-bg); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 15px; }.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; }.new-btn:hover { background: rgba(255,255,255,0.05); }/* 🔱 Main Chat */.main { flex: 1; display: flex; flex-direction: column; position: relative; }header { padding: 15px; text-align: center; border-bottom: 1px solid var(--border); font-weight: bold; letter-spacing: 2px; color: var(--accent); }#chat-container { flex: 1; overflow-y: auto; padding-bottom: 120px; scroll-behavior: smooth; }.row { padding: 25px 15%; display: flex; gap: 20px; border-bottom: 1px solid rgba(255,255,255,0.02); }.row.ai { background: rgba(255,255,255,0.02); }.avatar { width: 35px; height: 35px; border-radius: 4px; display: flex; align-items: center; justify-content: center; font-weight: bold; flex-shrink: 0; }.row.user .avatar { background: #5436da; }.row.ai .avatar { background: var(--accent); color: #000; }.content { line-height: 1.6; font-size: 1rem; white-space: pre-wrap; }/* 🔱 Thinking Animation */.thinking { display: none; padding: 20px 15%; align-items: center; gap: 10px; }.dots { display: flex; gap: 5px; }.dot { width: 8px; height: 8px; background: var(--accent); border-radius: 50%; animation: blink 1.4s infinite; }.dot:nth-child(2) { animation-delay: 0.2s; }.dot:nth-child(3) { animation-delay: 0.4s; }@keyframes blink { 0%, 100% { opacity: 0.3; } 50% { opacity: 1; } }/* 🔱 Input */.input-wrap { position: absolute; bottom: 0; width: 100%; padding: 20px 15% 40px 15%; background: linear-gradient(transparent, var(--bg-dark) 40%); }.input-box { background: #343541; border-radius: 10px; padding: 12px 15px; display: flex; align-items: center; border: 1px solid var(--border); }textarea { flex: 1; background: transparent; border: none; color: #fff; outline: none; font-size: 1rem; resize: none; max-height: 200px; }.send-btn { color: var(--accent); cursor: pointer; font-size: 1.2rem; margin-left: 10px; }</style></head><body><div class="sidebar"><div class="new-btn" onclick="location.reload()">+ New Chat</div><div style="font-size: 0.7rem; color: #555;">Elephant AI Pro v2.5<br>MINZO-PRIME Authorized</div></div><div class="main"><header>🐘 ELEPHANT AI CORE</header><div id="chat-container"></div><div id="thinking-ui" class="thinking"><div class="dots"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div><span style="font-size: 0.8rem; color: var(--accent);">Elephant AI is thinking...</span></div><div class="input-wrap"><div class="input-box"><textarea id="userInput" rows="1" placeholder="Type your command..." oninput="this.style.height='';this.style.height=this.scrollHeight+'px'"></textarea><i class="fa fa-paper-plane send-btn" onclick="execute()"></i></div></div></div><script>// 🔱 වැදගත්: ඔබේ Hugging Face URL එක මෙතනට දාන්නconst API_URL = "https://minzo456-elephant-ai-core.hf.space/api/chat";async function execute() {const input = document.getElementById('userInput');const text = input.value.trim();if(!text) return;input.value = '';input.style.height = 'auto';// 1. User MessageappendRow('user', 'M', text);// 2. Show Thinkingconst thinking = document.getElementById('thinking-ui');thinking.style.display = 'flex';document.getElementById('chat-container').scrollTop = 99999;try {const response = await fetch(API_URL, {method: "POST",headers: { "Content-Type": "application/json" },body: JSON.stringify({ message: text })});// 3. Hide Thinking & Start Responsethinking.style.display = 'none';const aiContentDiv = appendRow('ai', 'E', '');const reader = response.body.getReader();const decoder = new TextDecoder();while (true) {const { done, value } = await reader.read();if (done) break;aiContentDiv.innerText += decoder.decode(value);document.getElementById('chat-container').scrollTop = 99999;}} catch (e) {thinking.style.display = 'none';appendRow('ai', 'E', "Connection Error. Check Space Status.");}}function appendRow(role, icon, text) {const container = document.getElementById('chat-container');const row = document.createElement('div');row.className = `row ${role}`;row.innerHTML = `<div class="avatar">${icon}</div><div class="content">${text}</div>`;container.appendChild(row);return row.querySelector('.content');}</script></body></html>123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081import osimport randomimport requestsimport jsonimport feedparserfrom flask import Flask, request, Response, stream_with_contextfrom flask_cors import CORSapp = Flask(__name__)CORS(app, resources={r"/*": {"origins": "*"}})# 🔱 SECURE KEY LOADING & CLEANINGRAW_KEYS = os.getenv("OR_KEYS", "")# New lines හෝ spaces තිබුණොත් ඒවා පිරිසිදු කර Keys වෙන් කරගනීAPI_KEYS = [k.strip() for k in RAW_KEYS.replace("\n", ",").split(",") if k.strip()]def fetch_web_intel(query):intel = ""try:url = f'https://news.google.com/rss/search?q={query}&hl=en-US'feed = feedparser.parse(url)for entry in feed.entries[:3]:intel += f"\n- {entry.title}"except: passreturn intel@app.route('/api/chat', methods=['POST'])def chat():if not API_KEYS:return "Error: No API Keys found in Secrets.", 500try:data = request.jsonuser_msg = data.get("message", "")web_data = fetch_web_intel(user_msg)headers = {"Authorization": f"Bearer {random.choice(API_KEYS)}","Content-Type": "application/json"}payload = {"model": "minimax/minimax-m2.5:free","messages": [{"role": "system", "content": f"You are Elephant AI by MINZO-PRIME. Use this web data if relevant: {web_data}"},{"role": "user", "content": user_msg}],"stream": True}def generate():resp = requests.post("https://openrouter.ai/api/v1/chat/completions",headers=headers,json=payload,stream=True,timeout=60)for line in resp.iter_lines():if line:decoded = line.decode('utf-8').replace("data: ", "")if "[DONE]" in decoded:breaktry:chunk = json.loads(decoded)content = chunk['choices'][0]['delta'].get('content', '')yield contentexcept:continuereturn Response(stream_with_context(generate()), mimetype='text/plain')except Exception as e:return str(e), 500@app.route('/')def health():return "🐘 Elephant AI Core is Online. Port: 7860"if __name__ == "__main__":app.run(host='0.0.0.0', port=7860)
app.py+71-122