Upload 3 files
Browse files- app.py +136 -0
- engine.py +48 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from engine import Translate
|
3 |
+
import os
|
4 |
+
import shutil
|
5 |
+
import json
|
6 |
+
|
7 |
+
translator = Translate()
|
8 |
+
# Creating the list of the tasks the seamlessM4t model can perform
|
9 |
+
tasks = ['S2ST (Speech to Speech translation)',
|
10 |
+
'S2TT (Speech to Text translation)',
|
11 |
+
'T2ST (Text to Speech translation)',
|
12 |
+
'T2TT (Text to Text translation)']
|
13 |
+
|
14 |
+
def move_audio_file(source_path, destination_path):
|
15 |
+
try:
|
16 |
+
shutil.move(source_path, destination_path)
|
17 |
+
return True # Return True if the move was successful
|
18 |
+
except Exception as e:
|
19 |
+
print(f"Error moving the audio file: {str(e)}")
|
20 |
+
return False # Return False if there was an error
|
21 |
+
|
22 |
+
# List of language options
|
23 |
+
languages = [
|
24 |
+
"Catalan",
|
25 |
+
"Czech",
|
26 |
+
"Danish",
|
27 |
+
"Dutch",
|
28 |
+
"English",
|
29 |
+
"Estonian",
|
30 |
+
"Finnish",
|
31 |
+
"French",
|
32 |
+
"German",
|
33 |
+
"Hindi",
|
34 |
+
"Indonesian",
|
35 |
+
"Italian",
|
36 |
+
"Japanese",
|
37 |
+
"Korean",
|
38 |
+
"Maltese",
|
39 |
+
"Mandarin Chinese",
|
40 |
+
"Modern Standard Arabic",
|
41 |
+
"Northern Uzbek",
|
42 |
+
"Polish",
|
43 |
+
"Portuguese",
|
44 |
+
"Romanian",
|
45 |
+
"Russian",
|
46 |
+
"Slovak",
|
47 |
+
"Spanish",
|
48 |
+
"Swahili",
|
49 |
+
"Swedish",
|
50 |
+
"Tagalog",
|
51 |
+
"Telugu",
|
52 |
+
"Thai",
|
53 |
+
"Turkish",
|
54 |
+
"Ukrainian",
|
55 |
+
"Urdu",
|
56 |
+
"Vietnamese",
|
57 |
+
"Welsh",
|
58 |
+
"Western Persian"
|
59 |
+
]
|
60 |
+
|
61 |
+
st.title("Seamless-M4t Translator")
|
62 |
+
task = st.selectbox('Select Task:', tasks)
|
63 |
+
|
64 |
+
if task == 'S2ST (Speech to Speech translation)':
|
65 |
+
upload_audio = st.file_uploader('Upload Audio file', type=['mp3', 'wav', 'ogg'])
|
66 |
+
target_language = st.selectbox('Target language:', languages)
|
67 |
+
|
68 |
+
button_clicked = st.button('Convert')
|
69 |
+
if button_clicked:
|
70 |
+
if upload_audio and target_language:
|
71 |
+
result = translator.translate(task=task, audio=upload_audio, target_language=target_language)
|
72 |
+
|
73 |
+
# Set the source and destination paths for moving the audio file
|
74 |
+
source_path = result.replace("\\", "/")
|
75 |
+
audio_paths = source_path.split(',')
|
76 |
+
for audio_path in audio_paths:
|
77 |
+
try:
|
78 |
+
audio_path = audio_path.lstrip("['")
|
79 |
+
st.audio(audio_path.strip('"'), format="audio/*")
|
80 |
+
|
81 |
+
except Exception as e:
|
82 |
+
# audio_content = audio_path.lstrip(']')
|
83 |
+
audio_content=audio_path.rstrip(']')
|
84 |
+
st.write(audio_content)
|
85 |
+
|
86 |
+
|
87 |
+
elif task == 'S2TT (Speech to Text translation)':
|
88 |
+
upload_audio = st.file_uploader('Upload Audio file', type=['mp3', 'wav', 'ogg'])
|
89 |
+
# Create a button
|
90 |
+
target_language = st.selectbox('Target language : ', languages)
|
91 |
+
button_clicked = st.button("Convert")
|
92 |
+
|
93 |
+
if button_clicked:
|
94 |
+
if upload_audio:
|
95 |
+
result = translator.translate(task=task, audio=upload_audio, target_language=target_language)
|
96 |
+
result = json.loads(result)
|
97 |
+
st.write(result)
|
98 |
+
|
99 |
+
|
100 |
+
elif task == 'T2ST (Text to Speech translation)':
|
101 |
+
input_language = st.selectbox("Input Language:", languages)
|
102 |
+
text = st.text_area("Enter the Text Here:")
|
103 |
+
target_language = st.selectbox('Target language:', languages)
|
104 |
+
|
105 |
+
button_clicked = st.button('Convert')
|
106 |
+
|
107 |
+
if button_clicked:
|
108 |
+
if text and input_language and target_language:
|
109 |
+
result = translator.translate(task=task, text=text, input_language=input_language, target_language=target_language)
|
110 |
+
|
111 |
+
# Set the source and destination paths for moving the audio file
|
112 |
+
source_path = result.replace("\\", "/")
|
113 |
+
audio_paths = source_path.split(',')
|
114 |
+
for audio_path in audio_paths:
|
115 |
+
try:
|
116 |
+
audio_path = audio_path.lstrip("['")
|
117 |
+
st.audio(audio_path.strip('"'), format="audio/wav")
|
118 |
+
|
119 |
+
except Exception as e:
|
120 |
+
# audio_content = audio_path.lstrip(']')
|
121 |
+
audio_content=audio_path.rstrip(']')
|
122 |
+
st.write(audio_content)
|
123 |
+
|
124 |
+
|
125 |
+
elif task == 'T2TT (Text to Text translation)':
|
126 |
+
input_language = st.selectbox("Input Language:", languages)
|
127 |
+
text = st.text_area("Enter the Text Here:")
|
128 |
+
target_language = st.selectbox('Target language:', languages)
|
129 |
+
|
130 |
+
button_clicked = st.button('Convert')
|
131 |
+
|
132 |
+
if button_clicked:
|
133 |
+
if text and input_language and target_language:
|
134 |
+
result = translator.translate(task=task, text=text, input_language=input_language, target_language=target_language)
|
135 |
+
conv = json.loads(result)
|
136 |
+
st.write(conv[1])
|
engine.py
ADDED
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from gradio_client import Client
|
2 |
+
import json
|
3 |
+
import base64
|
4 |
+
|
5 |
+
class Translate:
|
6 |
+
|
7 |
+
def translate(self, task=None, audio=None, text=None, input_language=None, target_language=None):
|
8 |
+
client = Client("https://facebook-seamless-m4t.hf.space/")
|
9 |
+
|
10 |
+
audio_content = None # Initialize to None
|
11 |
+
result = None # Initialize result
|
12 |
+
|
13 |
+
if audio is not None:
|
14 |
+
# Handle the uploaded audio file
|
15 |
+
audio_content = audio.read() # Read the binary content of the uploaded audio
|
16 |
+
audio.close() # Close the uploaded file
|
17 |
+
# Convert audio content to base64-encoded string
|
18 |
+
audio_content = base64.b64encode(audio_content).decode('utf-8')
|
19 |
+
|
20 |
+
# Call the Gradio predict method and store the result
|
21 |
+
result = client.predict(
|
22 |
+
task,
|
23 |
+
audio_content, # Pass the audio content as base64-encoded string
|
24 |
+
"https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav",
|
25 |
+
"https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav",
|
26 |
+
text,
|
27 |
+
input_language,
|
28 |
+
target_language,
|
29 |
+
api_name="/run"
|
30 |
+
)
|
31 |
+
else:
|
32 |
+
result = client.predict(
|
33 |
+
task,
|
34 |
+
audio_content, # Pass the audio content as base64-encoded string
|
35 |
+
"https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav",
|
36 |
+
"https://github.com/gradio-app/gradio/raw/main/test/test_files/audio_sample.wav",
|
37 |
+
text,
|
38 |
+
input_language,
|
39 |
+
target_language,
|
40 |
+
api_name="/run"
|
41 |
+
)
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
# Serialize the dictionary to a JSON-serializable string
|
46 |
+
result_str = json.dumps(result)
|
47 |
+
|
48 |
+
return result_str
|
requirements.txt
ADDED
Binary file (2.06 kB). View file
|
|