bto00 commited on
Commit
9ea48fb
·
1 Parent(s): bf82756

Add application file

Browse files
Files changed (1) hide show
  1. app.py +42 -14
app.py CHANGED
@@ -1,22 +1,50 @@
1
  import streamlit as st
2
- from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
3
 
4
- st.title("Translation App (Kazakh to English)")
5
- st.write("Powered by Hugging Face and Streamlit")
6
 
7
- # Load model and tokenizer
8
- st.write("Loading the model, please wait...")
9
- model = AutoModelForSeq2SeqLM.from_pretrained('issai/tilmash')
10
- tokenizer = AutoTokenizer.from_pretrained("issai/tilmash")
 
 
 
 
 
 
 
 
11
 
12
- # Initialize translation pipeline
13
- tilmash = pipeline("translation", model=model, tokenizer=tokenizer, src_lang="kaz_Cyrl", tgt_lang="eng_Latn", max_length=1000)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # Input text
16
- input_text = st.text_area("Enter text in Kazakh:")
17
  if st.button("Translate"):
18
- if input_text.strip():
19
- translation = tilmash(input_text)
20
- st.write("Translation:", translation[0]['translation_text'])
 
 
 
 
 
 
 
 
21
  else:
22
  st.write("Please enter some text to translate.")
 
1
  import streamlit as st
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer, TranslationPipeline
3
 
4
+ st.title("Multilingual Translation App")
5
+ st.write("### Powered by Hugging Face and Streamlit")
6
 
7
+ @st.cache_resource
8
+ def load_tilmash():
9
+ # Загрузка модели и токенайзера
10
+ model = AutoModelForSeq2SeqLM.from_pretrained('issai/tilmash')
11
+ tokenizer = AutoTokenizer.from_pretrained("issai/tilmash")
12
+
13
+ # Инициализация пайплайна перевода
14
+ return TranslationPipeline(
15
+ model=model,
16
+ tokenizer=tokenizer,
17
+ max_length=1000
18
+ )
19
 
20
+ tilmash = load_tilmash()
21
+
22
+ # Список поддерживаемых языков
23
+ languages = {
24
+ "Kazakh (Cyrillic)": "kaz_Cyrl",
25
+ "Russian": "rus_Cyrl",
26
+ "English": "eng_Latn",
27
+ "Turkish": "tur_Latn"
28
+ }
29
+
30
+ # Выбор исходного и целевого языка
31
+ src_lang = st.selectbox("Choose the source language:", list(languages.keys()))
32
+ tgt_lang = st.selectbox("Choose the target language:", list(languages.keys()))
33
+
34
+ # Ввод текста пользователем
35
+ user_input = st.text_area("Enter text to translate:")
36
 
 
 
37
  if st.button("Translate"):
38
+ if src_lang == tgt_lang:
39
+ st.write("Source and target languages must be different.")
40
+ elif user_input.strip():
41
+ # Выполнение перевода
42
+ translation = tilmash(
43
+ user_input,
44
+ src_lang=languages[src_lang],
45
+ tgt_lang=languages[tgt_lang]
46
+ )
47
+ st.write(f"Translation ({src_lang} → {tgt_lang}):")
48
+ st.write(translation[0]["translation_text"])
49
  else:
50
  st.write("Please enter some text to translate.")