TiberiuCristianLeon commited on
Commit
11103fd
·
verified ·
1 Parent(s): 5e9286a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -6
app.py CHANGED
@@ -135,12 +135,41 @@ if submit_button:
135
  translated_text = f"No Argos model for {sselected_language} to {tselected_language}. Try other model or languages combination!"
136
  except Exception as error:
137
  translated_text = error
138
- if model_name == "winninghealth/WiNGPT-Babel-2":
139
- tokenizer = AutoTokenizer.from_pretrained("winninghealth/WiNGPT-Babel-2")
140
- model = AutoModelForCausalLM.from_pretrained("winninghealth/WiNGPT-Babel-2")
141
- pipe = pipeline("translation", model=model, tokenizer=tokenizer)
142
- translation = pipe(input_text)
143
- translated_text = translation[0]['translation_text']
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
144
  # Display the translated text
145
  print(translated_text)
146
  st.write(f"Translated text from {sselected_language} to {tselected_language} using {model_name}:")
 
135
  translated_text = f"No Argos model for {sselected_language} to {tselected_language}. Try other model or languages combination!"
136
  except Exception as error:
137
  translated_text = error
138
+ if model_name == "winninghealth/WiNGPT-Babel-2":
139
+ model = AutoModelForCausalLM.from_pretrained(
140
+ model_name,
141
+ torch_dtype="auto",
142
+ device_map="auto"
143
+ )
144
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
145
+
146
+ # Example: Translation of text within a JSON object to Chinese
147
+ prompt_json = """{"text": input_text}"""
148
+
149
+ messages = [
150
+ {"role": "system", "content": f"Translate this to {tselected_language} Language"},
151
+ {"role": "user", "content": prompt_json}
152
+ ]
153
+
154
+ text = tokenizer.apply_chat_template(
155
+ messages,
156
+ tokenize=False,
157
+ add_generation_prompt=False
158
+ )
159
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
160
+
161
+ generated_ids = model.generate(
162
+ **model_inputs,
163
+ max_new_tokens=4096,
164
+ temperature=0
165
+ )
166
+
167
+ generated_ids = [
168
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
169
+ ]
170
+
171
+ translated_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
172
+ # translated_text = translation[0]['translation_text']
173
  # Display the translated text
174
  print(translated_text)
175
  st.write(f"Translated text from {sselected_language} to {tselected_language} using {model_name}:")