repleeka commited on
Commit
96b4aac
1 Parent(s): 2bef433

torch import

Browse files
Files changed (1) hide show
  1. app.py +61 -60
app.py CHANGED
@@ -1,60 +1,61 @@
1
- from transformers import pipeline
2
- from datasets import Dataset
3
- import streamlit as st
4
-
5
- # Set the background color and layout with set_page_config
6
- st.set_page_config(
7
- page_title="English to Tawra Translator",
8
- page_icon=":repeat:",
9
- layout="wide",
10
- )
11
-
12
- # Streamlit app setup
13
- st.title(":repeat: English to Tawra Translator")
14
- st.markdown("Welcome to the English to Tawra Translator. :sparkles: Simply enter your text in English, and get the translation in Tawra instantly! :thumbsup:")
15
-
16
- # Text input
17
- if 'text_input' not in st.session_state:
18
- st.session_state.text_input = ""
19
- text_input = st.text_area("Enter English text to translate", height=150, value=st.session_state.text_input)
20
-
21
- # Define your model from Hugging Face
22
- model_directory = "repleeka/eng-taw-nmt"
23
-
24
- device = 0 if torch.cuda.is_available() else -1
25
- translation_pipeline = pipeline(
26
- task="translation",
27
- model="repleeka/eng-taw-nmt",
28
- tokenizer="repleeka/eng-taw-nmt",
29
- device=device
30
- )
31
-
32
- # Translate button
33
- if st.button("Translate", key="translate_button"):
34
- if text_input:
35
- with st.spinner("Translating... Please wait"):
36
- # Prepare data for translation
37
- sentences = [text_input]
38
- data = Dataset.from_dict({"text": sentences})
39
-
40
- # Apply translation
41
- try:
42
- results = data.map(lambda x: {"translation": translation_pipeline(x["text"])})
43
- result = results[0]["translation"][0]['translation_text']
44
-
45
- # Capitalize the first letter of the result
46
- result = result.capitalize()
47
-
48
- # Display translation result with custom styling
49
- st.markdown("#### Translated text:")
50
- st.markdown(f'<h2 class="result-text">{result}</2>', unsafe_allow_html=True)
51
- # st.markdown(result)
52
-
53
- except Exception as e:
54
- st.error(f"Translation error: {e}")
55
- else:
56
- st.warning("Please enter text to translate.")
57
-
58
- # Clear input button
59
- if st.button("Clear Input"):
60
- st.session_state.text_input = ""
 
 
1
+ from transformers import pipeline
2
+ from datasets import Dataset
3
+ import streamlit as st
4
+ import torch
5
+
6
+ # Set the background color and layout with set_page_config
7
+ st.set_page_config(
8
+ page_title="English to Tawra Translator",
9
+ page_icon=":repeat:",
10
+ layout="wide",
11
+ )
12
+
13
+ # Streamlit app setup
14
+ st.title(":repeat: English to Tawra Translator")
15
+ st.markdown("Welcome to the English to Tawra Translator. :sparkles: Simply enter your text in English, and get the translation in Tawra instantly! :thumbsup:")
16
+
17
+ # Text input
18
+ if 'text_input' not in st.session_state:
19
+ st.session_state.text_input = ""
20
+ text_input = st.text_area("Enter English text to translate", height=150, value=st.session_state.text_input)
21
+
22
+ # Define your model from Hugging Face
23
+ model_directory = "repleeka/eng-taw-nmt"
24
+
25
+ device = 0 if torch.cuda.is_available() else -1
26
+ translation_pipeline = pipeline(
27
+ task="translation",
28
+ model="repleeka/eng-taw-nmt",
29
+ tokenizer="repleeka/eng-taw-nmt",
30
+ device=device
31
+ )
32
+
33
+ # Translate button
34
+ if st.button("Translate", key="translate_button"):
35
+ if text_input:
36
+ with st.spinner("Translating... Please wait"):
37
+ # Prepare data for translation
38
+ sentences = [text_input]
39
+ data = Dataset.from_dict({"text": sentences})
40
+
41
+ # Apply translation
42
+ try:
43
+ results = data.map(lambda x: {"translation": translation_pipeline(x["text"])})
44
+ result = results[0]["translation"][0]['translation_text']
45
+
46
+ # Capitalize the first letter of the result
47
+ result = result.capitalize()
48
+
49
+ # Display translation result with custom styling
50
+ st.markdown("#### Translated text:")
51
+ st.markdown(f'<h2 class="result-text">{result}</2>', unsafe_allow_html=True)
52
+ # st.markdown(result)
53
+
54
+ except Exception as e:
55
+ st.error(f"Translation error: {e}")
56
+ else:
57
+ st.warning("Please enter text to translate.")
58
+
59
+ # Clear input button
60
+ if st.button("Clear Input"):
61
+ st.session_state.text_input = ""