Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,31 @@
|
|
| 1 |
from fastapi import FastAPI, Header, HTTPException
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
import os
|
| 4 |
import json
|
|
|
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 6 |
from duckduckgo_search import DDGS
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
model_id = "mistralai/Mistral-7B-v0.3"
|
| 16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 17 |
|
|
@@ -22,8 +36,7 @@ quant_config = BitsAndBytesConfig(
|
|
| 22 |
bnb_4bit_use_double_quant=True,
|
| 23 |
)
|
| 24 |
|
| 25 |
-
|
| 26 |
-
print("Loading Elephant Engine (Mistral-7B)...")
|
| 27 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=HF_TOKEN)
|
| 28 |
model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
model_id,
|
|
@@ -32,54 +45,84 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 32 |
token=HF_TOKEN
|
| 33 |
)
|
| 34 |
|
| 35 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def get_live_data(query):
|
| 37 |
try:
|
| 38 |
with DDGS() as ddgs:
|
| 39 |
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 40 |
return "\n".join(results)
|
| 41 |
-
except:
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
@app.get("/")
|
| 45 |
-
def
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
@app.post("/v1/chat")
|
| 49 |
-
async def
|
| 50 |
-
# API Key එක පරීක්ෂා කිරීම
|
| 51 |
if x_api_key not in API_KEYS_DB:
|
| 52 |
-
raise HTTPException(status_code=403, detail="
|
| 53 |
|
| 54 |
user_query = message.get("query", "")
|
| 55 |
-
|
| 56 |
-
# 2026 දත්ත සඳහා Web Search කිරීම
|
| 57 |
context = ""
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
context = get_live_data(user_query)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
system_instr = "Current Year: 2026. You are Elephant AI. Use the provided context to answer."
|
| 63 |
-
full_prompt = f"System: {system_instr}\nContext: {context}\nUser: {user_query}\nAssistant:"
|
| 64 |
-
|
| 65 |
inputs = tokenizer(full_prompt, return_tensors="pt").to("cuda")
|
| 66 |
|
| 67 |
-
# Response එක Generate කිරීම
|
| 68 |
with torch.no_grad():
|
| 69 |
output_tokens = model.generate(**inputs, max_new_tokens=300, do_sample=True, temperature=0.7)
|
| 70 |
|
| 71 |
response = tokenizer.decode(output_tokens[0], skip_special_tokens=True).split("Assistant:")[-1].strip()
|
| 72 |
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
log_entry = {"q": user_query, "ctx": context, "ans": response, "key": x_api_key}
|
| 76 |
-
f.write(json.dumps(log_entry) + "\n")
|
| 77 |
|
| 78 |
return {
|
| 79 |
-
"reply": response,
|
| 80 |
-
"model": "Elephant-Mistral-7B-v0.3",
|
| 81 |
"key_id": x_api_key,
|
| 82 |
-
"
|
| 83 |
}
|
| 84 |
|
| 85 |
main = app
|
|
|
|
| 1 |
from fastapi import FastAPI, Header, HTTPException
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
import torch
|
| 5 |
import os
|
| 6 |
import json
|
| 7 |
+
import datetime
|
| 8 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
| 9 |
from duckduckgo_search import DDGS
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
+
# CORS Settings
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_methods=["*"],
|
| 18 |
+
allow_headers=["*"],
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
# --- දත්ත ගබඩාව සහ මතකය (Learning Path) ---
|
| 22 |
+
API_KEYS_DB = {
|
| 23 |
+
"ELE-PRIME-ADMIN-SYS": {"credits": 999999, "status": "active"}
|
| 24 |
+
}
|
| 25 |
+
ADMIN_SECRET = "MINZO-SECRET-2026"
|
| 26 |
+
LEARNING_VAULT_PATH = "neural_learning_data.jsonl" # මෙතන තමයි AI එක ඉගෙන ගන්න දත්ත Save වෙන්නේ
|
| 27 |
|
| 28 |
+
# --- AI Model සැකසුම් ---
|
| 29 |
model_id = "mistralai/Mistral-7B-v0.3"
|
| 30 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 31 |
|
|
|
|
| 36 |
bnb_4bit_use_double_quant=True,
|
| 37 |
)
|
| 38 |
|
| 39 |
+
print("🐘 Elephant Learning Engine Loading...")
|
|
|
|
| 40 |
tokenizer = AutoTokenizer.from_pretrained(model_id, token=HF_TOKEN)
|
| 41 |
model = AutoModelForCausalLM.from_pretrained(
|
| 42 |
model_id,
|
|
|
|
| 45 |
token=HF_TOKEN
|
| 46 |
)
|
| 47 |
|
| 48 |
+
# --- Helpers ---
|
| 49 |
+
class NewKeyRequest(BaseModel):
|
| 50 |
+
admin_pass: str
|
| 51 |
+
new_key: str
|
| 52 |
+
|
| 53 |
def get_live_data(query):
|
| 54 |
try:
|
| 55 |
with DDGS() as ddgs:
|
| 56 |
results = [r['body'] for r in ddgs.text(query, max_results=3)]
|
| 57 |
return "\n".join(results)
|
| 58 |
+
except: return ""
|
| 59 |
+
|
| 60 |
+
# 🧠 CONTINUOUS LEARNING FUNCTION
|
| 61 |
+
# පද්ධතිය විසින් අලුත් දැනුම ගබඩා කරගන්නා ආකාරය
|
| 62 |
+
def capture_learning_data(query, context, response, key_id):
|
| 63 |
+
timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 64 |
+
learning_entry = {
|
| 65 |
+
"timestamp": timestamp,
|
| 66 |
+
"key_node": key_id,
|
| 67 |
+
"instruction": query,
|
| 68 |
+
"external_context": context,
|
| 69 |
+
"trained_output": response
|
| 70 |
+
}
|
| 71 |
+
# JSONL ගොනුවකට දත්ත එකතු කිරීම
|
| 72 |
+
with open(LEARNING_VAULT_PATH, "a", encoding="utf-8") as f:
|
| 73 |
+
f.write(json.dumps(learning_entry) + "\n")
|
| 74 |
+
print(f"📊 Neural Data Captured via {key_id}")
|
| 75 |
+
|
| 76 |
+
# --- Endpoints ---
|
| 77 |
|
| 78 |
@app.get("/")
|
| 79 |
+
def home():
|
| 80 |
+
# පද්ධතිය කොච්චර ඉගෙන ගෙන තියෙනවද කියලා බලාගන්න
|
| 81 |
+
learning_count = 0
|
| 82 |
+
if os.path.exists(LEARNING_VAULT_PATH):
|
| 83 |
+
with open(LEARNING_VAULT_PATH, "r") as f:
|
| 84 |
+
learning_count = sum(1 for line in f)
|
| 85 |
+
|
| 86 |
+
return {
|
| 87 |
+
"status": "Elephant AI Node 2026 Live",
|
| 88 |
+
"active_keys": len(API_KEYS_DB),
|
| 89 |
+
"learning_entries_captured": learning_count
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
@app.post("/admin/add-key")
|
| 93 |
+
async def register_key(data: NewKeyRequest):
|
| 94 |
+
if data.admin_pass != ADMIN_SECRET:
|
| 95 |
+
raise HTTPException(status_code=401, detail="Unauthorized Admin Access")
|
| 96 |
+
API_KEYS_DB[data.new_key] = {"credits": 5000, "status": "active"}
|
| 97 |
+
return {"message": f"Key {data.new_key} Registered Successfully"}
|
| 98 |
|
| 99 |
@app.post("/v1/chat")
|
| 100 |
+
async def chat_endpoint(message: dict, x_api_key: str = Header(None)):
|
|
|
|
| 101 |
if x_api_key not in API_KEYS_DB:
|
| 102 |
+
raise HTTPException(status_code=403, detail="Invalid API Key")
|
| 103 |
|
| 104 |
user_query = message.get("query", "")
|
|
|
|
|
|
|
| 105 |
context = ""
|
| 106 |
+
|
| 107 |
+
# 🌐 2026 Live Web Search
|
| 108 |
+
if any(word in user_query.lower() for word in ["today", "now", "2026", "news", "price"]):
|
| 109 |
context = get_live_data(user_query)
|
| 110 |
|
| 111 |
+
full_prompt = f"System: Year 2026. Context: {context}\nUser: {user_query}\nAssistant:"
|
|
|
|
|
|
|
|
|
|
| 112 |
inputs = tokenizer(full_prompt, return_tensors="pt").to("cuda")
|
| 113 |
|
|
|
|
| 114 |
with torch.no_grad():
|
| 115 |
output_tokens = model.generate(**inputs, max_new_tokens=300, do_sample=True, temperature=0.7)
|
| 116 |
|
| 117 |
response = tokenizer.decode(output_tokens[0], skip_special_tokens=True).split("Assistant:")[-1].strip()
|
| 118 |
|
| 119 |
+
# 🧠 දියුණු වීමට අවශ්ය දත්ත ගබඩා කිරීම (Learning Trigger)
|
| 120 |
+
capture_learning_data(user_query, context, response, x_api_key)
|
|
|
|
|
|
|
| 121 |
|
| 122 |
return {
|
| 123 |
+
"reply": response,
|
|
|
|
| 124 |
"key_id": x_api_key,
|
| 125 |
+
"learning_status": "synced"
|
| 126 |
}
|
| 127 |
|
| 128 |
main = app
|