MINZO4546 commited on
Commit
d04d078
·
verified ·
1 Parent(s): e9ce259

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Header, HTTPException
2
+ import torch
3
+ import json
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
5
+ from duckduckgo_search import DDGS
6
+
7
+ app = FastAPI()
8
+
9
+ # පද්ධතියේ මතකය (Storage සඳහා)
10
+ LEARNING_FILE = "/data/elephant_learning_data.jsonl" # HF Storage path
11
+
12
+ # 18GB RAM එකට ගැලපෙන පරිදි Mistral 4-bit වලින් Load කිරීම
13
+ model_id = "mistralai/Mistral-7B-v0.3"
14
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ model_id,
17
+ torch_dtype=torch.bfloat16,
18
+ device_map="auto",
19
+ load_in_4bit=True
20
+ )
21
+
22
+ # API Keys 50 (ELE-PRIME-001 to ELE-PRIME-050)
23
+ API_KEYS = {f"ELE-PRIME-{i:03d}": {"credits": 5000} for i in range(1, 51)}
24
+
25
+ @app.get("/")
26
+ def read_root():
27
+ return {"message": "Elephant API Node 2026 is Online"}
28
+
29
+ @app.post("/v1/chat")
30
+ async def chat(message: dict, x_api_key: str = Header(None)):
31
+ if x_api_key not in API_KEYS:
32
+ raise HTTPException(status_code=403, detail="Invalid API Key")
33
+
34
+ user_query = message.get("query", "")
35
+
36
+ # Web Search for 2026 Live Data
37
+ context = ""
38
+ try:
39
+ with DDGS() as ddgs:
40
+ results = [r['body'] for r in ddgs.text(user_query, max_results=2)]
41
+ context = "\n".join(results)
42
+ except:
43
+ context = "No live data available."
44
+
45
+ # Response Generation
46
+ input_text = f"Context: {context}\nUser: {user_query}\nAssistant:"
47
+ inputs = tokenizer(input_text, return_tensors="pt").to("cuda")
48
+ outputs = model.generate(**inputs, max_new_tokens=256)
49
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True).split("Assistant:")[-1].strip()
50
+
51
+ # Learning Loop: දත්ත පසුව Fine-tuning සඳහා Save කිරීම
52
+ log_data = {"q": user_query, "a": response, "key": x_api_key}
53
+ with open("learning_log.jsonl", "a") as f:
54
+ f.write(json.dumps(log_data) + "\n")
55
+
56
+ return {"reply": response, "status": "learned"}
57
+
58
+ main = app