minzo456 commited on
Commit
962a48b
·
verified ·
1 Parent(s): 291922d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -122
app.py CHANGED
@@ -1,132 +1,81 @@
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>
 
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)