mguven61 commited on
Commit
4d86f0d
Β·
verified Β·
1 Parent(s): 313da01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -20
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 audio input provided", "Please upload an audio file OR record with microphone", ""
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
- source="upload",
96
  label="πŸ“ Upload Audio File (MP3, WAV, etc.)"
97
  ),
98
  gr.Audio(
99
- source="microphone",
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"], # Example with Turkish to English
138
- ["", None, None, "en", "fr"], # Example with English to French
139
- ["Hello world", None, None, "en", "es"], # Example with text input
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
  )