Spaces:
Sleeping
Sleeping
File size: 4,764 Bytes
8b1b0bf 017d3e0 3c60972 7fad65d 8b1b0bf 3aa56c3 3619b86 ce17a3b 8b1b0bf 7fad65d ce57ddb 017d3e0 c135615 017d3e0 3aa56c3 5b37e31 017d3e0 3aa56c3 017d3e0 3aa56c3 5b37e31 017d3e0 3aa56c3 017d3e0 3aa56c3 c135615 017d3e0 7fad65d 5b37e31 7fad65d 017d3e0 5b37e31 7fad65d 5b37e31 017d3e0 3aa56c3 017d3e0 5b37e31 7fad65d c135615 3aa56c3 017d3e0 c135615 017d3e0 5b37e31 3aa56c3 c135615 9126c24 017d3e0 3aa56c3 c135615 8b1b0bf 3aa56c3 8b1b0bf 3aa56c3 8b1b0bf 0fb9ab9 5b37e31 0fb9ab9 8b1b0bf 3aa56c3 5c0d33d 4524b75 3619b86 5c0d33d 3aa56c3 c135615 8b1b0bf 3aa56c3 9126c24 a52f71c 5b37e31 8b1b0bf 017d3e0 c135615 a52f71c 3aa56c3 a52f71c 8b1b0bf 3aa56c3 8b1b0bf 5b37e31 | 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 | 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) |