emon-j commited on
Commit
fc56bc8
1 Parent(s): 92d36c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -8
app.py CHANGED
@@ -1,9 +1,10 @@
 
1
  import streamlit as st
2
- import ctranslate2
3
- import transformers
4
- from translator import translate
5
- st.title("On Device Translator")
6
- user_input = st.text_area("Enter text to be translated in English here.")
7
  languages = {
8
  "English": "eng_Latn",
9
  "Japanese": "jpn_Jpan",
@@ -16,7 +17,30 @@ languages = {
16
  "Russian": "rus_Cyrl"
17
  }
18
  selected_language = st.selectbox("Select Target Language", list(languages.keys()))
19
- if st.button("Translate"):
20
- translated_text = translate(user_input, tgt_lang=languages[selected_language])
21
- st.write(f"Translated text: {translated_text}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
 
 
1
+ from faster_whisper import WhisperModel
2
  import streamlit as st
3
+ from translator import translate
4
+ model_size = "small"
5
+ model = WhisperModel(model_size, device="cpu", compute_type="int8")
6
+ st.title("Audio to Text Translator")
7
+ audio_file = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg", "flac"])
8
  languages = {
9
  "English": "eng_Latn",
10
  "Japanese": "jpn_Jpan",
 
17
  "Russian": "rus_Cyrl"
18
  }
19
  selected_language = st.selectbox("Select Target Language", list(languages.keys()))
20
+ if st.button("Transcribe and Translate"):
21
+ if audio_file is not None:
22
+ with open("temp_audio.mp3", "wb") as f:
23
+ f.write(audio_file.getbuffer())
24
+ segments, info = model.transcribe("temp_audio.mp3", beam_size=5)
25
+ st.write("Detected language '%s' with probability %f" % (info.language, info.language_probability))
26
+ col1, col2 = st.columns(2)
27
+
28
+ with col1:
29
+ st.subheader("Transcription")
30
+
31
+ with col2:
32
+ st.subheader("Translation")
33
+
34
+ # Process each segment individually
35
+ for segment in segments:
36
+ # Translate the segment text
37
+ translated_text = translate(segment.text, tgt_lang=languages[selected_language])
38
+
39
+ with col1:
40
+ st.markdown(f"**[{segment.start:.2f}s - {segment.end:.2f}s]** {segment.text}")
41
+
42
+ with col2:
43
+ st.markdown(f"**[{segment.start:.2f}s - {segment.end:.2f}s]** {translated_text}")
44
+ else:
45
+ st.error("Please upload an audio file.")
46