Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,79 +1,59 @@
|
|
| 1 |
import os
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
from flask import Flask, request,
|
| 5 |
from flask_cors import CORS
|
| 6 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 7 |
-
from threading import Thread
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
-
# 🔱
|
| 11 |
-
CORS(app
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 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...")
|
| 20 |
-
|
| 21 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 22 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
-
MODEL_ID,
|
| 24 |
-
torch_dtype="auto",
|
| 25 |
-
device_map="auto",
|
| 26 |
-
token=HF_TOKEN
|
| 27 |
-
)
|
| 28 |
-
|
| 29 |
-
SYSTEM_PROMPT = "🐘 HELPFUL ELEPHANT AI v1.0. High-speed Research Mode. Commander: MINZO-PRIME."
|
| 30 |
-
|
| 31 |
-
# ============================================
|
| 32 |
-
# 🔱 API ENDPOINT
|
| 33 |
-
# ============================================
|
| 34 |
@app.route('/api/chat', methods=['POST'])
|
| 35 |
def chat():
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
user_msg = data.get('message', '').strip()
|
| 39 |
-
|
| 40 |
-
messages = [
|
| 41 |
-
{"role": "system", "content": SYSTEM_PROMPT},
|
| 42 |
-
{"role": "user", "content": user_msg}
|
| 43 |
-
]
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
add_generation_prompt=True,
|
| 48 |
-
return_tensors="pt"
|
| 49 |
-
).to(model.device)
|
| 50 |
-
|
| 51 |
-
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 52 |
-
|
| 53 |
-
generation_kwargs = dict(
|
| 54 |
-
input_ids=inputs,
|
| 55 |
-
streamer=streamer,
|
| 56 |
-
max_new_tokens=1024,
|
| 57 |
-
temperature=0.7,
|
| 58 |
-
do_sample=True
|
| 59 |
-
)
|
| 60 |
-
|
| 61 |
-
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 62 |
-
thread.start()
|
| 63 |
-
|
| 64 |
-
def generate():
|
| 65 |
-
for new_text in streamer:
|
| 66 |
-
yield new_text
|
| 67 |
-
|
| 68 |
-
return Response(generate(), mimetype='text/plain')
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
+
import random
|
| 3 |
+
import requests
|
| 4 |
+
from flask import Flask, request, Response, stream_with_context
|
| 5 |
from flask_cors import CORS
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
| 8 |
+
# 🔱 ලෝකයේ ඕනෑම තැනක සිට එන HTML එකකට සම්බන්ධ වීමට ඉඩ දීම
|
| 9 |
+
CORS(app)
|
| 10 |
|
| 11 |
+
# Hugging Face Secrets වලින් Keys ටික ලබා ගැනීම
|
| 12 |
+
RAW_KEYS = os.getenv("OR_KEYS", "")
|
| 13 |
+
API_KEYS = RAW_KEYS.split(",") if RAW_KEYS else []
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
@app.route('/api/chat', methods=['POST'])
|
| 16 |
def chat():
|
| 17 |
+
if not API_KEYS:
|
| 18 |
+
return "Error: No API Keys found in Secrets.", 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
user_data = request.json
|
| 21 |
+
user_msg = user_data.get("message", "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Random ලෙස Key එකක් තෝරා ගැනීම (Key Rotation)
|
| 24 |
+
current_key = random.choice(API_KEYS)
|
| 25 |
+
|
| 26 |
+
headers = {
|
| 27 |
+
"Authorization": f"Bearer {current_key}",
|
| 28 |
+
"Content-Type": "application/json"
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
payload = {
|
| 32 |
+
"model": "minimax/minimax-m2.5:free",
|
| 33 |
+
"messages": [{"role": "user", "content": user_msg}],
|
| 34 |
+
"stream": True
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
def generate():
|
| 38 |
+
resp = requests.post(
|
| 39 |
+
"https://openrouter.ai/api/v1/chat/completions",
|
| 40 |
+
headers=headers,
|
| 41 |
+
json=payload,
|
| 42 |
+
stream=True
|
| 43 |
+
)
|
| 44 |
+
for line in resp.iter_lines():
|
| 45 |
+
if line:
|
| 46 |
+
decoded = line.decode('utf-8')
|
| 47 |
+
if "data: " in decoded and "[DONE]" not in decoded:
|
| 48 |
+
try:
|
| 49 |
+
import json
|
| 50 |
+
chunk = json.loads(decoded.replace("data: ", ""))
|
| 51 |
+
content = chunk['choices'][0]['delta'].get('content', '')
|
| 52 |
+
yield content
|
| 53 |
+
except: pass
|
| 54 |
+
|
| 55 |
+
return Response(stream_with_context(generate()), mimetype='text/plain')
|
| 56 |
+
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
# HF Spaces සඳහා අනිවාර්යයෙන්ම port 7860 විය යුතුයි
|
| 59 |
app.run(host='0.0.0.0', port=7860)
|