-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (97 loc) · 4.66 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import streamlit as st
from text_to_speech.text_to_speech import text_to_speech_app
from translator.translator import detect_language, translate
from summarizer.summarizer import summarize_text
from reader import read_txt, read_docx, read_pdf
def handle_translation_error(error):
"""Handles errors during translation and displays informative messages."""
st.error(f"An error occurred during translation: {error}")
def is_text_empty(text):
"""Checks if the entered text is empty."""
return not text.strip()
def get_file_contents(uploaded_file):
"""Reads and returns the contents of the uploaded file."""
if uploaded_file is not None:
file_contents = uploaded_file.getvalue()
if uploaded_file.type == "text/plain":
return read_txt(file_contents)
elif uploaded_file.type == "application/pdf":
return read_pdf(file_contents)
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return read_docx(file_contents)
else:
st.error("Unsupported file format")
return ""
def handle_translation_error(error):
"""Handles errors during translation and displays informative messages."""
st.error(f"An error occurred during translation: {error}")
def is_text_empty(text):
"""Checks if the entered text is empty."""
return not text.strip()
def translate_text(src_text, src_lang, trg_lang):
"""Translates the text from source language to target language."""
if not is_text_empty(src_text):
try:
translated_text = translate(src_lang, trg_lang, src_text)
if translated_text:
st.success("Translation successful:")
st.write(translated_text)
else:
st.error("Translation failed.")
except Exception as e:
handle_translation_error(e)
else:
st.warning("Please enter text to translate.")
def main():
st.sidebar.title("Choose an Option")
option = st.sidebar.selectbox(
"Select the Purpose", ["Translator", "Summarize", "Text-to-Speech"]
)
if option == "Translator":
st.title("Language Translator")
uploaded_file = st.file_uploader(
"Upload file (TXT, DOCX, PDF)", type=["txt", "docx", "pdf"])
if uploaded_file is not None:
if uploaded_file.type == "text/plain":
src_text = read_txt(uploaded_file.getvalue())
elif uploaded_file.type == "application/pdf":
src_text = read_pdf(uploaded_file.getvalue())
elif uploaded_file.type == "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
src_text = read_docx(uploaded_file.getvalue())
else:
st.error("Unsupported file format")
return
src_lang = detect_language(src_text)
target_language = st.selectbox("Select the target language:", [
"English", "Spanish", "French", "German", "Chinese (Simplified)", "Japanese", "Korean", "Russian", "Italian",
"Arabic", "Portuguese", "Dutch", "Swedish", "Danish", "Finnish", "Greek", "Hebrew", "Hindi", "Turkish", "Nepali"
])
language_codes = {
"English": "en", "Spanish": "es", "French": "fr", "German": "de", "Chinese (Simplified)": "zh-CN",
"Japanese": "ja", "Korean": "ko", "Russian": "ru", "Italian": "it", "Arabic": "ar",
"Portuguese": "pt", "Dutch": "nl", "Swedish": "sv", "Danish": "da", "Finnish": "fi",
"Greek": "el", "Hebrew": "iw", "Hindi": "hi", "Turkish": "tr", "Nepali": "ne"
}
trg_lang = language_codes.get(target_language)
if st.button("Translate"):
translate_text(src_text, src_lang, trg_lang)
elif option == "Summarize":
st.title("Text Summarizer")
uploaded_file = st.file_uploader(
"Upload file (TXT, DOCX, PDF)", type=["txt", "docx", "pdf"])
text_to_summarize = get_file_contents(uploaded_file)
st.text_area("File Content", text_to_summarize)
if st.button("Summarize"):
if not is_text_empty(text_to_summarize):
try:
summary_text = summarize_text(text_to_summarize)
st.subheader("Summary:")
st.write(summary_text)
except Exception as e:
st.error(f"An error occurred during summarization: {e}")
else:
st.warning("Please enter some text to summarize.")
elif option == "Text-to-Speech":
text_to_speech_app()
if __name__ == "__main__":
main()