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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -44
app.py CHANGED
@@ -10,7 +10,7 @@ from duckduckgo_search import DDGS
10
 
11
  app = FastAPI()
12
 
13
- # CORS Fix
14
  app.add_middleware(
15
  CORSMiddleware,
16
  allow_origins=["*"],
@@ -18,81 +18,127 @@ app.add_middleware(
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 ---
39
- class NewKeyRequest(BaseModel):
40
  admin_pass: str
41
  new_key: str
 
42
 
43
- def get_live_data(query):
 
 
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: 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
 
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