repleeka commited on
Commit
b9b9496
·
verified ·
1 Parent(s): 1ab09d4

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +61 -0
  2. requirements.txt +0 -0
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 Nyishi Translator",
9
+ page_icon=":repeat:",
10
+ layout="wide",
11
+ )
12
+
13
+ # Streamlit app setup
14
+ st.title(":repeat: English to Nyishi Translator")
15
+ st.markdown("Welcome to the English to Nyishi Translator. :sparkles: Simply enter your text in English, and get the translation in Nyishi 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-nyi-nmt"
24
+
25
+ device = 0 if torch.cuda.is_available() else -1
26
+ translation_pipeline = pipeline(
27
+ task="translation",
28
+ model="repleeka/eng-nyi-nmt",
29
+ tokenizer="repleeka/eng-nyi-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 = ""
requirements.txt ADDED
Binary file (230 Bytes). View file