Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,86 +1,43 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
3 |
-
from transformers import WhisperProcessor, WhisperForConditionalGeneration
|
4 |
-
import numpy as np
|
5 |
-
import librosa
|
6 |
|
7 |
-
|
8 |
-
processor = WhisperProcessor.from_pretrained("openai/whisper-base")
|
9 |
-
model = WhisperForConditionalGeneration.from_pretrained("openai/whisper-base")
|
10 |
-
|
11 |
-
# Set light green theme
|
12 |
-
theme = gr.themes.Base(
|
13 |
-
primary_hue="emerald",
|
14 |
-
secondary_hue="emerald",
|
15 |
-
neutral_hue="gray",
|
16 |
-
)
|
17 |
-
|
18 |
-
def validate_file(file_path):
|
19 |
-
# Check if file exists
|
20 |
-
if not file_path or not os.path.exists(file_path):
|
21 |
-
return False, "No file uploaded or file not found."
|
22 |
-
|
23 |
-
# Check file size (25 MB limit)
|
24 |
-
file_size_mb = os.path.getsize(file_path) / (1024 * 1024)
|
25 |
-
if file_size_mb > 25:
|
26 |
-
return False, f"File size is {file_size_mb:.2f} MB. Please upload a file smaller than 25 MB."
|
27 |
-
|
28 |
-
# Check file extension
|
29 |
-
file_extension = os.path.splitext(file_path)[1].lower()
|
30 |
-
if file_extension not in ['.mp3', '.wav']:
|
31 |
-
return False, "Only .mp3 and .wav formats are supported."
|
32 |
-
|
33 |
-
return True, "File is valid."
|
34 |
|
35 |
def transcribe_audio(audio_file):
|
36 |
-
# Check if
|
37 |
if audio_file is None:
|
38 |
-
return "Please upload an audio file."
|
39 |
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
44 |
|
45 |
try:
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# Process the audio file
|
50 |
-
input_features = processor(speech_array, sampling_rate=16000, return_tensors="pt").input_features
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
# Decode token ids to text
|
56 |
-
transcription = processor.batch_decode(predicted_ids, skip_special_tokens=True)[0]
|
57 |
-
|
58 |
-
return transcription
|
59 |
|
|
|
|
|
60 |
except Exception as e:
|
61 |
-
return f"
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
gr.
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
with gr.Column():
|
75 |
-
output = gr.Textbox(label="Transcription Result", lines=10)
|
76 |
-
|
77 |
-
submit_btn.click(fn=transcribe_audio, inputs=audio_input, outputs=output)
|
78 |
-
|
79 |
-
gr.Markdown("### Limitations")
|
80 |
-
gr.Markdown("- Maximum file size: 25 MB")
|
81 |
-
gr.Markdown("- Supported formats: .mp3 and .wav")
|
82 |
-
gr.Markdown("- Uses the Whisper base model which works best with clear audio")
|
83 |
|
84 |
-
# Launch the app
|
85 |
if __name__ == "__main__":
|
86 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import whisper
|
3 |
import os
|
|
|
|
|
|
|
4 |
|
5 |
+
model = whisper.load_model("base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
def transcribe_audio(audio_file):
|
8 |
+
# Check if file is uploaded
|
9 |
if audio_file is None:
|
10 |
+
return "Error: Please upload an audio file.", None
|
11 |
|
12 |
+
# Get the file path - in newer Gradio versions, audio_file might be a string path directly
|
13 |
+
file_path = audio_file if isinstance(audio_file, str) else audio_file.name
|
14 |
+
|
15 |
+
# Check file size (25MB limit)
|
16 |
+
if os.path.getsize(file_path) > 25 * 1024 * 1024:
|
17 |
+
return "Error: File size exceeds 25MB limit.", None
|
18 |
|
19 |
try:
|
20 |
+
result = model.transcribe(file_path)
|
21 |
+
output_filename = os.path.splitext(os.path.basename(file_path))[0] + ".txt"
|
|
|
|
|
|
|
22 |
|
23 |
+
with open(output_filename, "w") as text_file:
|
24 |
+
text_file.write(result["text"])
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
return result["text"], output_filename
|
27 |
+
|
28 |
except Exception as e:
|
29 |
+
return f"Error during transcription: {str(e)}", None
|
30 |
|
31 |
+
iface = gr.Interface(
|
32 |
+
fn=transcribe_audio,
|
33 |
+
inputs=gr.File(label="Upload Audio File (Max 25MB)", file_types=["audio"]),
|
34 |
+
outputs=[
|
35 |
+
gr.Textbox(label="Transcription"),
|
36 |
+
gr.File(label="Download Transcript")
|
37 |
+
],
|
38 |
+
title="Free Transcript Maker",
|
39 |
+
description="Upload an audio file (WAV, MP3, etc.) up to 25MB to get its transcription. The transcript will be displayed and available for download. Please use responsibly."
|
40 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
|
|
42 |
if __name__ == "__main__":
|
43 |
+
iface.launch()
|