import gradio as gr from utils.file_utils import validate_and_process_file, convert_mp4_to_mp3 from utils.transcription_utils import transcribe_audio import tempfile from tqdm import tqdm # Application header title = "ScribbleBot by Heuristica.pl" description = "Audio transcription application - convert speech to text. Enter your API key and upload your audio file (.mp3, .wav, .mp4). Bot will transcribe your audio content into text format!" def process_input(api_key, file_path, progress=gr.Progress(track_tqdm=True)): logs = [] if not file_path: return "Please select a file for transcription.", None, False, "" logs.append("Processing file...") progress(0, desc="Starting file processing...") try: # Check if file is MP4 if isinstance(file_path, str) and file_path.lower().endswith(".mp4"): logs.append("Converting MP4 to MP3...") progress(0.2, desc="Converting MP4 to MP3...") try: file_path = convert_mp4_to_mp3(file_path) logs.append(f"File converted to: {file_path}") except Exception as e: logs.append(f"Conversion error: {str(e)}") return "\n".join(logs), None, False, "" processed_files = validate_and_process_file(file_path) transcriptions = [] # Calculate total steps for progress bar total_steps = len(processed_files) progress(0.3, desc="Starting transcription...") for i, file in enumerate(processed_files): # Calculate progress from 30% to 90% current_progress = 0.3 + (0.6 * (i / total_steps)) progress(current_progress, desc=f"Transcribing part {i + 1}/{total_steps}...") logs.append(f"Transcribing part {i + 1}/{total_steps}...") transcription = transcribe_audio(api_key, file) transcriptions.append(transcription) logs.append(f"Part {i + 1} transcription completed.") progress(0.9, desc="Finalizing...") # Połącz wszystkie transkrypcje full_transcription = "\n\n".join(transcriptions) # Save transcription to temporary file with tempfile.NamedTemporaryFile(delete=False, mode="w", suffix=".txt", encoding="utf-8") as f: f.write(full_transcription) temp_file_path = f.name progress(1.0, desc="Completed!") logs.append("Transcription completed. Ready for download.") return "\n".join(logs), temp_file_path, True, full_transcription except Exception as e: error_msg = str(e) logs.append(f"An error occurred: {error_msg}") return "\n".join(logs), None, False, "" # User interface with gr.Blocks() as demo: gr.Markdown(f"# {title}") gr.Markdown(description) with gr.Row(): api_key = gr.Textbox(label="Enter OpenAI API Key", placeholder="sk-...") with gr.Row(): file_input = gr.File( label="Upload audio file", file_types=[".mp3", ".wav", ".mp4"] ) upload_progress = gr.Textbox( label="Upload Status", value="No file selected", interactive=False ) with gr.Row(): submit_button = gr.Button("Start Transcription") stop_button = gr.Button("Stop", variant="stop") with gr.Row(): logs = gr.Textbox(label="Process", interactive=False, lines=10) with gr.Row(): download_link = gr.File(label="Download Transcription", visible=False) with gr.Row(): transcription_preview = gr.Textbox( label="Transcription Preview", interactive=False, lines=15, placeholder="Transcription will appear here..." ) # Add visibility management component download_visibility = gr.Checkbox(value=False, visible=False) # Update upload status when file is selected def update_upload_status(file): if file is None: return "No file selected" else: return f"File uploaded: {file.name}" file_input.change( fn=update_upload_status, inputs=[file_input], outputs=[upload_progress] ) submit_button.click( process_input, inputs=[api_key, file_input], outputs=[logs, download_link, download_visibility, transcription_preview] ) # Set download_link visibility based on download_visibility value download_visibility.change( lambda visible: gr.File(visible=visible), inputs=download_visibility, outputs=download_link ) # Launch application if __name__ == "__main__": demo.launch(share=True)