MINZO4546 commited on
Commit
d76d7d1
·
verified ·
1 Parent(s): 13fbacd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -60
app.py CHANGED
@@ -1,14 +1,12 @@
1
- import torch
2
- from fastapi import FastAPI, Header, HTTPException
3
  from fastapi.middleware.cors import CORSMiddleware
4
  from pydantic import BaseModel
5
- from transformers import AutoModelForCausalLM, AutoTokenizer
6
- import datetime
7
 
8
- # ── API INITIALIZATION ──
9
- # Hugging Face සොයන 'main' attribute එක මෙතනට ලබා දී ඇත
10
  main = FastAPI()
11
 
 
12
  main.add_middleware(
13
  CORSMiddleware,
14
  allow_origins=["*"],
@@ -16,71 +14,45 @@ main.add_middleware(
16
  allow_headers=["*"],
17
  )
18
 
19
- # ── CONFIGURATION ──
20
- API_KEYS_DB = {
21
- "ELE-PRIME-ADMIN-SYS": {"status": "active"},
22
- }
23
  MODEL_ID = "tencent/Hy-MT1.5-1.8B-2bit"
24
-
25
- print(f"🔱 Specialist, Loading {MODEL_ID} on CPU...")
26
-
27
- # Load Tokenizer
28
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True, clean_up_tokenization_spaces=False)
29
-
30
- # Load Model Optimized for CPU
31
- model = AutoModelForCausalLM.from_pretrained(
32
- MODEL_ID,
33
- device_map="cpu", # GPU නොමැති නිසා කෙලින්ම CPU එකට ලබා දීම
34
- torch_dtype=torch.float32,
35
- trust_remote_code=True
36
  )
37
 
38
- print("🔱 Inachi-Lite is Online and Ready.")
39
-
40
- # ── DATA MODELS ──
41
  class ChatRequest(BaseModel):
42
  message: str
43
- history: list = []
44
- temperature: float = 0.7
45
- max_tokens: int = 512
46
 
47
- # ── CHAT ENDPOINT ──
48
  @main.post("/v1/chat")
49
- async def chat(request_data: ChatRequest, x_api_key: str = Header(None)):
50
- # API Key පරීක්ෂා කිරීම
51
- if not x_api_key or x_api_key not in API_KEYS_DB:
52
- raise HTTPException(status_code=403, detail="Access Denied")
53
-
54
  user_query = request_data.message.strip()
55
- today = datetime.datetime.now().strftime("%Y-%m-%d")
56
-
57
- # Prompt සකස් කිරීම
58
- prompt = f"System: You are Inachi AI, an expert assistant for MINZO-PRIME. Date: {today}\n"
59
-
60
- # History ඇතුළත් කිරීම
61
- for human, ai in request_data.history[-2:]:
62
- prompt += f"User: {human}\nAI: {ai}\n"
63
 
64
- prompt += f"User: {user_query}\nAI:"
 
65
 
66
- inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
 
 
 
 
 
 
 
67
 
68
- with torch.no_grad():
69
- outputs = model.generate(
70
- **inputs,
71
- max_new_tokens=request_data.max_tokens,
72
- temperature=request_data.temperature,
73
- do_sample=True,
74
- pad_token_id=tokenizer.eos_token_id
75
- )
76
 
77
- response_text = tokenizer.decode(outputs[0][inputs.input_ids.shape[-1]:], skip_special_tokens=True).strip()
78
-
79
- return {
80
- "reply": response_text,
81
- "status": "success"
82
- }
83
 
84
  @main.get("/")
85
- def home():
86
- return {"message": "Inachi-Lite is Running", "status": "online"}
 
1
+ from fastapi import FastAPI
 
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
+ from transformers import pipeline
5
+ import torch
6
 
 
 
7
  main = FastAPI()
8
 
9
+ # CORS සක්‍රීය කිරීම - ඕනෑම තැනක සිට සම්බන්ධ වීමට
10
  main.add_middleware(
11
  CORSMiddleware,
12
  allow_origins=["*"],
 
14
  allow_headers=["*"],
15
  )
16
 
 
 
 
 
17
  MODEL_ID = "tencent/Hy-MT1.5-1.8B-2bit"
18
+ print(f"🔱 Specialist, Loading {MODEL_ID} (Public Mode)...")
19
+
20
+ # Pipeline initialization
21
+ pipe = pipeline(
22
+ "text-generation",
23
+ model=MODEL_ID,
24
+ device_map="cpu",
25
+ model_kwargs={"trust_remote_code": True}
 
 
 
 
26
  )
27
 
 
 
 
28
  class ChatRequest(BaseModel):
29
  message: str
 
 
 
30
 
 
31
  @main.post("/v1/chat")
32
+ async def chat(request_data: ChatRequest):
 
 
 
 
33
  user_query = request_data.message.strip()
 
 
 
 
 
 
 
 
34
 
35
+ # Prompt Setup
36
+ prompt = f"User: {user_query}\nAssistant:"
37
 
38
+ # Text Generation
39
+ results = pipe(
40
+ prompt,
41
+ max_new_tokens=150,
42
+ do_sample=True,
43
+ temperature=0.7,
44
+ pad_token_id=50256
45
+ )
46
 
47
+ # Result Cleaning
48
+ generated_text = results[0]['generated_text']
49
+ reply = generated_text.split("Assistant:")[-1].strip()
 
 
 
 
 
50
 
51
+ if not reply:
52
+ reply = "I am here, MINZO-PRIME. Systems are nominal."
53
+
54
+ return {"reply": reply}
 
 
55
 
56
  @main.get("/")
57
+ def health():
58
+ return {"status": "Public Inachi-Lite Online"}