Update app.py
Browse files
app.py
CHANGED
|
@@ -13,14 +13,15 @@ main.add_middleware(
|
|
| 13 |
allow_headers=["*"],
|
| 14 |
)
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
# Pipeline එක පාවිච්චි කිරීම වඩාත් ස්ථාවරයි
|
| 20 |
pipe = pipeline(
|
| 21 |
"text-generation",
|
| 22 |
model=MODEL_ID,
|
| 23 |
device_map="cpu",
|
|
|
|
| 24 |
trust_remote_code=True
|
| 25 |
)
|
| 26 |
|
|
@@ -31,34 +32,26 @@ class ChatRequest(BaseModel):
|
|
| 31 |
async def chat(request_data: ChatRequest):
|
| 32 |
user_query = request_data.message.strip()
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
results = pipe(
|
| 38 |
-
|
| 39 |
-
max_new_tokens=256,
|
| 40 |
do_sample=True,
|
| 41 |
-
temperature=0.
|
| 42 |
-
top_p=0.9
|
| 43 |
-
repetition_penalty=1.2, # 🔱 එකම දේ ලිවීම නතර කිරීමට
|
| 44 |
-
pad_token_id=50256
|
| 45 |
)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Assistant: පසුව එන කොටස වෙන් කර ගැනීම
|
| 50 |
-
if "Assistant:" in generated_text:
|
| 51 |
-
reply = generated_text.split("Assistant:")[-1].strip()
|
| 52 |
-
else:
|
| 53 |
-
reply = generated_text.replace(prompt, "").strip()
|
| 54 |
-
|
| 55 |
-
# 🔱 හිස් පිළිතුරක් ආවොත් raw generation එක පෙන්වන්න (Debug සඳහා)
|
| 56 |
-
if not reply or len(reply) < 2:
|
| 57 |
-
reply = generated_text[:100] + "..."
|
| 58 |
|
| 59 |
-
print(f"🔱
|
| 60 |
return {"reply": reply}
|
| 61 |
|
| 62 |
@main.get("/")
|
| 63 |
def health():
|
| 64 |
-
return {"status": "Online"}
|
|
|
|
| 13 |
allow_headers=["*"],
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# 🔱 Gemma 3 1B මොඩල් එක ලෝඩ් කිරීම
|
| 17 |
+
MODEL_ID = "google/gemma-3-1b-it"
|
| 18 |
+
print(f"🔱 Specialist, Upgrading to {MODEL_ID}...")
|
| 19 |
|
|
|
|
| 20 |
pipe = pipeline(
|
| 21 |
"text-generation",
|
| 22 |
model=MODEL_ID,
|
| 23 |
device_map="cpu",
|
| 24 |
+
torch_dtype=torch.float32,
|
| 25 |
trust_remote_code=True
|
| 26 |
)
|
| 27 |
|
|
|
|
| 32 |
async def chat(request_data: ChatRequest):
|
| 33 |
user_query = request_data.message.strip()
|
| 34 |
|
| 35 |
+
# Gemma 3 Chat Format
|
| 36 |
+
messages = [
|
| 37 |
+
{"role": "user", "content": user_query},
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
# Generation
|
| 41 |
results = pipe(
|
| 42 |
+
messages,
|
| 43 |
+
max_new_tokens=256,
|
| 44 |
do_sample=True,
|
| 45 |
+
temperature=0.7,
|
| 46 |
+
top_p=0.9
|
|
|
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# 🔱 පිළිතුර පමණක් වෙන් කර ගැනීම
|
| 50 |
+
reply = results[0]['generated_text'][-1]['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
print(f"🔱 Inachi Response: {reply}")
|
| 53 |
return {"reply": reply}
|
| 54 |
|
| 55 |
@main.get("/")
|
| 56 |
def health():
|
| 57 |
+
return {"status": "Gemma-3 Powered Inachi Online"}
|