eoeooe commited on
Commit
c2fc27a
·
verified ·
1 Parent(s): ba75d94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +82 -18
app.py CHANGED
@@ -1,33 +1,97 @@
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()
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
+ import re
5
 
6
+ # ใช้โมเดล Typhoon Translate 4B (เก่ง EN/TH สุด ๆ สำหรับงานนี้)
7
+ model_name = "typhoon-ai/typhoon-translate-4b" # หรือลอง "scb10x/typhoon-1.5x-1b-chat" ถ้าอยากขนาดเล็กกว่า
8
+
9
+ print(f"Loading model: {model_name}")
10
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
11
  model = AutoModelForCausalLM.from_pretrained(
12
  model_name,
13
+ torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
 
14
  device_map="auto"
15
  )
16
+ model.eval() # โหมด inference
17
+
18
+ def translate_to_thai(text, source_lang="English"):
19
+ """
20
+ แปลจาก source_lang เป็นไทยธรรมชาติ สไตล์มังงะ/บทสนทนา
21
+ """
22
+ if not text.strip():
23
+ return "กรุณาใส่ข้อความที่จะแปล"
24
+
25
+ # Prompt ที่แข็งแรงสำหรับ Typhoon (ปรับให้เหมาะมังงะ)
26
+ system_prompt = """คุณคือนักแปลมังงะ/การ์ตูนมืออาชีพ แปลข้อความจากภาษาต้นทางเป็นภาษาไทยที่เป็นธรรมชาติ อ่านง่าย สไตล์บทสนทนามังงะ ใช้ภาษาพูดเหมาะกับตัวละคร รักษาความหมายเดิม 100% อย่าเพิ่มหรือตัดเนื้อหา
27
+
28
+ ส่งคืนเฉพาะข้อความภาษาไทยล้วน ๆ เท่านั้น ห้ามมีคำอธิบาย ห้ามมี "แปลว่า" หรือเครื่องหมายพิเศษ ห้ามตอบภาษาอื่น"""
29
+
30
+ user_prompt = f"แปลข้อความภาษา{source_lang}นี้เป็นไทย:\n{text}"
31
 
 
32
  messages = [
33
+ {"role": "system", "content": system_prompt},
34
+ {"role": "user", "content": user_prompt}
35
  ]
 
 
 
 
 
36
 
37
+ # Apply chat template (Typhoon ใช้ format คล้าย Mistral/Gemma)
38
+ inputs = tokenizer.apply_chat_template(
39
+ messages,
40
+ add_generation_prompt=True,
41
+ return_tensors="pt"
42
+ ).to(model.device)
43
+
44
+ with torch.no_grad():
45
+ output_ids = model.generate(
46
+ inputs,
47
+ max_new_tokens=150, # เพิ่มเผื่อ bubble ยาว
48
+ temperature=0.7,
49
+ top_p=0.9,
50
+ repetition_penalty=1.1,
51
+ do_sample=True
52
+ )
53
+
54
+ response = tokenizer.decode(output_ids[0], skip_special_tokens=True)
55
+
56
+ # Clean output: ตัดส่วน prompt ออก เหลือแต่คำแปล
57
+ # Typhoon มักตอบหลัง "assistant" หรือตรง ๆ
58
+ if "assistant" in response.lower():
59
+ response = response.split("assistant", 1)[-1].strip()
60
+ # ลบ input ที่อาจติดมาด้วย regex
61
+ response = re.sub(r'^.*?:\s*', '', response).strip()
62
+ response = response.replace(text, "").strip() # ลบข้อความต้นฉบับถ้าติด
63
+
64
+ return response if response else "แปลไม่ได้ ลองข้อความอื่นหรือปรับ prompt"
65
+
66
+ # Gradio Interface
67
  demo = gr.Interface(
68
+ fn=translate_to_thai,
69
+ inputs=[
70
+ gr.Textbox(
71
+ label="ใส่ข้อความที่จะแปล (English หรือ Japanese จากมังงะ)",
72
+ placeholder="เช่น: Damn it! Run away!! หรือ くそっ!逃げろ!!",
73
+ lines=5
74
+ ),
75
+ gr.Dropdown(
76
+ choices=["English", "Japanese"],
77
+ value="English",
78
+ label="ภาษาต้นทาง"
79
+ )
80
+ ],
81
+ outputs=gr.Textbox(label="แปลเป็นภาษาไทย (ธรรมชาติ สไตล์มังงะ)"),
82
+ title="Manga Translator: English/Japanese → Thai",
83
+ description="""ใช้โมเดล Typhoon Translate 4B แปลบทสนทนามังงะเป็นไทยธรรมชาติ รัน local/offline ได้ ส่งคืนเฉพาะข้อความไทยล้วน
84
+ - ตัวอย่าง: "What the hell?!" → "เฮ้ย อะไรวะเนี่ย!"
85
+ - ถ้าเพี้ยน ลองเพิ่ม context เช่น "ตัวเอกพูดห้าว ๆ:" นำหน้า text
86
+ """,
87
+ examples=[
88
+ ["Damn it! We need to run now!!", "English"],
89
+ ["くそっ!逃げろ!!", "Japanese"],
90
+ ["Ohayo! Kyou mo kawaii ne\~", "Japanese"],
91
+ ["This is the best day ever!", "English"]
92
+ ],
93
+ theme=gr.themes.Soft(primary_hue="blue")
94
  )
95
 
96
+ # เปิด app
97
+ demo.launch(server_name="0.0.0.0", server_port=7860)