Spaces:
Runtime error
Runtime error
multi line support
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
5 |
|
6 |
# --- Configuration ---
|
7 |
MODEL_NAME = "TechnoByte/Qwen2.5-7B-VNTL-JP-EN"
|
8 |
-
MAX_NEW_TOKENS = 512 # Max length of the generated translation
|
9 |
|
10 |
# --- Load Model and Tokenizer ---
|
11 |
# Load the model and tokenizer only once when the app starts
|
@@ -26,47 +26,57 @@ except Exception as e:
|
|
26 |
@spaces.GPU(duration=20)
|
27 |
def translate_japanese_to_english(input_text):
|
28 |
"""
|
29 |
-
Translates Japanese text to English using the loaded model.
|
30 |
"""
|
31 |
if not input_text:
|
32 |
return "Please enter some Japanese text to translate."
|
33 |
|
34 |
-
print(f"Received input
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
except Exception as e:
|
72 |
print(f"Error during translation: {e}")
|
@@ -83,7 +93,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
83 |
with gr.Row():
|
84 |
with gr.Column(scale=1):
|
85 |
input_textbox = gr.Textbox(
|
86 |
-
lines=5,
|
87 |
label="Japanese Input Text"
|
88 |
)
|
89 |
translate_button = gr.Button("Translate", variant="primary")
|
@@ -108,6 +118,8 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
108 |
["ใใฎใฝใใใฆใงใขใฎไฝฟใๆนใใใใใใใพใใใ"],
|
109 |
["ๆๆฅใฎๅคฉๆฐใฏใฉใใชใใพใใ๏ผ"],
|
110 |
["ๆฅๆฌใฎๆๅใซใคใใฆใใฃใจ็ฅใใใใงใใ"],
|
|
|
|
|
111 |
],
|
112 |
inputs=input_textbox,
|
113 |
outputs=output_textbox,
|
|
|
5 |
|
6 |
# --- Configuration ---
|
7 |
MODEL_NAME = "TechnoByte/Qwen2.5-7B-VNTL-JP-EN"
|
8 |
+
MAX_NEW_TOKENS = 512 # Max length of the generated translation per line
|
9 |
|
10 |
# --- Load Model and Tokenizer ---
|
11 |
# Load the model and tokenizer only once when the app starts
|
|
|
26 |
@spaces.GPU(duration=20)
|
27 |
def translate_japanese_to_english(input_text):
|
28 |
"""
|
29 |
+
Translates Japanese text to English using the loaded model, processing line by line.
|
30 |
"""
|
31 |
if not input_text:
|
32 |
return "Please enter some Japanese text to translate."
|
33 |
|
34 |
+
print(f"Received input:\n{input_text}")
|
35 |
+
|
36 |
+
lines = input_text.splitlines() # Split input into lines
|
37 |
+
translated_lines = []
|
38 |
+
|
39 |
+
try: # Wrap the entire multi-line processing
|
40 |
+
for line in lines:
|
41 |
+
if not line.strip(): # If the line is empty or just whitespace
|
42 |
+
translated_lines.append("") # Keep the empty line structure
|
43 |
+
continue # Skip processing for this empty line
|
44 |
+
|
45 |
+
print(f"Translating line: {line}")
|
46 |
+
|
47 |
+
# Prepare the input for the current line using the chat template
|
48 |
+
messages = [
|
49 |
+
{"role": "user", "content": line}
|
50 |
+
]
|
51 |
+
# Apply chat template
|
52 |
+
prompt_text = tokenizer.apply_chat_template(
|
53 |
+
messages,
|
54 |
+
tokenize=False,
|
55 |
+
add_generation_prompt=True
|
56 |
+
)
|
57 |
+
|
58 |
+
# Tokenize the input for the current line
|
59 |
+
model_inputs = tokenizer([prompt_text], return_tensors="pt").to(model.device)
|
60 |
+
|
61 |
+
# Generate the translation for the current line
|
62 |
+
generated_ids = model.generate(
|
63 |
+
**model_inputs,
|
64 |
+
max_new_tokens=MAX_NEW_TOKENS,
|
65 |
+
do_sample=False # Use greedy decoding for consistency
|
66 |
+
)
|
67 |
+
|
68 |
+
# Decode the generated text, skipping the prompt part
|
69 |
+
input_ids_len = model_inputs.input_ids.shape[1]
|
70 |
+
output_ids = generated_ids[0][input_ids_len:]
|
71 |
+
response = tokenizer.decode(output_ids, skip_special_tokens=True).strip() # Strip leading/trailing whitespace from the translation
|
72 |
+
|
73 |
+
print(f"Generated response for line: {response}")
|
74 |
+
translated_lines.append(response)
|
75 |
+
|
76 |
+
# Join the translated lines back together with newline characters
|
77 |
+
final_translation = "\n".join(translated_lines)
|
78 |
+
print(f"Final combined translation:\n{final_translation}")
|
79 |
+
return final_translation
|
80 |
|
81 |
except Exception as e:
|
82 |
print(f"Error during translation: {e}")
|
|
|
93 |
with gr.Row():
|
94 |
with gr.Column(scale=1):
|
95 |
input_textbox = gr.Textbox(
|
96 |
+
lines=5, # Keep initial size, but it can grow
|
97 |
label="Japanese Input Text"
|
98 |
)
|
99 |
translate_button = gr.Button("Translate", variant="primary")
|
|
|
118 |
["ใใฎใฝใใใฆใงใขใฎไฝฟใๆนใใใใใใใพใใใ"],
|
119 |
["ๆๆฅใฎๅคฉๆฐใฏใฉใใชใใพใใ๏ผ"],
|
120 |
["ๆฅๆฌใฎๆๅใซใคใใฆใใฃใจ็ฅใใใใงใใ"],
|
121 |
+
["ใใใซใกใฏใ\nๅ
ๆฐใงใใ๏ผ\n็งใฏๅ
ๆฐใงใใ"], # Multi-line example
|
122 |
+
["ใใใฏๆๅใฎ่กใงใใ\n\nใใใฏ๏ผ่ก็ฎใงใใ็ฉบ่กใๆใฟใพใใ"] # Example with empty line
|
123 |
],
|
124 |
inputs=input_textbox,
|
125 |
outputs=output_textbox,
|