File size: 4,636 Bytes
696b78e
e76c7d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
696b78e
e76c7d1
 
696b78e
e76c7d1
 
 
 
 
 
 
 
696b78e
e76c7d1
 
 
 
 
 
 
 
 
696b78e
e76c7d1
 
 
 
696b78e
 
e76c7d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
696b78e
 
e76c7d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import gradio as gr
import time
import os
import zipfile
from typing import List, Tuple, Generator

# Initial status message
STANDARD_OUTPUT_TEXT = "**Status:**<br>"

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}<br>"
        )
        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)}%**<br>" + "".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)}%**<br>" + "".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()