Update app.py
Browse files
app.py
CHANGED
@@ -7,14 +7,9 @@ import os
|
|
7 |
import warnings
|
8 |
warnings.filterwarnings("ignore")
|
9 |
|
10 |
-
# Load model once
|
11 |
model = whisper.load_model("base")
|
12 |
|
13 |
def translate_audio(text_input, upload_audio, mic_audio, source_lang="tr", target_lang="en"):
|
14 |
-
"""
|
15 |
-
Function that handles both uploaded audio and microphone audio
|
16 |
-
"""
|
17 |
-
# Determine which audio source to use
|
18 |
audio_file = None
|
19 |
audio_source = ""
|
20 |
|
@@ -26,13 +21,34 @@ def translate_audio(text_input, upload_audio, mic_audio, source_lang="tr", targe
|
|
26 |
audio_file = upload_audio
|
27 |
audio_source = "π Upload"
|
28 |
print(f"Using uploaded audio: {upload_audio}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
else:
|
30 |
-
return None, "β No
|
31 |
|
32 |
try:
|
33 |
print(f"Processing {audio_source} audio: {audio_file}")
|
34 |
|
35 |
-
# Transcribe audio
|
36 |
result = model.transcribe(audio_file, language=source_lang, fp16=False)
|
37 |
original_text = result["text"].strip()
|
38 |
|
@@ -41,7 +57,6 @@ def translate_audio(text_input, upload_audio, mic_audio, source_lang="tr", targe
|
|
41 |
|
42 |
print(f"Transcribed: {original_text}")
|
43 |
|
44 |
-
# Translate text
|
45 |
if source_lang != target_lang:
|
46 |
translator = GoogleTranslator(source=source_lang, target=target_lang)
|
47 |
translated_text = translator.translate(original_text)
|
@@ -50,10 +65,8 @@ def translate_audio(text_input, upload_audio, mic_audio, source_lang="tr", targe
|
|
50 |
|
51 |
print(f"Translated: {translated_text}")
|
52 |
|
53 |
-
# Generate speech
|
54 |
tts = gTTS(text=translated_text, lang=target_lang, slow=False)
|
55 |
|
56 |
-
# Save to temporary file
|
57 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file:
|
58 |
tts.save(tmp_file.name)
|
59 |
return (
|
@@ -67,7 +80,6 @@ def translate_audio(text_input, upload_audio, mic_audio, source_lang="tr", targe
|
|
67 |
print(f"Error: {str(e)}")
|
68 |
return None, f"β Error: {str(e)}", "Please try again", f"Source: {audio_source}"
|
69 |
|
70 |
-
# Language options
|
71 |
languages = {
|
72 |
"tr": "πΉπ· Turkish",
|
73 |
"en": "πΊπΈ English",
|
@@ -81,7 +93,6 @@ languages = {
|
|
81 |
"zh": "π¨π³ Chinese"
|
82 |
}
|
83 |
|
84 |
-
# Create Gradio interface exactly like the Clone-Your-Voice example
|
85 |
demo = gr.Interface(
|
86 |
fn=translate_audio,
|
87 |
inputs=[
|
@@ -92,14 +103,13 @@ demo = gr.Interface(
|
|
92 |
),
|
93 |
gr.Audio(
|
94 |
type="filepath",
|
95 |
-
|
96 |
label="π Upload Audio File (MP3, WAV, etc.)"
|
97 |
),
|
98 |
gr.Audio(
|
99 |
-
|
100 |
label="π€ OR Record with Microphone",
|
101 |
-
type="filepath"
|
102 |
-
optional=True
|
103 |
),
|
104 |
gr.Dropdown(
|
105 |
choices=list(languages.keys()),
|
@@ -126,6 +136,7 @@ demo = gr.Interface(
|
|
126 |
<h3>π― How to Use:</h3>
|
127 |
<p><strong>Option 1:</strong> π Upload an audio file (recommended)</p>
|
128 |
<p><strong>Option 2:</strong> π€ Record directly with microphone</p>
|
|
|
129 |
<p><strong>Then:</strong> Select source and target languages, wait for processing!</p>
|
130 |
<br>
|
131 |
<p>π§ <strong>Troubleshooting:</strong> If microphone doesn't work, use file upload instead.</p>
|
@@ -134,12 +145,11 @@ demo = gr.Interface(
|
|
134 |
""",
|
135 |
|
136 |
examples=[
|
137 |
-
["", None, None, "tr", "en"],
|
138 |
-
["", None, None, "en", "fr"],
|
139 |
-
["Hello world", None, None, "en", "es"],
|
140 |
],
|
141 |
|
142 |
-
# Important: Set these flags
|
143 |
allow_flagging="never",
|
144 |
show_error=True
|
145 |
)
|
|
|
7 |
import warnings
|
8 |
warnings.filterwarnings("ignore")
|
9 |
|
|
|
10 |
model = whisper.load_model("base")
|
11 |
|
12 |
def translate_audio(text_input, upload_audio, mic_audio, source_lang="tr", target_lang="en"):
|
|
|
|
|
|
|
|
|
13 |
audio_file = None
|
14 |
audio_source = ""
|
15 |
|
|
|
21 |
audio_file = upload_audio
|
22 |
audio_source = "π Upload"
|
23 |
print(f"Using uploaded audio: {upload_audio}")
|
24 |
+
elif text_input and text_input.strip():
|
25 |
+
try:
|
26 |
+
original_text = text_input.strip()
|
27 |
+
|
28 |
+
if source_lang != target_lang:
|
29 |
+
translator = GoogleTranslator(source=source_lang, target=target_lang)
|
30 |
+
translated_text = translator.translate(original_text)
|
31 |
+
else:
|
32 |
+
translated_text = original_text
|
33 |
+
|
34 |
+
tts = gTTS(text=translated_text, lang=target_lang, slow=False)
|
35 |
+
|
36 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file:
|
37 |
+
tts.save(tmp_file.name)
|
38 |
+
return (
|
39 |
+
tmp_file.name,
|
40 |
+
f"β
Original: {original_text}",
|
41 |
+
f"π Translated: {translated_text}",
|
42 |
+
f"π‘ Source: π¬ Text Input"
|
43 |
+
)
|
44 |
+
except Exception as e:
|
45 |
+
return None, f"β Error: {str(e)}", "Please try again", "Source: π¬ Text Input"
|
46 |
else:
|
47 |
+
return None, "β No input provided", "Please upload audio, record with microphone, OR enter text", ""
|
48 |
|
49 |
try:
|
50 |
print(f"Processing {audio_source} audio: {audio_file}")
|
51 |
|
|
|
52 |
result = model.transcribe(audio_file, language=source_lang, fp16=False)
|
53 |
original_text = result["text"].strip()
|
54 |
|
|
|
57 |
|
58 |
print(f"Transcribed: {original_text}")
|
59 |
|
|
|
60 |
if source_lang != target_lang:
|
61 |
translator = GoogleTranslator(source=source_lang, target=target_lang)
|
62 |
translated_text = translator.translate(original_text)
|
|
|
65 |
|
66 |
print(f"Translated: {translated_text}")
|
67 |
|
|
|
68 |
tts = gTTS(text=translated_text, lang=target_lang, slow=False)
|
69 |
|
|
|
70 |
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') as tmp_file:
|
71 |
tts.save(tmp_file.name)
|
72 |
return (
|
|
|
80 |
print(f"Error: {str(e)}")
|
81 |
return None, f"β Error: {str(e)}", "Please try again", f"Source: {audio_source}"
|
82 |
|
|
|
83 |
languages = {
|
84 |
"tr": "πΉπ· Turkish",
|
85 |
"en": "πΊπΈ English",
|
|
|
93 |
"zh": "π¨π³ Chinese"
|
94 |
}
|
95 |
|
|
|
96 |
demo = gr.Interface(
|
97 |
fn=translate_audio,
|
98 |
inputs=[
|
|
|
103 |
),
|
104 |
gr.Audio(
|
105 |
type="filepath",
|
106 |
+
sources=["upload"],
|
107 |
label="π Upload Audio File (MP3, WAV, etc.)"
|
108 |
),
|
109 |
gr.Audio(
|
110 |
+
sources=["microphone"],
|
111 |
label="π€ OR Record with Microphone",
|
112 |
+
type="filepath"
|
|
|
113 |
),
|
114 |
gr.Dropdown(
|
115 |
choices=list(languages.keys()),
|
|
|
136 |
<h3>π― How to Use:</h3>
|
137 |
<p><strong>Option 1:</strong> π Upload an audio file (recommended)</p>
|
138 |
<p><strong>Option 2:</strong> π€ Record directly with microphone</p>
|
139 |
+
<p><strong>Option 3:</strong> π¬ Type text directly</p>
|
140 |
<p><strong>Then:</strong> Select source and target languages, wait for processing!</p>
|
141 |
<br>
|
142 |
<p>π§ <strong>Troubleshooting:</strong> If microphone doesn't work, use file upload instead.</p>
|
|
|
145 |
""",
|
146 |
|
147 |
examples=[
|
148 |
+
["", None, None, "tr", "en"],
|
149 |
+
["", None, None, "en", "fr"],
|
150 |
+
["Hello world", None, None, "en", "es"],
|
151 |
],
|
152 |
|
|
|
153 |
allow_flagging="never",
|
154 |
show_error=True
|
155 |
)
|