eaglelandsonce commited on
Commit
38926c5
1 Parent(s): 367af4e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
3
+
4
+ @st.cache(allow_output_mutation=True)
5
+ def load_model():
6
+
7
+ checkpoint = "CohereForAI/aya-101"
8
+ tokenizer = AutoTokenizer.from_pretrained(checkpoint)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(checkpoint)
10
+
11
+ return tokenizer, model
12
+
13
+ tokenizer, model = load_model()
14
+
15
+ st.title("Neural Machine Translation")
16
+
17
+ text = st.text_input("Enter text to translate:")
18
+ src_language = "auto"
19
+ tgt_language = st.selectbox("Translate to:", ["English", "Hindi", "Turkish"])
20
+
21
+ if st.button("Translate"):
22
+
23
+ text_input_ids = tokenizer.encode(text, return_tensors="pt")
24
+
25
+ if tgt_language == "English":
26
+ tgt_language = "en"
27
+ elif tgt_language == "Hindi":
28
+ tgt_language = "hi"
29
+ elif tgt_language == "Turkish":
30
+ tgt_language = "tr"
31
+
32
+ output_ids = model.generate(text_input_ids,
33
+ forced_bos_token_id=tokenizer.lang_code_to_id[tgt_language])
34
+ translated_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
35
+
36
+ st.write(f"Translated text in {tgt_language}:")
37
+ st.write(translated_text)