zenityx commited on
Commit
0f8d476
·
verified ·
1 Parent(s): 9859dae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +114 -11
app.py CHANGED
@@ -1,19 +1,41 @@
1
  import gradio as gr
2
  from transformers import MarianMTModel, MarianTokenizer
 
3
  import random
4
 
5
- # โหลดโมเดลแปล (MarianMT)
 
 
 
 
 
6
  model_name = "Helsinki-NLP/opus-mt-th-en"
7
  tokenizer = MarianTokenizer.from_pretrained(model_name)
8
- model = MarianMTModel.from_pretrained(model_name)
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def translate_th_to_en(th_text: str) -> str:
11
  th_text = th_text.strip()
12
  if not th_text:
13
  return ""
14
- inputs = tokenizer(th_text, return_tensors="pt", max_length=512, truncation=True)
15
- translation_tokens = model.generate(**inputs, max_length=512)
16
- en_text = tokenizer.decode(translation_tokens[0], skip_special_tokens=True)
 
 
 
 
17
  return en_text
18
 
19
  # ตัวเลือก (ภาษาไทย) สำหรับ Dropdown
@@ -49,6 +71,86 @@ moods_th = [
49
  "เศร้าสร้อย", "จริงจัง", "มีความสุข", "ขี้ตกใจ"
50
  ]
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  def generate_monster_lab(
53
  body_dd, body_tb,
54
  head_dd, head_tb,
@@ -71,12 +173,13 @@ def generate_monster_lab(
71
  f"ผิว/ลายแบบ{skin_th}, และพลังพิเศษคือ{ability_th}!"
72
  )
73
 
74
- body_en = translate_th_to_en(body_th)
75
- head_en = translate_th_to_en(head_th)
76
- arms_en = translate_th_to_en(arms_th)
77
- skin_en = translate_th_to_en(skin_th)
78
- ability_en = translate_th_to_en(ability_th)
79
- mood_en = translate_th_to_en(mood_th)
 
80
 
81
  prompt_en = (
82
  f"This is a {mood_en} monster with {body_en}, "
 
1
  import gradio as gr
2
  from transformers import MarianMTModel, MarianTokenizer
3
+ import torch
4
  import random
5
 
6
+ # ตรวจสอบว่าใช้งาน GPU หรือไม่ (ในกรณีนี้ไม่มี GPU)
7
+ device = torch.device("cpu")
8
+
9
+ # เลือกโมเดลแปลภาษาที่มีขนาดเล็กลง
10
+ # คุณสามารถค้นหาโมเดลที่เล็กกว่าได้ที่ Hugging Face Model Hub
11
+ # ในตัวอย่างนี้ยังคงใช้ "Helsinki-NLP/opus-mt-th-en" แต่คุณสามารถเปลี่ยนได้ตามต้องการ
12
  model_name = "Helsinki-NLP/opus-mt-th-en"
13
  tokenizer = MarianTokenizer.from_pretrained(model_name)
14
+ model = MarianMTModel.from_pretrained(model_name).to(device)
15
+ model.eval() # ตั้งค่าโมเดลเป็นโหมดประเมินผล
16
+
17
+ # แคชสำหรับการแปลข้อความที่ไม่อยู่ในรายการคงที่
18
+ translation_cache = {}
19
+
20
+ def translate_batch(th_texts):
21
+ """แปลข้อความแบบแบทช์เพื่อเพิ่มความเร็ว"""
22
+ inputs = tokenizer(th_texts, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
23
+ with torch.no_grad():
24
+ translated = model.generate(**inputs, max_length=512)
25
+ en_texts = [tokenizer.decode(t, skip_special_tokens=True) for t in translated]
26
+ return en_texts
27
 
28
  def translate_th_to_en(th_text: str) -> str:
29
  th_text = th_text.strip()
30
  if not th_text:
31
  return ""
32
+ # ตรวจสอบแคชก่อน
33
+ if th_text in translation_cache:
34
+ return translation_cache[th_text]
35
+ # แปลข้อความ
36
+ en_text = translate_batch([th_text])[0]
37
+ # เก็บในแคช
38
+ translation_cache[th_text] = en_text
39
  return en_text
40
 
41
  # ตัวเลือก (ภาษาไทย) สำหรับ Dropdown
 
71
  "เศร้าสร้อย", "จริงจัง", "มีความสุข", "ขี้ตกใจ"
72
  ]
73
 
74
+ # แปลล่วงหน้าสำหรับรายการคงที่
75
+ # คุณสามารถแปลข้อความเหล่านี้ล่วงหน้านอกโปรแกรมและใส่ค่าลงไปตรงนี้เพื่อความรวดเร็ว
76
+ body_shapes_en = {
77
+ "ตัวเหนียวขยุกขยิก": "a squishy and wobbly body",
78
+ "ตัวปุกปุยกลม": "a round and fluffy body",
79
+ "ตัวเต็มไปด้วยหนาม": "a spiky body",
80
+ "ตัวเล็กเหมือนแมลง": "a small, insect-like body",
81
+ "ตัวโปร่งแสงลอยได้": "a translucent, floating body",
82
+ "ตัวโคลนยืดหยุ่น": "a flexible, slime-like body",
83
+ "ตัวหินแข็งแรง": "a strong, rocky body",
84
+ "ตัวทรงพีระมิด": "a pyramidal body",
85
+ "ตัวระยิบระยับ": "a sparkling body",
86
+ "ตัวเหมือนฟองสบู่": "a bubble-like body"
87
+ }
88
+
89
+ heads_en = {
90
+ "หัวตาเดียว": "a single-eyed head",
91
+ "หัวสองเขาเหมือนมังกร": "a two-horned dragon-like head",
92
+ "หัวมีเขาใหญ่มาก": "a head with very large horns",
93
+ "หัวผีแสยะยิ้ม": "a smiling ghostly head",
94
+ "หัวกระต่ายน่ารัก": "a cute rabbit head",
95
+ "หัวปลาหมึกสุดแปลก": "a bizarre squid head",
96
+ "หัวเพลิงลุก": "a flaming head",
97
+ "หัวงูพ่นพิษ": "a venomous snake head",
98
+ "หัววุ้นใส": "a transparent jelly head",
99
+ "หัวเมฆหมอก": "a misty cloud head"
100
+ }
101
+
102
+ arms_legs_en = {
103
+ "แขนหนวดปลาหมึกและขาปลาหมึก": "octopus-like tentacles and legs",
104
+ "แขนกลและเท้าสเก็ต": "mechanical arms and skating feet",
105
+ "ปีกขนนกและอุ้งเท้าสิงโต": "feathered wings and lion-like paws",
106
+ "แขนเรืองแสงและขายานโฮเวอร์": "glowing arms and hover jets",
107
+ "แขนไม้หุ่นเชิดและขายักษ์แมงมุม": "puppet wooden arms and giant spider legs",
108
+ "แขนโลหะ": "metal arms",
109
+ "แขนเหล็กข้อต่อ": "jointed iron arms",
110
+ "ปีกค้างคาว": "bat wings",
111
+ "ขาไก่ยักษ์": "giant chicken legs",
112
+ "แขนขนนุ่ม": "soft feathered arms"
113
+ }
114
+
115
+ skin_patterns_en = {
116
+ "ลายสีรุ้ง": "rainbow patterns",
117
+ "จุดม่วงเรืองแสง": "glowing purple spots",
118
+ "เกล็ดเหล็ก": "iron scales",
119
+ "ขนนกวิบวับ": "shimmering feathers",
120
+ "จุดกลมกากเพชร": "diamond-like round dots",
121
+ "ลายหินอ่อน": "marble patterns",
122
+ "ลายไฟลุก": "flaming patterns",
123
+ "ลายฟ้าแลบ": "lightning patterns",
124
+ "ลายสเกลปลา": "fish scale patterns",
125
+ "ลายหมอก": "misty patterns"
126
+ }
127
+
128
+ abilities_en = {
129
+ "พ่นไฟ": "breathe fire",
130
+ "ปล่อยฟองสบู่": "release soap bubbles",
131
+ "ควบคุมสายฟ้า": "control lightning",
132
+ "ร้องเพลงกล่อม": "sing a lullaby",
133
+ "วาร์ประยะสั้น": "short-range warp",
134
+ "เสกสิ่งของ": "conjure objects",
135
+ "ปล่อยพลังงานแสง": "emit light energy",
136
+ "ควบคุมแรงโน้มถ่วง": "control gravity",
137
+ "เปลี่ยนรูปร่าง": "shape-shift",
138
+ "เสกภาพลวงตา": "create illusions"
139
+ }
140
+
141
+ moods_en = {
142
+ "เป็นมิตร": "friendly",
143
+ "ขี้หงุดหงิด": "irritable",
144
+ "ตลกโปกฮา": "funny",
145
+ "ซนเป็นลิง": "mischievous like a monkey",
146
+ "ขี้อาย": "shy",
147
+ "ขี้เล่น": "playful",
148
+ "เศร้าสร้อย": "sorrowful",
149
+ "จริงจัง": "serious",
150
+ "มีความสุข": "happy",
151
+ "ขี้ตกใจ": "easily startled"
152
+ }
153
+
154
  def generate_monster_lab(
155
  body_dd, body_tb,
156
  head_dd, head_tb,
 
173
  f"ผิว/ลายแบบ{skin_th}, และพลังพิเศษคือ{ability_th}!"
174
  )
175
 
176
+ # แปลเป็นภาษาอังกฤษ โดยใช้การแปลล่วงหน้าสำหรับรายการคงที่
177
+ body_en = body_shapes_en.get(body_th, translate_th_to_en(body_th))
178
+ head_en = heads_en.get(head_th, translate_th_to_en(head_th))
179
+ arms_en = arms_legs_en.get(arms_th, translate_th_to_en(arms_th))
180
+ skin_en = skin_patterns_en.get(skin_th, translate_th_to_en(skin_th))
181
+ ability_en = abilities_en.get(ability_th, translate_th_to_en(ability_th))
182
+ mood_en = moods_en.get(mood_th, translate_th_to_en(mood_th))
183
 
184
  prompt_en = (
185
  f"This is a {mood_en} monster with {body_en}, "