-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal_p.py
More file actions
166 lines (132 loc) · 6.49 KB
/
Copy pathfinal_p.py
File metadata and controls
166 lines (132 loc) · 6.49 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import streamlit as st
from datetime import date
from gtts import gTTS, lang
from googletrans import Translator
import PyPDF2
# setting app's title, icon & layout
st.set_page_config(page_title="NexVerse", page_icon=r"E:\project\temp_image.jpg")
def get_key(val):
"""function to find the key of the given value in the dict object
Args:
val (str): value to find key
Returns:
key(str): key for the given value
"""
for key, value in lang.tts_langs().items():
if val == value:
return key
def categorize_languages():
"""Separate languages into Indian and World categories."""
indian_languages = ['Hindi', 'Bengali', 'Telugu', 'Marathi', 'Tamil', 'Urdu', 'Gujarati', 'Malayalam', 'Kannada', 'Odia', 'Punjabi']
langs = lang.tts_langs()
indian_langs = {key: value for key, value in langs.items() if value in indian_languages}
world_langs = {key: value for key, value in langs.items() if value not in indian_languages}
return indian_langs, world_langs
def read_pdf(file):
"""Reads a PDF file and extracts text from it.
Args:
file (BytesIO): BytesIO object representing the uploaded PDF file.
Returns:
str: Extracted text from the PDF.
"""
pdf_reader = PyPDF2.PdfReader(file)
text = ""
for page_num in range(len(pdf_reader.pages)):
text += pdf_reader.pages[page_num].extract_text()
return text
def translate_pdf_text(pdf_text, dest_lang):
"""Translates text extracted from a PDF to the specified destination language.
Args:
pdf_text (str): Text extracted from the PDF.
dest_lang (str): Destination language for translation.
Returns:
str: Translated text.
"""
translator = Translator()
translation = translator.translate(pdf_text, dest=dest_lang)
return translation.text
def collect_feedback():
feedback = st.sidebar.text_area("Feedback:", max_chars=500)
if st.sidebar.button("Submit Feedback") and feedback:
st.sidebar.success("Thank you for your feedback!")
def main():
# instance of Translator()
trans = Translator()
# gets gtts supported languages as dict
indian_languages, world_languages = categorize_languages()
st.header("Translate your thoughts.")
st.write(f"Date : {date.today()}")
translation_choice = st.radio("Choose an option:", ["Translate Sentence", "Translate PDF"])
if translation_choice == "Translate Sentence":
input_text = st.text_input("Enter text for translation", key="input_text")
lang_choice_category = st.selectbox("Select Language Category", ["Indian Languages", "World Languages"])
if lang_choice_category == "Indian Languages":
lang_choices = indian_languages.values()
else:
lang_choices = world_languages.values()
lang_choice = st.selectbox("Language to translate: ", lang_choices, key="lang_choice")
if st.button("Translate"):
if input_text == "":
st.write("Please Enter text to translate")
else:
detect_expander = st.expander("Detected Language")
with detect_expander:
detect = trans.detect([input_text])[0]
detect_text = f"Detected Language : {indian_languages.get(detect.lang, detect.lang)}"
st.success(detect_text)
detect_audio = gTTS(text=input_text, lang=detect.lang, slow=False)
detect_audio.save("user_detect.mp3")
audio_file = open("user_detect.mp3", "rb")
audio_bytes = audio_file.read()
st.audio(audio_bytes, format="audio/ogg", start_time=0)
trans_expander = st.expander("Translated Text")
with trans_expander:
translation = trans.translate(input_text, dest=get_key(lang_choice))
translation_text = f"Translated Text : {translation.text}"
st.success(translation_text)
translated_audio = gTTS(text=translation.text, lang=get_key(lang_choice), slow=False)
translated_audio.save("user_trans.mp3")
audio_file = open("user_trans.mp3", "rb")
audio_bytes = audio_file.read()
st.audio(audio_bytes, format="audio/ogg", start_time=0)
with open("user_trans.mp3", "rb") as file:
st.download_button(
label="Download",
data=file,
file_name="trans.mp3",
mime="audio/ogg",
)
elif translation_choice == "Translate PDF":
uploaded_file = st.file_uploader("Upload PDF file", type=["pdf"])
if uploaded_file is not None:
pdf_text = read_pdf(uploaded_file)
st.subheader("PDF Text:")
st.write(pdf_text)
lang_choice_pdf_category = st.selectbox("Select Language Category for PDF", ["Indian Languages", "World Languages"])
if lang_choice_pdf_category == "Indian Languages":
lang_choices_pdf = indian_languages.values()
else:
lang_choices_pdf = world_languages.values()
lang_choice_pdf = st.selectbox("Language to translate PDF text: ", lang_choices_pdf, key="lang_choice_pdf")
if st.button("Translate PDF Text"):
if not pdf_text:
st.warning("The PDF does not contain any text.")
else:
translated_pdf_text = translate_pdf_text(pdf_text, get_key(lang_choice_pdf))
st.subheader("Translated PDF Text:")
st.success(translated_pdf_text)
translated_pdf_audio = gTTS(text=translated_pdf_text, lang=get_key(lang_choice_pdf), slow=False)
translated_pdf_audio.save("translated_pdf.mp3")
audio_file_pdf = open("translated_pdf.mp3", "rb")
audio_bytes_pdf = audio_file_pdf.read()
st.audio(audio_bytes_pdf, format="audio/ogg", start_time=0)
with open("translated_pdf.mp3", "rb") as file_pdf:
st.download_button(
label="Download Translated PDF",
data=file_pdf,
file_name="translated_pdf.mp3",
mime="audio/ogg",
)
collect_feedback()
if __name__ == "__main__":
main()