Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from duckduckgo_search import DDGS
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
# CORS
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
allow_origins=["*"],
|
|
@@ -18,81 +18,127 @@ app.add_middleware(
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
| 23 |
ADMIN_SECRET = "MINZO-SECRET-2026"
|
| 24 |
LEARNING_VAULT_PATH = "neural_learning_data.jsonl"
|
| 25 |
|
| 26 |
-
# -
|
| 27 |
-
# Qwen 1.5B එක
|
| 28 |
-
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
|
|
|
| 29 |
|
| 30 |
-
print("🐘 Elephant
|
| 31 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 32 |
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
model_id,
|
| 34 |
-
torch_dtype="auto",
|
| 35 |
-
device_map="cpu"
|
| 36 |
)
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
class
|
| 40 |
admin_pass: str
|
| 41 |
new_key: str
|
|
|
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
with DDGS() as ddgs:
|
| 46 |
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 47 |
return "\n".join(results)
|
| 48 |
-
except
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
def
|
|
|
|
| 51 |
entry = {
|
| 52 |
-
"
|
| 53 |
-
"key":
|
| 54 |
-
"
|
| 55 |
-
"
|
| 56 |
-
"
|
| 57 |
}
|
| 58 |
-
with open(LEARNING_VAULT_PATH, "a") as f:
|
| 59 |
f.write(json.dumps(entry) + "\n")
|
| 60 |
|
| 61 |
-
# --- Endpoints ---
|
| 62 |
|
| 63 |
@app.get("/")
|
| 64 |
-
def
|
| 65 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
@app.post("/admin/add-key")
|
| 68 |
-
async def
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
@app.post("/v1/chat")
|
| 74 |
-
async def
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
messages = [
|
| 82 |
-
{"role": "system", "content": f"You are Elephant AI.
|
| 83 |
-
{"role": "user", "content":
|
| 84 |
]
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
with torch.no_grad():
|
| 89 |
-
generated_ids = model.generate(
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
#
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
|
|
|
| 98 |
main = app
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
# 1. CORS Configuration - Frontend එක සමඟ සම්බන්ධ වීමට
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
allow_origins=["*"],
|
|
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# 2. Database & Storage
|
| 22 |
+
# පද්ධතියේ පවතින Keys සහ ඒවායේ Limits ගබඩා කිරීම
|
| 23 |
+
API_KEYS_DB = {
|
| 24 |
+
"ELE-PRIME-ADMIN-SYS": {"limit": 10000, "used": 0, "status": "active"}
|
| 25 |
+
}
|
| 26 |
ADMIN_SECRET = "MINZO-SECRET-2026"
|
| 27 |
LEARNING_VAULT_PATH = "neural_learning_data.jsonl"
|
| 28 |
|
| 29 |
+
# 3. Light-Weight AI Model Setup (CPU Optimized)
|
| 30 |
+
# Qwen 1.5B මොඩලය 16GB RAM එකක CPU මත ඉතා වේගවත් වේ
|
| 31 |
+
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 32 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 33 |
|
| 34 |
+
print("🐘 Elephant Engine Loading on CPU Mode...")
|
| 35 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 36 |
model = AutoModelForCausalLM.from_pretrained(
|
| 37 |
model_id,
|
| 38 |
+
torch_dtype="auto",
|
| 39 |
+
device_map="cpu" # CPU මත පමණක් ධාවනයට ස්ථාවර කර ඇත
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# 4. Data Models
|
| 43 |
+
class KeyRequest(BaseModel):
|
| 44 |
admin_pass: str
|
| 45 |
new_key: str
|
| 46 |
+
limit: int = 50
|
| 47 |
|
| 48 |
+
# 5. Helper Functions
|
| 49 |
+
def get_live_web_data(query):
|
| 50 |
+
"""2026 තොරතුරු සඳහා DuckDuckGo හරහා සෙවීම"""
|
| 51 |
try:
|
| 52 |
with DDGS() as ddgs:
|
| 53 |
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 54 |
return "\n".join(results)
|
| 55 |
+
except Exception as e:
|
| 56 |
+
print(f"Search Error: {e}")
|
| 57 |
+
return ""
|
| 58 |
|
| 59 |
+
def log_to_learning_vault(query, context, response, key):
|
| 60 |
+
"""පද්ධතිය දියුණු වීමට අවශ්ය දත්ත ගබඩා කිරීම"""
|
| 61 |
entry = {
|
| 62 |
+
"ts": str(datetime.datetime.now()),
|
| 63 |
+
"key": key,
|
| 64 |
+
"input": query,
|
| 65 |
+
"web_ctx": context,
|
| 66 |
+
"ai_output": response
|
| 67 |
}
|
| 68 |
+
with open(LEARNING_VAULT_PATH, "a", encoding="utf-8") as f:
|
| 69 |
f.write(json.dumps(entry) + "\n")
|
| 70 |
|
| 71 |
+
# --- API Endpoints ---
|
| 72 |
|
| 73 |
@app.get("/")
|
| 74 |
+
def health_check():
|
| 75 |
+
return {
|
| 76 |
+
"status": "Elephant Node v3.6 Active",
|
| 77 |
+
"engine": "Qwen-2.5-1.5B-Instruct",
|
| 78 |
+
"keys_loaded": len(API_KEYS_DB)
|
| 79 |
+
}
|
| 80 |
|
| 81 |
@app.post("/admin/add-key")
|
| 82 |
+
async def register_new_key(data: KeyRequest):
|
| 83 |
+
"""නව API Key එකක් පද්ධතියට ලියාපදිංචි කිරීම"""
|
| 84 |
+
if data.admin_pass != ADMIN_SECRET:
|
| 85 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 86 |
+
|
| 87 |
+
API_KEYS_DB[data.new_key] = {
|
| 88 |
+
"limit": data.limit,
|
| 89 |
+
"used": 0,
|
| 90 |
+
"status": "active"
|
| 91 |
+
}
|
| 92 |
+
return {"message": f"Token {data.new_key} activated with limit {data.limit}"}
|
| 93 |
|
| 94 |
@app.post("/v1/chat")
|
| 95 |
+
async def chat_service(message: dict, x_api_key: str = Header(None)):
|
| 96 |
+
"""ප්රධාන Chat සේවාව"""
|
| 97 |
+
# Key එක පරීක්ෂා කිරීම
|
| 98 |
+
if not x_api_key or x_api_key not in API_KEYS_DB:
|
| 99 |
+
raise HTTPException(status_code=403, detail="Invalid API Key")
|
| 100 |
|
| 101 |
+
# Daily Limit පරීක්ෂා කිරීම
|
| 102 |
+
key_data = API_KEYS_DB[x_api_key]
|
| 103 |
+
if key_data["used"] >= key_data["limit"]:
|
| 104 |
+
raise HTTPException(status_code=429, detail="Daily request limit reached")
|
| 105 |
|
| 106 |
+
user_query = message.get("query", "")
|
| 107 |
+
if not user_query:
|
| 108 |
+
return {"reply": "Please provide a query."}
|
| 109 |
+
|
| 110 |
+
# 2026 Live Data අවශ්යදැයි බැලීම
|
| 111 |
+
context = ""
|
| 112 |
+
trigger_words = ["now", "today", "2026", "news", "price", "current"]
|
| 113 |
+
if any(w in user_query.lower() for w in trigger_words):
|
| 114 |
+
context = get_live_web_data(user_query)
|
| 115 |
+
|
| 116 |
+
# Prompt එක සැකසීම
|
| 117 |
messages = [
|
| 118 |
+
{"role": "system", "content": f"You are Elephant AI. Year: 2026. Context: {context}"},
|
| 119 |
+
{"role": "user", "content": user_query}
|
| 120 |
]
|
| 121 |
+
|
| 122 |
+
# AI Inference
|
| 123 |
+
input_text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 124 |
+
inputs = tokenizer([input_text], return_tensors="pt").to("cpu")
|
| 125 |
|
| 126 |
with torch.no_grad():
|
| 127 |
+
generated_ids = model.generate(inputs.input_ids, max_new_tokens=512, do_sample=True, temperature=0.7)
|
| 128 |
+
raw_response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 129 |
+
|
| 130 |
+
# Response එක පිරිසිදු කිරීම
|
| 131 |
+
final_reply = raw_response.split("assistant")[-1].strip()
|
| 132 |
+
|
| 133 |
+
# Usage එක යාවත්කාලීන කිරීම සහ ඉගෙනුම් දත්ත ගබඩා කිරීම
|
| 134 |
+
API_KEYS_DB[x_api_key]["used"] += 1
|
| 135 |
+
log_to_learning_vault(user_query, context, final_reply, x_api_key)
|
| 136 |
+
|
| 137 |
+
return {
|
| 138 |
+
"reply": final_reply,
|
| 139 |
+
"usage": f"{API_KEYS_DB[x_api_key]['used']}/{API_KEYS_DB[x_api_key]['limit']}",
|
| 140 |
+
"timestamp": "2026-04-27"
|
| 141 |
+
}
|
| 142 |
|
| 143 |
+
# Entry point for HF Spaces
|
| 144 |
main = app
|