first commit
Browse files- app.py +70 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from huggingface_hub import notebook_login
|
3 |
+
import gradio as gr
|
4 |
+
from transformers import pipeline
|
5 |
+
from deep_translator import GoogleTranslator
|
6 |
+
from gtts import gTTS
|
7 |
+
import shutil
|
8 |
+
from pytube import YouTube
|
9 |
+
|
10 |
+
os.environ["HF_TOKEN"] = "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"
|
11 |
+
notebook_login()
|
12 |
+
|
13 |
+
pipe = pipeline(model="nelanbu/ID2223_Lab2_Whisper") # change to "your-username/the-name-you-picked"
|
14 |
+
|
15 |
+
|
16 |
+
def transcribe(audio_input, link_input, lang):
|
17 |
+
try:
|
18 |
+
if link_input:
|
19 |
+
video=YouTube(link_input).streams.filter(only_audio=True).all()
|
20 |
+
audio=video[0].download()
|
21 |
+
|
22 |
+
elif audio_input:
|
23 |
+
audio = audio_input
|
24 |
+
# input_path = "input.mp3"
|
25 |
+
# shutil.copyfile(audio, input_path)
|
26 |
+
# print(f"Input audio is saved to {input_path}")
|
27 |
+
|
28 |
+
result = pipe(audio)
|
29 |
+
text = result['text']
|
30 |
+
print(f"Transcribed text: {text}")
|
31 |
+
|
32 |
+
if lang == 'english':
|
33 |
+
target_lang = 'en'
|
34 |
+
elif lang == 'swedish':
|
35 |
+
target_lang = 'sv'
|
36 |
+
elif lang == 'italian':
|
37 |
+
target_lang = 'it'
|
38 |
+
elif lang == 'german':
|
39 |
+
target_lang = 'de'
|
40 |
+
elif lang == 'french':
|
41 |
+
target_lang = 'fr'
|
42 |
+
|
43 |
+
translator = GoogleTranslator(source='auto', target=target_lang)
|
44 |
+
translated_text = translator.translate(text)
|
45 |
+
print(f"Translated text: {translated_text}")
|
46 |
+
# Convert translated text to speech
|
47 |
+
tts = gTTS(text=translated_text, tld='com', slow=False, lang=target_lang)
|
48 |
+
output_audio_path = "test2.mp3"
|
49 |
+
tts.save(output_audio_path)
|
50 |
+
print(f"Saved TTS audio to {output_audio_path}")
|
51 |
+
|
52 |
+
return output_audio_path
|
53 |
+
except Exception as e:
|
54 |
+
print(f"An error occurred: {e}")
|
55 |
+
raise
|
56 |
+
|
57 |
+
demo = gr.Interface(
|
58 |
+
fn=transcribe,
|
59 |
+
inputs=[gr.Audio(type="filepath", label="Translate from microphone/MP3 input"),
|
60 |
+
gr.Text(max_lines=1, label="Translate from YouTube URL"),
|
61 |
+
gr.Dropdown(
|
62 |
+
["english", "swedish", "italian", "german", "french"], label="Language", info="pick the language you want to translate your auido"),
|
63 |
+
],
|
64 |
+
outputs=gr.Audio(type="filepath"),
|
65 |
+
title="Turkish Audio Translator",
|
66 |
+
description="You can upload YouTube link of a video in Turkish or use the microphone to record your voice or upload an MP3 file to translate Turkish audio to other languages."
|
67 |
+
)
|
68 |
+
|
69 |
+
if __name__ == "__main__":
|
70 |
+
demo.launch(debug=True)
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
transformers
|
3 |
+
gTTS
|
4 |
+
deep-translator
|
5 |
+
ttpx==0.24.1
|
6 |
+
pytube
|