Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
-
import
|
| 2 |
-
from fastapi import FastAPI, Header, HTTPException
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
from pydantic import BaseModel
|
| 5 |
-
from transformers import
|
| 6 |
-
import
|
| 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 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 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
|
| 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 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 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 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
"status": "success"
|
| 82 |
-
}
|
| 83 |
|
| 84 |
@main.get("/")
|
| 85 |
-
def
|
| 86 |
-
return {"
|
|
|
|
| 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"}
|