TiberiuCristianLeon commited on
Commit
94f6f84
·
verified ·
1 Parent(s): b33232d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -62
app.py CHANGED
@@ -553,72 +553,72 @@ with st.container(border=None, width="stretch", height="content", horizontal=Fal
553
 
554
  # Handle the submit button click
555
  if submit_button:
556
- if model_name.startswith('Helsinki-NLP'):
557
- # input_ids = tokenizer.encode(input_text, return_tensors='pt')
558
- # # Perform translation
559
- # output_ids = model.generate(input_ids)
560
- # # Decode the translated text
561
- # translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
562
- # Use a pipeline as a high-level helper
563
- try:
564
- model_name = f"Helsinki-NLP/opus-mt-{sl}-{tl}"
565
- tokenizer = AutoTokenizer.from_pretrained(model_name)
566
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
567
- pipe = pipeline("translation", model=model, tokenizer=tokenizer)
568
- except (EnvironmentError, OSError):
569
- model_name = f"Helsinki-NLP/opus-tatoeba-{sl}-{tl}"
570
- tokenizer = AutoTokenizer.from_pretrained(model_name)
571
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
572
- pipe = pipeline("translation", model=model, tokenizer=tokenizer)
573
- translation = pipe(input_text)
574
- translated_text = translation[0]['translation_text']
575
-
576
- elif model_name.startswith('Google'):
577
- url = os.environ['GCLIENT'] + f'sl={sl}&tl={tl}&q={input_text}'
578
- response = httpx.get(url)
579
- translated_text = response.json()[0][0][0]
580
- print(response.json()[0][0])
581
- elif model_name.startswith('t5'):
582
- tokenizer = T5Tokenizer.from_pretrained(model_name)
583
- model = T5ForConditionalGeneration.from_pretrained(model_name).to(device)
584
- prompt = f'translate {sselected_language} to {tselected_language}: {input_text}'
585
- input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)
586
- # Perform translation
587
- output_ids = model.generate(input_ids)
588
- # Decode the translated text
589
- translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
590
- elif 'Unbabel' in model_name:
591
- pipe = pipeline("text-generation", model=model_name, torch_dtype=torch.bfloat16, device_map="auto")
592
- # We use the tokenizer’s chat template to format each message - see https://huggingface.co/docs/transformers/main/en/chat_templating
593
- messages = [{"role": "user",
594
- "content": f"Translate the following text from {sselected_language} into {tselected_language}.\n{sselected_language}: {input_text}.\n{tselected_language}:"}]
595
- prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
596
- outputs = pipe(prompt, max_new_tokens=256, do_sample=False)
597
- translated_text = outputs[0]["generated_text"]
598
- start_marker = "<end_of_turn>"
599
- if start_marker in translated_text:
600
- translated_text = translated_text.split(start_marker)[1].strip()
601
- translated_text = translated_text.replace('Answer:', '').strip() if translated_text.startswith('Answer:') else translated_text
602
- elif 'Argos' in model_name:
603
- import argostranslate.translate
604
- # Translate
605
- try:
606
- download_argos_model(sl, tl)
607
- translated_text = argostranslate.translate.translate(input_text, sl, tl)
608
- except StopIteration:
609
- translated_text = f"No Argos model for {sselected_language} to {tselected_language}. Try other model or languages combination!"
610
- except Exception as error:
611
- translated_text = error
612
- elif model_name == "winninghealth/WiNGPT-Babel-2":
613
- translated_text = wingpt(model_name, sselected_language, tselected_language, input_text)
 
614
 
615
  # Display the translated text
616
  print(f"Translated from {sselected_language} to {tselected_language} using {model_name}.", input_text, translated_text)
617
  message = f"Translated from {sselected_language} to {tselected_language} using {model_name}."
618
  # translated_textarea.text_area(":green[Translation:]", value=translated_text)
619
  # message_textarea.text_input(":blue[Message:]", value=message)
620
- with st.spinner("Translating...", show_time=True):
621
- st.success(message)
622
- st.text_area(":green[Translation:]", placeholder="Translation area", value=translated_text)
623
- st.text_input(":blue[Messages:]", placeholder="Messages area", value=message)
624
  # st.rerun()
 
553
 
554
  # Handle the submit button click
555
  if submit_button:
556
+ with st.spinner("Translating...", show_time=True):
557
+ if model_name.startswith('Helsinki-NLP'):
558
+ # input_ids = tokenizer.encode(input_text, return_tensors='pt')
559
+ # # Perform translation
560
+ # output_ids = model.generate(input_ids)
561
+ # # Decode the translated text
562
+ # translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
563
+ # Use a pipeline as a high-level helper
564
+ try:
565
+ model_name = f"Helsinki-NLP/opus-mt-{sl}-{tl}"
566
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
567
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
568
+ pipe = pipeline("translation", model=model, tokenizer=tokenizer)
569
+ except (EnvironmentError, OSError):
570
+ model_name = f"Helsinki-NLP/opus-tatoeba-{sl}-{tl}"
571
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
572
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
573
+ pipe = pipeline("translation", model=model, tokenizer=tokenizer)
574
+ translation = pipe(input_text)
575
+ translated_text = translation[0]['translation_text']
576
+
577
+ elif model_name.startswith('Google'):
578
+ url = os.environ['GCLIENT'] + f'sl={sl}&tl={tl}&q={input_text}'
579
+ response = httpx.get(url)
580
+ translated_text = response.json()[0][0][0]
581
+ print(response.json()[0][0])
582
+ elif model_name.startswith('t5'):
583
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
584
+ model = T5ForConditionalGeneration.from_pretrained(model_name).to(device)
585
+ prompt = f'translate {sselected_language} to {tselected_language}: {input_text}'
586
+ input_ids = tokenizer.encode(prompt, return_tensors='pt').to(device)
587
+ # Perform translation
588
+ output_ids = model.generate(input_ids)
589
+ # Decode the translated text
590
+ translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
591
+ elif 'Unbabel' in model_name:
592
+ pipe = pipeline("text-generation", model=model_name, torch_dtype=torch.bfloat16, device_map="auto")
593
+ # We use the tokenizer’s chat template to format each message - see https://huggingface.co/docs/transformers/main/en/chat_templating
594
+ messages = [{"role": "user",
595
+ "content": f"Translate the following text from {sselected_language} into {tselected_language}.\n{sselected_language}: {input_text}.\n{tselected_language}:"}]
596
+ prompt = pipe.tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=False)
597
+ outputs = pipe(prompt, max_new_tokens=256, do_sample=False)
598
+ translated_text = outputs[0]["generated_text"]
599
+ start_marker = "<end_of_turn>"
600
+ if start_marker in translated_text:
601
+ translated_text = translated_text.split(start_marker)[1].strip()
602
+ translated_text = translated_text.replace('Answer:', '').strip() if translated_text.startswith('Answer:') else translated_text
603
+ elif 'Argos' in model_name:
604
+ import argostranslate.translate
605
+ # Translate
606
+ try:
607
+ download_argos_model(sl, tl)
608
+ translated_text = argostranslate.translate.translate(input_text, sl, tl)
609
+ except StopIteration:
610
+ translated_text = f"No Argos model for {sselected_language} to {tselected_language}. Try other model or languages combination!"
611
+ except Exception as error:
612
+ translated_text = error
613
+ elif model_name == "winninghealth/WiNGPT-Babel-2":
614
+ translated_text = wingpt(model_name, sselected_language, tselected_language, input_text)
615
 
616
  # Display the translated text
617
  print(f"Translated from {sselected_language} to {tselected_language} using {model_name}.", input_text, translated_text)
618
  message = f"Translated from {sselected_language} to {tselected_language} using {model_name}."
619
  # translated_textarea.text_area(":green[Translation:]", value=translated_text)
620
  # message_textarea.text_input(":blue[Message:]", value=message)
621
+ st.text_area(":green[Translation:]", placeholder="Translation area", value=translated_text)
622
+ st.success(message)
623
+ st.text_input(":blue[Messages:]", value=message)
 
624
  # st.rerun()