import gradio as gr
import time
import os
import zipfile
from typing import List, Tuple, Generator
# Initial status message
STANDARD_OUTPUT_TEXT = "**Status:**
"
def process_files_with_live_updates(
files: List[gr.File],
dropdown_option: str,
dropdown_option_2: str
) -> Generator[Tuple[str, List[str]], None, None]:
"""
Processes a list of uploaded files and provides live updates with progress.
Args:
files (List[gr.File]): List of files uploaded by the user.
dropdown_option (str): Selected option from the first dropdown.
dropdown_option_2 (str): Selected option from the second dropdown.
Yields:
Tuple[str, List[str]]: Updated status message and list of processed file paths.
"""
file_details = []
total_files = len(files)
output_files = []
# Create a folder to temporarily store output files
output_dir = "output_files"
os.makedirs(output_dir, exist_ok=True)
for idx, file in enumerate(files):
# Simulate file processing
time.sleep(1)
# Add to file details
detail = (
f"**File Name**: {file.name} - {dropdown_option} - {dropdown_option_2}
"
)
file_details.append(detail)
# Generate a .txt file
txt_filename = os.path.join(output_dir, f"output_file_{idx + 1}.txt")
with open(txt_filename, "w") as txt_file:
txt_file.write(f"Original File Name: {file.name}")
output_files.append(txt_filename)
# Update progress bar and yield the updated Markdown
yield (
f"**Status: {int(((idx + 1) / total_files) * 100)}%**
" + "".join(file_details),
output_files,
)
# Create a zip archive
zip_filename = os.path.join(output_dir, "output_files.zip")
with zipfile.ZipFile(zip_filename, "w") as zipf:
for file_path in output_files:
zipf.write(file_path, os.path.basename(file_path))
output_files.append(zip_filename)
# Final yield
yield (
f"**Status: {int(((idx + 1) / total_files) * 100)}%**
" + "".join(file_details),
output_files,
)
# Gradio app layout
with gr.Blocks() as demo:
# Title and Description
gr.Markdown("# AI-Powered Speech-to-Text Batch Processor")
gr.Markdown(
"""
Upload multiple audio files, select desired processing options, and view real-time updates as files are transcribed.
The application uses advanced AI models for sequential speech-to-text translation.
"""
)
# Input section
with gr.Row():
with gr.Column():
file_input = gr.Files(file_types=[".wav", ".mp3"], label="Upload your audio files")
with gr.Column():
dropdown = gr.Dropdown(
choices=["Language: English", "Language: German", "Language: French"],
label="Select Language",
value="Language: English",
)
dropdown_2 = gr.Dropdown(
choices=["Format: Plain Text", "Format: JSON", "Format: SRT"],
label="Select Output Format",
value="Format: Plain Text",
)
# Buttons
with gr.Row():
submit_button = gr.Button("Start Transcription")
clear_button = gr.Button("Clear")
# Output section
output_md = gr.Markdown(label="Transcription Progress", value=STANDARD_OUTPUT_TEXT)
output_files = gr.Files(label="Generated Output Files")
# Button actions
submit_button.click(
process_files_with_live_updates,
inputs=[file_input, dropdown, dropdown_2],
outputs=[output_md, output_files],
)
clear_button.click(
lambda: (None, "Language: English", "Format: Plain Text", STANDARD_OUTPUT_TEXT, None),
inputs=[], # No inputs
outputs=[file_input, dropdown, dropdown_2, output_md, output_files],
)
gr.Textbox(os.getcwd(), label="Current Working Directory")
gr.Image("Fraunhofer-IPA-Logo.jpg", show_label=False)
# Centered Footer with Logo and Licensing Text
with gr.Row():
gr.Markdown(
"""
**Fraunhofer IPA**
This application is provided under a basic licensing agreement for non-commercial use only.
For inquiries, visit [Fraunhofer IPA](https://www.ipa.fraunhofer.de).
""",
elem_id="footer-markdown",
)
# CSS to center the footer content
demo.css = """
#footer-markdown {
text-align: center;
margin-top: 20px;
padding-top: 10px;
border-top: 1px solid #ccc;
}
"""
# Launch app
demo.launch()