Update app.py
Browse files123456789101112131415161718192021222324252627282930313233import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLMimport torchmodel_name = "LiquidAI/LFM2.5-1.2B-Instruct"tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(model_name,trust_remote_code=True,torch_dtype=torch.float16,device_map="auto")def translate_jp_to_th(text):messages = [{"role": "system", "content": "คุณคือนักแปลมังงะ แปลญี่ปุ่นเป็นไทยธรรมชาติ ส่งคืนเฉพาะข้อความไทยเท่านั้น"},{"role": "user", "content": text}]inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)output = model.generate(inputs, max_new_tokens=100, temperature=0.7, do_sample=True)response = tokenizer.decode(output[0], skip_special_tokens=True)# ตัดส่วน prompt ออก เหลือแต่ outputreturn response.split("assistant")[-1].strip() if "assistant" in response else response.strip()demo = gr.Interface(fn=translate_jp_to_th,inputs=gr.Textbox(label="ใส่ข้อความญี่ปุ่น (เช่น くそっ!逃げろ!!)"),outputs=gr.Textbox(label="แปลเป็นไทย"),title="Japanese to Thai Manga Translator (LFM2.5-1.2B-Instruct)",description="ทดสอบแปลมังงะญี่ปุ่น → ไทย ด้วย LiquidAI LFM2.5-1.2B-Instruct")demo.launch()12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697import gradio as grfrom transformers import AutoTokenizer, AutoModelForCausalLMimport torchimport re# ใช้โมเดล Typhoon Translate 4B (เก่ง EN/TH สุด ๆ สำหรับงานนี้)model_name = "typhoon-ai/typhoon-translate-4b" # หรือลอง "scb10x/typhoon-1.5x-1b-chat" ถ้าอยากขนาดเล็กกว่าprint(f"Loading model: {model_name}")tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained(model_name,torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,device_map="auto")model.eval() # โหมด inferencedef translate_to_thai(text, source_lang="English"):"""แปลจาก source_lang เป็นไทยธรรมชาติ สไตล์มังงะ/บทสนทนา"""if not text.strip():return "กรุณาใส่ข้อความที่จะแปล"# Prompt ที่แข็งแรงสำหรับ Typhoon (ปรับให้เหมาะมังงะ)system_prompt = """คุณคือนักแปลมังงะ/การ์ตูนมืออาชีพ แปลข้อความจากภาษาต้นทางเป็นภาษาไทยที่เป็นธรรมชาติ อ่านง่าย สไตล์บทสนทนามังงะ ใช้ภาษาพูดเหมาะกับตัวละคร รักษาความหมายเดิม 100% อย่าเพิ่มหรือตัดเนื้อหาส่งคืนเฉพาะข้อความภาษาไทยล้วน ๆ เท่านั้น ห้ามมีคำอธิบาย ห้ามมี "แปลว่า" หรือเครื่องหมายพิเศษ ห้ามตอบภาษาอื่น"""user_prompt = f"แปลข้อความภาษา{source_lang}นี้เป็นไทย:\n{text}"messages = [{"role": "system", "content": system_prompt},{"role": "user", "content": user_prompt}]# Apply chat template (Typhoon ใช้ format คล้าย Mistral/Gemma)inputs = tokenizer.apply_chat_template(messages,add_generation_prompt=True,return_tensors="pt").to(model.device)with torch.no_grad():output_ids = model.generate(inputs,max_new_tokens=150, # เพิ่มเผื่อ bubble ยาวtemperature=0.7,top_p=0.9,repetition_penalty=1.1,do_sample=True)response = tokenizer.decode(output_ids[0], skip_special_tokens=True)# Clean output: ตัดส่วน prompt ออก เหลือแต่คำแปล# Typhoon มักตอบหลัง "assistant" หรือตรง ๆif "assistant" in response.lower():response = response.split("assistant", 1)[-1].strip()# ลบ input ที่อาจติดมาด้วย regexresponse = re.sub(r'^.*?:\s*', '', response).strip()response = response.replace(text, "").strip() # ลบข้อความต้นฉบับถ้าติดreturn response if response else "แปลไม่ได้ ลองข้อความอื่นหรือปรับ prompt"# Gradio Interfacedemo = gr.Interface(fn=translate_to_thai,inputs=[gr.Textbox(label="ใส่ข้อความที่จะแปล (English หรือ Japanese จากมังงะ)",placeholder="เช่น: Damn it! Run away!! หรือ くそっ!逃げろ!!",lines=5),gr.Dropdown(choices=["English", "Japanese"],value="English",label="ภาษาต้นทาง")],outputs=gr.Textbox(label="แปลเป็นภาษาไทย (ธรรมชาติ สไตล์มังงะ)"),title="Manga Translator: English/Japanese → Thai",description="""ใช้โมเดล Typhoon Translate 4B แปลบทสนทนามังงะเป็นไทยธรรมชาติ รัน local/offline ได้ ส่งคืนเฉพาะข้อความไทยล้วน- ตัวอย่าง: "What the hell?!" → "เฮ้ย อะไรวะเนี่ย!"- ถ้าเพี้ยน ลองเพิ่ม context เช่น "ตัวเอกพูดห้าว ๆ:" นำหน้า text""",examples=[["Damn it! We need to run now!!", "English"],["くそっ!逃げろ!!", "Japanese"],["Ohayo! Kyou mo kawaii ne\~", "Japanese"],["This is the best day ever!", "English"]],theme=gr.themes.Soft(primary_hue="blue"))# เปิด appdemo.launch(server_name="0.0.0.0", server_port=7860)
app.py+82-18