Create main.py
Browse files
main.py
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_name = "LiquidAI/LFM2.5-1.2B-Instruct"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
model_name,
|
| 9 |
+
trust_remote_code=True,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
def translate_jp_to_th(text):
|
| 15 |
+
messages = [
|
| 16 |
+
{"role": "system", "content": "คุณคือนักแปลมังงะ แปลญี่ปุ่นเป็นไทยธรรมชาติ ส่งคืนเฉพาะข้อความไทยเท่านั้น"},
|
| 17 |
+
{"role": "user", "content": text}
|
| 18 |
+
]
|
| 19 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
| 20 |
+
output = model.generate(inputs, max_new_tokens=100, temperature=0.7, do_sample=True)
|
| 21 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 22 |
+
# ตัดส่วน prompt ออก เหลือแต่ output
|
| 23 |
+
return response.split("assistant")[-1].strip() if "assistant" in response else response.strip()
|
| 24 |
+
|
| 25 |
+
demo = gr.Interface(
|
| 26 |
+
fn=translate_jp_to_th,
|
| 27 |
+
inputs=gr.Textbox(label="ใส่ข้อความญี่ปุ่น (เช่น くそっ!逃げろ!!)"),
|
| 28 |
+
outputs=gr.Textbox(label="แปลเป็นไทย"),
|
| 29 |
+
title="Japanese to Thai Manga Translator (LFM2.5-1.2B-Instruct)",
|
| 30 |
+
description="ทดสอบแปลมังงะญี่ปุ่น → ไทย ด้วย LiquidAI LFM2.5-1.2B-Instruct"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
demo.launch()
|