minzo456 commited on
Commit
291922d
·
verified ·
1 Parent(s): 47692bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -90
app.py CHANGED
@@ -1,100 +1,132 @@
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():
37
- if not API_KEYS:
38
- return "Error: No API Keys found in Secrets.", 500
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
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 = {
49
- "Authorization": f"Bearer {current_key}",
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)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Elephant AI Pro | Professional Suite</title>
7
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
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
+ .row { padding: 25px 15%; display: flex; gap: 20px; border-bottom: 1px solid rgba(255,255,255,0.02); }
31
+ .row.ai { background: rgba(255,255,255,0.02); }
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
+ /* 🔱 Thinking Animation */
38
+ .thinking { display: none; padding: 20px 15%; align-items: center; gap: 10px; }
39
+ .dots { display: flex; gap: 5px; }
40
+ .dot { width: 8px; height: 8px; background: var(--accent); border-radius: 50%; animation: blink 1.4s infinite; }
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
+ /* 🔱 Input */
46
+ .input-wrap { position: absolute; bottom: 0; width: 100%; padding: 20px 15% 40px 15%; background: linear-gradient(transparent, var(--bg-dark) 40%); }
47
+ .input-box { background: #343541; border-radius: 10px; padding: 12px 15px; display: flex; align-items: center; border: 1px solid var(--border); }
48
+ textarea { flex: 1; background: transparent; border: none; color: #fff; outline: none; font-size: 1rem; resize: none; max-height: 200px; }
49
+ .send-btn { color: var(--accent); cursor: pointer; font-size: 1.2rem; margin-left: 10px; }
50
+ </style>
51
+ </head>
52
+ <body>
53
+
54
+ <div class="sidebar">
55
+ <div class="new-btn" onclick="location.reload()">+ New Chat</div>
56
+ <div style="font-size: 0.7rem; color: #555;">Elephant AI Pro v2.5<br>MINZO-PRIME Authorized</div>
57
+ </div>
58
+
59
+ <div class="main">
60
+ <header>🐘 ELEPHANT AI CORE</header>
61
+ <div id="chat-container"></div>
62
 
63
+ <div id="thinking-ui" class="thinking">
64
+ <div class="dots"><div class="dot"></div><div class="dot"></div><div class="dot"></div></div>
65
+ <span style="font-size: 0.8rem; color: var(--accent);">Elephant AI is thinking...</span>
66
+ </div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ // 1. User Message
89
+ appendRow('user', 'M', text);
90
+
91
+ // 2. Show Thinking
92
+ const thinking = document.getElementById('thinking-ui');
93
+ thinking.style.display = 'flex';
94
+ document.getElementById('chat-container').scrollTop = 99999;
95
+
96
+ try {
97
+ const response = await fetch(API_URL, {
98
+ method: "POST",
99
+ headers: { "Content-Type": "application/json" },
100
+ body: JSON.stringify({ message: text })
101
+ });
102
+
103
+ // 3. Hide Thinking & Start Response
104
+ thinking.style.display = 'none';
105
+ const aiContentDiv = appendRow('ai', 'E', '');
106
+
107
+ const reader = response.body.getReader();
108
+ const decoder = new TextDecoder();
109
+
110
+ while (true) {
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
+ function appendRow(role, icon, text) {
123
+ const container = document.getElementById('chat-container');
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>