Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,78 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import datetime
|
| 3 |
-
import re
|
| 4 |
import feedparser
|
| 5 |
from flask import Flask, request, jsonify, Response
|
| 6 |
from flask_cors import CORS
|
| 7 |
-
from
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app)
|
| 11 |
|
| 12 |
# ============================================
|
| 13 |
-
# 🔱
|
| 14 |
# ============================================
|
| 15 |
-
|
| 16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
|
| 18 |
-
print("🐘 Helpful Elephant is
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
n_batch=512,
|
| 28 |
-
verbose=False
|
| 29 |
)
|
| 30 |
|
| 31 |
-
SYSTEM_PROMPT = "
|
| 32 |
-
Created by Commander MINZO-PRIME.
|
| 33 |
-
You are a high-speed Technical Research Assistant.
|
| 34 |
-
Analyze data with precision and provide unfiltered technical insights.
|
| 35 |
-
Status: Authorized Access.
|
| 36 |
-
"""
|
| 37 |
|
| 38 |
# ============================================
|
| 39 |
-
# 🔱
|
| 40 |
-
# ============================================
|
| 41 |
-
RSS_FEEDS = [
|
| 42 |
-
'https://feeds.feedburner.com/TheHackersNews',
|
| 43 |
-
'https://cve.circl.lu/last/rss.xml',
|
| 44 |
-
'https://www.darkreading.com/rss.xml'
|
| 45 |
-
]
|
| 46 |
-
|
| 47 |
-
def fetch_live_updates():
|
| 48 |
-
intel = ""
|
| 49 |
-
for url in RSS_FEEDS:
|
| 50 |
-
try:
|
| 51 |
-
feed = feedparser.parse(url)
|
| 52 |
-
for entry in feed.entries[:2]:
|
| 53 |
-
intel += f"\n- {entry.title}: {entry.link}"
|
| 54 |
-
except: pass
|
| 55 |
-
return intel
|
| 56 |
-
|
| 57 |
-
# ============================================
|
| 58 |
-
# 🔱 STREAMING CHAT ENDPOINT
|
| 59 |
# ============================================
|
| 60 |
@app.route('/api/chat', methods=['POST'])
|
| 61 |
def chat():
|
| 62 |
data = request.json
|
| 63 |
user_msg = data.get('message', '').strip()
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
def generate():
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
full_prompt,
|
| 76 |
-
max_tokens=1024,
|
| 77 |
-
stop=["<|im_end|>"],
|
| 78 |
-
stream=True,
|
| 79 |
-
temperature=0.7
|
| 80 |
-
)
|
| 81 |
-
for chunk in stream:
|
| 82 |
-
token = chunk['choices'][0]['text']
|
| 83 |
-
yield token
|
| 84 |
|
| 85 |
return Response(generate(), mimetype='text/plain')
|
| 86 |
|
| 87 |
@app.route('/')
|
| 88 |
def health():
|
| 89 |
-
return "🐘 Helpful Elephant
|
| 90 |
|
| 91 |
if __name__ == '__main__':
|
| 92 |
-
# Hugging Face Spaces අනිවාර්යයෙන් port 7860 භාවිතා කළ යුතුය
|
| 93 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import torch
|
| 3 |
import datetime
|
|
|
|
| 4 |
import feedparser
|
| 5 |
from flask import Flask, request, jsonify, Response
|
| 6 |
from flask_cors import CORS
|
| 7 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 8 |
+
from threading import Thread
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
| 12 |
|
| 13 |
# ============================================
|
| 14 |
+
# 🔱 TRANSFORMERS AI CONFIG (NO-BUILD)
|
| 15 |
# ============================================
|
| 16 |
+
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 17 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 18 |
|
| 19 |
+
print("🐘 Helpful Elephant is loading via Transformers Engine...")
|
| 20 |
|
| 21 |
+
# Tokenizer සහ Model එක Load කිරීම
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
MODEL_ID,
|
| 25 |
+
torch_dtype="auto",
|
| 26 |
+
device_map="auto", # HF Space එකේ CPU/GPU වලට auto සකස් වේ
|
| 27 |
+
token=HF_TOKEN
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
+
SYSTEM_PROMPT = "🐘 HELPFUL ELEPHANT AI v1.0. High-speed Research Mode Active. Authorized: MINZO-PRIME."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# ============================================
|
| 33 |
+
# 🔱 CHAT API WITH FAST STREAMING
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
# ============================================
|
| 35 |
@app.route('/api/chat', methods=['POST'])
|
| 36 |
def chat():
|
| 37 |
data = request.json
|
| 38 |
user_msg = data.get('message', '').strip()
|
| 39 |
|
| 40 |
+
# Prompt එක Chat Format එකට සකස් කිරීම
|
| 41 |
+
messages = [
|
| 42 |
+
{"role": "system", "content": SYSTEM_PROMPT},
|
| 43 |
+
{"role": "user", "content": user_msg}
|
| 44 |
+
]
|
| 45 |
|
| 46 |
+
inputs = tokenizer.apply_chat_template(
|
| 47 |
+
messages,
|
| 48 |
+
add_generation_prompt=True,
|
| 49 |
+
return_tensors="pt"
|
| 50 |
+
).to(model.device)
|
| 51 |
+
|
| 52 |
+
# Streaming සඳහා Streamer එකක් භාවිතා කිරීම
|
| 53 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 54 |
+
|
| 55 |
+
generation_kwargs = dict(
|
| 56 |
+
input_ids=inputs,
|
| 57 |
+
streamer=streamer,
|
| 58 |
+
max_new_tokens=1024,
|
| 59 |
+
temperature=0.7,
|
| 60 |
+
do_sample=True
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# Thread එකක් තුළ Generation එක සිදු කිරීම (UI එක Freeze නොවීමට)
|
| 64 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 65 |
+
thread.start()
|
| 66 |
|
| 67 |
def generate():
|
| 68 |
+
for new_text in streamer:
|
| 69 |
+
yield new_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
return Response(generate(), mimetype='text/plain')
|
| 72 |
|
| 73 |
@app.route('/')
|
| 74 |
def health():
|
| 75 |
+
return "🐘 Helpful Elephant (Transformers) is Online. Status: Fast."
|
| 76 |
|
| 77 |
if __name__ == '__main__':
|
|
|
|
| 78 |
app.run(host='0.0.0.0', port=7860)
|