Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,43 +4,98 @@ import torch
|
|
| 4 |
import librosa
|
| 5 |
from utils import download_from_gdrive, save_as_docx
|
| 6 |
|
| 7 |
-
#
|
| 8 |
engines = {"Whisper V3 Turbo": None, "Bhashini (AI4Bharat)": None, "Sarvam": None}
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def get_engine(model_choice):
|
|
|
|
| 11 |
if engines[model_choice] is None:
|
| 12 |
if model_choice == "Whisper V3 Turbo":
|
| 13 |
from engines.whisper_engine import transcribe_whisper
|
| 14 |
engines[model_choice] = transcribe_whisper
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
return engines[model_choice]
|
| 17 |
|
| 18 |
def process_audio(files, gdrive_link, model_choice, input_lang, progress=gr.Progress()):
|
| 19 |
-
# ... (Existing Drive logic)
|
| 20 |
-
|
| 21 |
-
# Load Engine
|
| 22 |
-
transcribe_fn = get_engine(model_choice)
|
| 23 |
-
|
| 24 |
all_transcripts = []
|
| 25 |
-
|
| 26 |
-
# ...
|
| 27 |
-
# Within the loop:
|
| 28 |
-
# transcript = transcribe_fn(file.name)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
with gr.Blocks(
|
| 36 |
-
gr.HTML("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column():
|
| 40 |
-
file_input = gr.File(label="Upload Audio", file_count="multiple")
|
| 41 |
-
link_input = gr.Textbox(label="Google Drive Link")
|
| 42 |
|
| 43 |
-
# ADDED: Language Selector to prevent translation bugs
|
| 44 |
lang_selector = gr.Dropdown(
|
| 45 |
choices=["Auto-Detect", "Hindi", "English", "Punjabi", "Telugu"],
|
| 46 |
value="Hindi",
|
|
@@ -63,4 +118,9 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 63 |
outputs=[output_file]
|
| 64 |
)
|
| 65 |
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
import librosa
|
| 5 |
from utils import download_from_gdrive, save_as_docx
|
| 6 |
|
| 7 |
+
# Keeping your existing engine loading logic
|
| 8 |
engines = {"Whisper V3 Turbo": None, "Bhashini (AI4Bharat)": None, "Sarvam": None}
|
| 9 |
|
| 10 |
+
def get_audio_duration(file_path):
|
| 11 |
+
"""Returns duration in minutes and a formatted string."""
|
| 12 |
+
try:
|
| 13 |
+
duration_sec = librosa.get_duration(path=file_path)
|
| 14 |
+
duration_min = duration_sec / 60
|
| 15 |
+
return duration_min, f"{duration_min:.2f} min"
|
| 16 |
+
except Exception:
|
| 17 |
+
return 0, "Unknown duration"
|
| 18 |
+
|
| 19 |
def get_engine(model_choice):
|
| 20 |
+
"""Lazy loads the selected engine."""
|
| 21 |
if engines[model_choice] is None:
|
| 22 |
if model_choice == "Whisper V3 Turbo":
|
| 23 |
from engines.whisper_engine import transcribe_whisper
|
| 24 |
engines[model_choice] = transcribe_whisper
|
| 25 |
+
elif model_choice == "Bhashini (AI4Bharat)":
|
| 26 |
+
from engines.indic_engine import transcribe_indic
|
| 27 |
+
engines[model_choice] = transcribe_indic
|
| 28 |
+
elif model_choice == "Sarvam":
|
| 29 |
+
from engines.sarvam_engine import transcribe_sarvam
|
| 30 |
+
engines[model_choice] = transcribe_sarvam
|
| 31 |
return engines[model_choice]
|
| 32 |
|
| 33 |
def process_audio(files, gdrive_link, model_choice, input_lang, progress=gr.Progress()):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
all_transcripts = []
|
| 35 |
+
processed_files = []
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# 1. Handle Google Drive Link if provided
|
| 38 |
+
if gdrive_link:
|
| 39 |
+
progress(0, desc="Fetching from Google Drive...")
|
| 40 |
+
path = download_from_gdrive(gdrive_link)
|
| 41 |
+
if path:
|
| 42 |
+
# Create a simple object with a .name attribute to match gr.File objects
|
| 43 |
+
class MockFile:
|
| 44 |
+
def __init__(self, name):
|
| 45 |
+
self.name = name
|
| 46 |
+
processed_files.append(MockFile(path))
|
| 47 |
+
|
| 48 |
+
# 2. Add uploaded files
|
| 49 |
+
if files:
|
| 50 |
+
processed_files.extend(files)
|
| 51 |
+
|
| 52 |
+
if not processed_files:
|
| 53 |
+
raise gr.Error("Please upload files or provide a G-Drive link.")
|
| 54 |
+
|
| 55 |
+
# 3. Load Engine
|
| 56 |
+
progress(0.1, desc=f"Loading {model_choice} weights...")
|
| 57 |
+
transcribe_fn = get_engine(model_choice)
|
| 58 |
|
| 59 |
+
# 4. Process each file
|
| 60 |
+
for i, file in enumerate(processed_files):
|
| 61 |
+
file_name = os.path.basename(file.name)
|
| 62 |
+
_, duration_str = get_audio_duration(file.name)
|
| 63 |
+
|
| 64 |
+
progress((i + 0.2) / len(processed_files),
|
| 65 |
+
desc=f"Transcribing {file_name} ({duration_str})...")
|
| 66 |
+
|
| 67 |
+
try:
|
| 68 |
+
# Actually call the transcription function and store the result
|
| 69 |
+
transcript_data = transcribe_fn(file.name)
|
| 70 |
+
all_transcripts.append({"filename": file_name, "text": transcript_data})
|
| 71 |
+
except Exception as e:
|
| 72 |
+
print(f"Error processing {file_name}: {e}")
|
| 73 |
+
all_transcripts.append({"filename": file_name, "text": f"Error: {str(e)}"})
|
| 74 |
+
|
| 75 |
+
# 5. Create the Docx
|
| 76 |
+
progress(0.9, desc="Generating Document...")
|
| 77 |
+
output_path = save_as_docx(all_transcripts)
|
| 78 |
+
return output_path
|
| 79 |
+
|
| 80 |
+
# Custom CSS for the UI
|
| 81 |
+
custom_css = """
|
| 82 |
+
.gradio-container { font-family: 'Noto Sans', sans-serif; }
|
| 83 |
+
#title-container { text-align: center; padding: 20px; }
|
| 84 |
+
"""
|
| 85 |
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
gr.HTML("""
|
| 88 |
+
<div id="title-container">
|
| 89 |
+
<h1 style="font-size: 2.5em; margin-bottom: 0;">🎙️ Indic Transcribe Pro</h1>
|
| 90 |
+
<p style="font-size: 1.2em; color: #666;">v1.6 - Fixed Empty Output Bug</p>
|
| 91 |
+
</div>
|
| 92 |
+
""")
|
| 93 |
|
| 94 |
with gr.Row():
|
| 95 |
with gr.Column():
|
| 96 |
+
file_input = gr.File(label="Upload Audio Files", file_count="multiple")
|
| 97 |
+
link_input = gr.Textbox(label="OR Paste Google Drive Link (Public)")
|
| 98 |
|
|
|
|
| 99 |
lang_selector = gr.Dropdown(
|
| 100 |
choices=["Auto-Detect", "Hindi", "English", "Punjabi", "Telugu"],
|
| 101 |
value="Hindi",
|
|
|
|
| 118 |
outputs=[output_file]
|
| 119 |
)
|
| 120 |
|
| 121 |
+
if __name__ == "__main__":
|
| 122 |
+
# Theme and CSS moved here to launch() to satisfy Gradio 6.0
|
| 123 |
+
demo.launch(
|
| 124 |
+
theme=gr.themes.Soft(),
|
| 125 |
+
css=custom_css
|
| 126 |
+
)
|