Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from duckduckgo_search import DDGS
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
#
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
allow_origins=["*"],
|
|
@@ -18,127 +18,80 @@ app.add_middleware(
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
-
#
|
| 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 |
-
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
# Qwen 1.5B මොඩලය 16GB RAM එකක CPU මත ඉතා වේගවත් වේ
|
| 31 |
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 32 |
-
|
| 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 |
-
#
|
| 43 |
class KeyRequest(BaseModel):
|
| 44 |
admin_pass: str
|
| 45 |
new_key: str
|
| 46 |
-
limit: int =
|
| 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
|
| 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
|
| 83 |
-
"""නව API Key එකක් පද්ධතියට ලියාපදිංචි කිරීම"""
|
| 84 |
if data.admin_pass != ADMIN_SECRET:
|
| 85 |
-
raise HTTPException(status_code=401
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
}
|
| 92 |
-
return {"message": f"Token {data.new_key} activated with limit {data.limit}"}
|
| 93 |
|
| 94 |
@app.post("/v1/chat")
|
| 95 |
-
async def
|
| 96 |
-
|
| 97 |
-
|
| 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 |
-
|
| 102 |
-
|
| 103 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 111 |
context = ""
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
| 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 |
-
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
with torch.no_grad():
|
| 127 |
-
|
| 128 |
-
|
| 129 |
|
| 130 |
-
#
|
| 131 |
-
final_reply = raw_response.split("assistant")[-1].strip()
|
| 132 |
-
|
| 133 |
-
# Usage එක යාවත්කාලීන කිරීම සහ ඉගෙනුම් දත්ත ගබඩා කිරීම
|
| 134 |
API_KEYS_DB[x_api_key]["used"] += 1
|
| 135 |
-
|
| 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
|
|
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
# CORS Fix for Dashboard connectivity
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
allow_origins=["*"],
|
|
|
|
| 18 |
allow_headers=["*"],
|
| 19 |
)
|
| 20 |
|
| 21 |
+
# --- Database & Config ---
|
|
|
|
| 22 |
API_KEYS_DB = {
|
| 23 |
"ELE-PRIME-ADMIN-SYS": {"limit": 10000, "used": 0, "status": "active"}
|
| 24 |
}
|
| 25 |
ADMIN_SECRET = "MINZO-SECRET-2026"
|
| 26 |
+
LEARNING_VAULT = "neural_learning_data.jsonl"
|
| 27 |
|
| 28 |
+
# --- AI Model (Qwen-2.5-1.5B) ---
|
|
|
|
| 29 |
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 30 |
+
print("🐘 Elephant Node v3.7 Loading...")
|
|
|
|
|
|
|
| 31 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 32 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# --- Data Models ---
|
| 35 |
class KeyRequest(BaseModel):
|
| 36 |
admin_pass: str
|
| 37 |
new_key: str
|
| 38 |
+
limit: int = 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --- API Endpoints ---
|
| 41 |
|
| 42 |
@app.get("/")
|
| 43 |
+
def home():
|
| 44 |
+
return {"status": "Elephant Pro Active", "keys": len(API_KEYS_DB)}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
@app.post("/admin/add-key")
|
| 47 |
+
async def add_key(data: KeyRequest):
|
|
|
|
| 48 |
if data.admin_pass != ADMIN_SECRET:
|
| 49 |
+
raise HTTPException(status_code=401)
|
| 50 |
+
API_KEYS_DB[data.new_key] = {"limit": data.limit, "used": 0, "status": "active"}
|
| 51 |
+
return {"message": "Key Activated"}
|
| 52 |
+
|
| 53 |
+
@app.get("/v1/usage")
|
| 54 |
+
async def get_usage(x_api_key: str = Header(None)):
|
| 55 |
+
"""Key එකේ පාවිච්චිය පරීක්ෂා කිරීමේ Endpoint එක"""
|
| 56 |
+
if not x_api_key or x_api_key not in API_KEYS_DB:
|
| 57 |
+
raise HTTPException(status_code=403, detail="Invalid Key")
|
| 58 |
+
info = API_KEYS_DB[x_api_key]
|
| 59 |
+
return {
|
| 60 |
+
"used": info["used"],
|
| 61 |
+
"limit": info["limit"],
|
| 62 |
+
"percentage": (info["used"] / info["limit"]) * 100 if info["limit"] > 0 else 0
|
| 63 |
}
|
|
|
|
| 64 |
|
| 65 |
@app.post("/v1/chat")
|
| 66 |
+
async def chat(message: dict, x_api_key: str = Header(None)):
|
| 67 |
+
if x_api_key not in API_KEYS_DB:
|
| 68 |
+
raise HTTPException(status_code=403)
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
key_info = API_KEYS_DB[x_api_key]
|
| 71 |
+
if key_info["used"] >= key_info["limit"]:
|
| 72 |
+
raise HTTPException(status_code=429, detail="Limit Reached")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
query = message.get("query", "")
|
| 75 |
+
|
| 76 |
+
# 2026 Web Search Logic
|
| 77 |
context = ""
|
| 78 |
+
if any(w in query.lower() for w in ["today", "now", "2026"]):
|
| 79 |
+
try:
|
| 80 |
+
with DDGS() as ddgs:
|
| 81 |
+
context = "\n".join([r['body'] for r in ddgs.text(query, max_results=2)])
|
| 82 |
+
except: pass
|
| 83 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
# AI Inference
|
| 85 |
+
msgs = [{"role": "system", "content": f"Elephant AI. 2026 mode. Context: {context}"}, {"role": "user", "content": query}]
|
| 86 |
+
text = tokenizer.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)
|
| 87 |
+
inputs = tokenizer([text], return_tensors="pt").to("cpu")
|
| 88 |
|
| 89 |
with torch.no_grad():
|
| 90 |
+
ids = model.generate(inputs.input_ids, max_new_tokens=256)
|
| 91 |
+
ans = tokenizer.batch_decode(ids, skip_special_tokens=True)[0].split("assistant")[-1].strip()
|
| 92 |
|
| 93 |
+
# Update Stats
|
|
|
|
|
|
|
|
|
|
| 94 |
API_KEYS_DB[x_api_key]["used"] += 1
|
| 95 |
+
return {"reply": ans, "usage": API_KEYS_DB[x_api_key]["used"]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
|
|
|
| 97 |
main = app
|