MINZO4546 commited on
Commit
2c0c6d1
·
verified ·
1 Parent(s): 54d701f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -74
app.py CHANGED
@@ -5,12 +5,12 @@ 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=["*"],
@@ -18,31 +18,21 @@ app.add_middleware(
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
 
32
- quant_config = BitsAndBytesConfig(
33
- load_in_4bit=True,
34
- bnb_4bit_compute_dtype=torch.bfloat16,
35
- bnb_4bit_quant_type="nf4",
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,
43
- quantization_config=quant_config,
44
- device_map="auto",
45
- token=HF_TOKEN
46
  )
47
 
48
  # --- Helpers ---
@@ -57,72 +47,52 @@ def get_live_data(query):
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
 
5
  import os
6
  import json
7
  import datetime
8
+ from transformers import AutoModelForCausalLM, AutoTokenizer
9
  from duckduckgo_search import DDGS
10
 
11
  app = FastAPI()
12
 
13
+ # CORS Fix
14
  app.add_middleware(
15
  CORSMiddleware,
16
  allow_origins=["*"],
 
18
  allow_headers=["*"],
19
  )
20
 
21
+ # --- Neural Vault Setup ---
22
+ API_KEYS_DB = {"ELE-PRIME-ADMIN-SYS": {"credits": 999999, "status": "active"}}
 
 
23
  ADMIN_SECRET = "MINZO-SECRET-2026"
24
+ LEARNING_VAULT_PATH = "neural_learning_data.jsonl"
25
 
26
+ # --- New Model Setup (CPU Optimized) ---
27
+ # Qwen 1.5B එක Mistral වලට වඩා 5 ගුණයකට වඩා සැහැල්ලුයි
28
+ model_id = "Qwen/Qwen2.5-1.5B-Instruct"
29
 
30
+ print("🐘 Elephant Light-Engine Loading on CPU...")
31
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
 
 
 
 
 
 
 
32
  model = AutoModelForCausalLM.from_pretrained(
33
  model_id,
34
+ torch_dtype="auto", # CPU එකට ගැලපෙන පරිදි auto-select වේ
35
+ device_map="cpu" # GPU නැති නිසා අනිවාර්යයෙන් CPU ලෙස දිය යුතුයි
 
36
  )
37
 
38
  # --- Helpers ---
 
47
  return "\n".join(results)
48
  except: return ""
49
 
 
 
50
  def capture_learning_data(query, context, response, key_id):
51
+ entry = {
52
+ "timestamp": str(datetime.datetime.now()),
53
+ "key": key_id,
54
+ "q": query,
55
+ "ctx": context,
56
+ "ans": response
 
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 status():
65
+ return {"status": "Elephant-Qwen Node Active", "engine": "Qwen-2.5-1.5B"}
 
 
 
 
 
 
 
 
 
 
66
 
67
  @app.post("/admin/add-key")
68
+ async def add_key(data: NewKeyRequest):
69
+ if data.admin_pass != ADMIN_SECRET: raise HTTPException(status_code=401)
 
70
  API_KEYS_DB[data.new_key] = {"credits": 5000, "status": "active"}
71
+ return {"message": "Key Registered"}
72
 
73
  @app.post("/v1/chat")
74
+ async def chat(message: dict, x_api_key: str = Header(None)):
75
+ if x_api_key not in API_KEYS_DB: raise HTTPException(status_code=403)
 
76
 
77
+ query = message.get("query", "")
78
+ context = get_live_data(query) if any(w in query.lower() for w in ["now", "2026", "today"]) else ""
79
+
80
+ # Qwen Chat Template එක භාවිතය
81
+ messages = [
82
+ {"role": "system", "content": f"You are Elephant AI. Current year: 2026. Context: {context}"},
83
+ {"role": "user", "content": query}
84
+ ]
85
+ text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
86
+ model_inputs = tokenizer([text], return_tensors="pt").to("cpu")
87
 
 
 
 
88
  with torch.no_grad():
89
+ generated_ids = model.generate(model_inputs.input_ids, max_new_tokens=512)
90
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
91
 
92
+ # පද්ධතියේ පිළිතුර පිරිසිදු කිරීම
93
+ clean_response = response.split("assistant")[-1].strip()
94
+
95
+ capture_learning_data(query, context, clean_response, x_api_key)
96
+ return {"reply": clean_response, "key_id": x_api_key}
 
 
 
 
 
97
 
98
  main = app