TechnoByte commited on
Commit
473c11d
ยท
verified ยท
1 Parent(s): faa5266

multi line support

Browse files
Files changed (1) hide show
  1. app.py +51 -39
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: {input_text}")
35
-
36
- # Prepare the input using the chat template
37
- messages = [
38
- {"role": "user", "content": input_text}
39
- ]
40
- try:
41
- # Apply chat template
42
- prompt_text = tokenizer.apply_chat_template(
43
- messages,
44
- tokenize=False,
45
- add_generation_prompt=True
46
- )
47
-
48
- # Tokenize the input
49
- model_inputs = tokenizer([prompt_text], return_tensors="pt").to(model.device)
50
-
51
- print("Generating translation...")
52
- # Generate the translation
53
- generated_ids = model.generate(
54
- **model_inputs,
55
- max_new_tokens=MAX_NEW_TOKENS,
56
- do_sample=False
57
- )
58
-
59
- # Decode the generated text, skipping the prompt part
60
- # Find the length of the input prompt tokens
61
- input_ids_len = model_inputs.input_ids.shape[1]
62
- # Slice the generated_ids to get only the new tokens
63
- output_ids = generated_ids[0][input_ids_len:]
64
-
65
- # Decode the output tokens
66
- response = tokenizer.decode(output_ids, skip_special_tokens=True)
67
-
68
- print(f"Generated response: {response}")
69
- return response
 
 
 
 
 
 
 
 
 
 
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,