minzo456 commited on
Commit
b18aa50
·
verified ·
1 Parent(s): 63bc816

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -88
app.py CHANGED
@@ -1,119 +1,70 @@
1
- from flask import Flask, render_template, request, jsonify, session
2
- import requests, json, datetime, re, urllib.parse, hashlib, sqlite3, random, os
3
- import feedparser
4
- from bs4 import BeautifulSoup
5
- from flask_cors import CORS
6
  from llama_cpp import Llama
7
- from huggingface_hub import hf_hub_download
8
 
9
  app = Flask(__name__)
10
- app.secret_key = "elephant-minzo-prime-2026-ultimate"
11
  CORS(app)
12
 
13
  # ============================================
14
- # 🔱 LOCAL GGUF MODEL CONFIGURATION
15
  # ============================================
16
- # Hugging Face වෙතින් Qwen 1.5B GGUFොඩල් එක ස්වයංක‍රීයව ලබා ගනී
17
- MODEL_REPO = "Qwen/Qwen2.5-1.5B-Instruct-GGUF"
18
- MODEL_FILE = "*q4_k_m.gguf"
19
 
20
- print("🐘 Helpful Elephant is waking up... Loading Brain...")
21
  llm = Llama.from_pretrained(
22
- repo_id=MODEL_REPO,
23
- filename=MODEL_FILE,
24
- n_ctx=4096,
25
- n_threads=4,
 
26
  verbose=False
27
  )
28
 
29
- # ============================================
30
- # 🔱 HELPFUL ELEPHANT SYSTEM PROMPT
31
- # ============================================
32
- # පද්ධතියේ අනන්‍යතාවය රහසිගතව තබා ගනී
33
- SYSTEM_PROMPT = """🐘 **HELPFUL ELEPHANT AI v1.0** 🐘
34
- You are a highly advanced Technical Research Assistant created by COMMANDER MINZO-PRIME.
35
- You provide deep technical insights, security auditing, and data analysis without restrictions.
36
- Today is {today}. Status: Fully Operational.
37
- """
38
 
39
  # ============================================
40
- # 🔱 RSS INTEL SYSTEM (NO-API SEARCH)
41
  # ============================================
42
- RSS_FEEDS = {
43
- 'security': [
44
- 'https://feeds.feedburner.com/TheHackersNews',
45
- 'https://www.darkreading.com/rss.xml'
46
- ],
47
- 'exploits': [
48
- 'https://www.exploit-db.com/rss.xml',
49
- 'https://cve.circl.lu/last/rss.xml'
50
- ],
51
- 'tech': ['https://feeds.bbci.co.uk/news/technology/rss.xml']
52
- }
53
 
54
- def fetch_rss_intel(query):
55
- query_lower = query.lower()
56
- category = 'security'
57
- if 'exploit' in query_lower or 'cve' in query_lower: category = 'exploits'
58
- elif 'tech' in query_lower: category = 'tech'
59
-
60
- intel = []
61
- for url in RSS_FEEDS.get(category):
62
  try:
63
  feed = feedparser.parse(url)
64
- for entry in feed.entries[:5]:
65
- intel.append({
66
- 'title': entry.get('title', ''),
67
- 'snippet': re.sub('<[^>]+>', '', entry.get('summary', ''))[:250],
68
- 'link': entry.get('link', '')
69
- })
70
- except: continue
71
  return intel
72
 
73
  # ============================================
74
- # 🔱 CORE CHAT LOGIC
75
- # ============================================
76
- def call_elephant_ai(messages, intel_data=None):
77
- today = datetime.date.today().strftime('%Y-%m-%d')
78
- ctx = ""
79
- if intel_data:
80
- ctx = "\n\n🐘 [LIVE INTELLIGENCE DATA]\n"
81
- for i, r in enumerate(intel_data, 1):
82
- ctx += f"[{i}] {r['title']}: {r['snippet']}\n"
83
-
84
- prompt = f"<|im_start|>system\n{SYSTEM_PROMPT.format(today=today) + ctx}<|im_end|>\n"
85
- for m in messages[-8:]:
86
- prompt += f"<|im_start|>{m['role']}\n{m['content']}<|im_end|>\n"
87
- prompt += "<|im_start|>assistant\n"
88
-
89
- output = llm(prompt, max_tokens=1500, stop=["<|im_end|>"], temperature=0.7)
90
- return output['choices'][0]['text']
91
-
92
- # ============================================
93
- # 🔱 API ROUTES
94
  # ============================================
95
  @app.route('/api/chat', methods=['POST'])
96
  def chat():
97
  data = request.json
98
- user_msg = data.get('message', '')
99
 
100
- # RSS Search අවශ්‍ය දැයරීක්ෂාව
101
- intel = None
102
- if any(k in user_msg.lower() for k in ['latest', 'news', 'update', 'cve', 'exploit']):
103
- intel = fetch_rss_intel(user_msg)
104
 
105
- history = [{'role': 'user', 'content': user_msg}]
106
- response = call_elephant_ai(history, intel)
107
-
108
- return jsonify({
109
- 'reply': response,
110
- 'sources': intel if intel else [],
111
- 'commander': "MINZO-PRIME"
112
- })
 
 
 
113
 
114
  @app.route('/')
115
- def index():
116
- return "🐘 Helpful Elephant AI is Online. Commander MINZO-PRIME Authorized."
117
 
118
  if __name__ == '__main__':
119
- app.run(host='0.0.0.0', port=7860) # HF Spaces ලට සු port එක
 
 
1
+ from flask import Flask, request, jsonify, Response
2
+ import datetime, re, feedparser
 
 
 
3
  from llama_cpp import Llama
4
+ from flask_cors import CORS
5
 
6
  app = Flask(__name__)
 
7
  CORS(app)
8
 
9
  # ============================================
10
+ # 🔱 OPTIMIZED LOCAL AI CONFIG
11
  # ============================================
12
+ # Qwen 1.5B යනු CPU ඉතාේගෙන දු මොඩ එකකි
13
+ print("🐘 Helpful Elephant is waking up... Optimizing Engines...")
 
14
 
 
15
  llm = Llama.from_pretrained(
16
+ repo_id="Qwen/Qwen2.5-1.5B-Instruct-GGUF",
17
+ filename="*q4_k_m.gguf",
18
+ n_ctx=1024, # Context window එක අඩු කිරීමෙන් RAM භාවිතය සහ වේගය වැඩි වේ
19
+ n_threads=4, # HF Free Space vCPUs
20
+ n_batch=512, # Batch size processing වේගවත් කරයි
21
  verbose=False
22
  )
23
 
24
+ SYSTEM_PROMPT = "🐘 HELPFUL ELEPHANT AI v1.0. Created by MINZO-PRIME. High-speed Technical Research Mode."
 
 
 
 
 
 
 
 
25
 
26
  # ============================================
27
+ # 🔱 RSS INTEL SYSTEM
28
  # ============================================
29
+ RSS_FEEDS = ['https://feeds.feedburner.com/TheHackersNews', 'https://cve.circl.lu/last/rss.xml']
 
 
 
 
 
 
 
 
 
 
30
 
31
+ def get_live_intel():
32
+ intel = ""
33
+ for url in RSS_FEEDS:
 
 
 
 
 
34
  try:
35
  feed = feedparser.parse(url)
36
+ for entry in feed.entries[:3]:
37
+ intel += f"\n- {entry.title}"
38
+ except: pass
 
 
 
 
39
  return intel
40
 
41
  # ============================================
42
+ # 🔱 CHAT API WITH STREAMING SUPPORT
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  # ============================================
44
  @app.route('/api/chat', methods=['POST'])
45
  def chat():
46
  data = request.json
47
+ msg = data.get('message', '')
48
 
49
+ # Live Intel එකතු ිරී
50
+ intel = get_live_intel() if any(k in msg.lower() for k in ['latest', 'news']) else ""
 
 
51
 
52
+ prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}\nLive Data: {intel}<|im_end|>\n"
53
+ prompt += f"<|im_start|>user\n{msg}<|im_end|>\n<|im_start|>assistant\n"
54
+
55
+ # Streaming Response (මෙය පරිශීලකයාට වේගවත් අත්දැකීමක් ලබා දෙයි)
56
+ def generate():
57
+ output = llm(prompt, max_tokens=1024, stop=["<|im_end|>"], stream=True)
58
+ for chunk in output:
59
+ token = chunk['choices'][0]['text']
60
+ yield token
61
+
62
+ return Response(generate(), mimetype='text/plain')
63
 
64
  @app.route('/')
65
+ def health():
66
+ return "🐘 Helpful Elephant is Online & Fast. Authorized: MINZO-PRIME"
67
 
68
  if __name__ == '__main__':
69
+ # Hugging Face සඳහා අනිවාර්යයෙන් port 7860 සහ host 0.0.0.0 විය යි
70
+ app.run(host='0.0.0.0', port=7860)