EspeMoe commited on
Commit
3a8bd33
·
verified ·
1 Parent(s): 2648187

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -18
app.py CHANGED
@@ -1,32 +1,25 @@
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
- import os # Import os module to handle file paths
4
 
5
- # Load the transcription model
6
- # Using a smaller model that might fit in memory, or consider running on CPU if memory is still an issue
7
- # For demonstration, let's try 'openai/whisper-small'
8
  print("🎤 Loading transcription pipeline...")
9
  try:
10
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cuda:0") # Explicitly use GPU if available
11
  except Exception as e:
12
  print(f"Could not load model on GPU: {e}. Trying on CPU.")
13
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cpu") # Fallback to CPU
14
 
15
 
16
  def transcribe_audio(audio_file_path):
17
- """
18
- Transcribes the audio file using the loaded pipeline.
19
- """
20
  if audio_file_path is None:
21
  return "Please upload an audio file."
22
 
23
- # Ensure the file path is accessible
24
  if not os.path.exists(audio_file_path):
25
  return f"Error: Audio file not found at {audio_file_path}"
26
 
27
  print(f" transcribe the audio file: {audio_file_path}")
28
- # Run transcription on the audio file with chunking
29
- # Adjust chunk_length_s and return_timestamps as needed for your audio
30
  try:
31
  transcription_result = transcriber(audio_file_path, chunk_length_s=30, return_timestamps=True)
32
  return transcription_result["text"]
@@ -34,18 +27,18 @@ def transcribe_audio(audio_file_path):
34
  return f"Error during transcription: {e}"
35
 
36
 
37
- # Create the Gradio interface
38
  print("🚀 Creating Gradio interface for Transcription...")
39
  iface = gr.Interface(
40
  fn=transcribe_audio,
41
- inputs=gr.Audio(type="filepath", label="Upload Audio File"), # Use type="filepath" to get the path to the temporary file
42
  outputs=gr.Textbox(label="Transcription"),
43
  title="Audio Transcription Pipeline",
44
  description="Upload an audio file (e.g., MP3, WAV) to get a transcription."
45
  )
46
 
47
- # Launch the interface
48
  print("✨ Launching Gradio interface...")
49
- # Set share=True to get a public link for sharing (optional)
50
- iface.launch(debug=True, share=True)
51
- print("\n✅ Gradio interface launched.")
 
 
 
1
+ with open("app.py", "w") as f:
2
+ f.write("""
3
  import gradio as gr
4
  from transformers import pipeline
5
+ import os
6
 
 
 
 
7
  print("🎤 Loading transcription pipeline...")
8
  try:
9
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cuda:0")
10
  except Exception as e:
11
  print(f"Could not load model on GPU: {e}. Trying on CPU.")
12
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-small", device="cpu")
13
 
14
 
15
  def transcribe_audio(audio_file_path):
 
 
 
16
  if audio_file_path is None:
17
  return "Please upload an audio file."
18
 
 
19
  if not os.path.exists(audio_file_path):
20
  return f"Error: Audio file not found at {audio_file_path}"
21
 
22
  print(f" transcribe the audio file: {audio_file_path}")
 
 
23
  try:
24
  transcription_result = transcriber(audio_file_path, chunk_length_s=30, return_timestamps=True)
25
  return transcription_result["text"]
 
27
  return f"Error during transcription: {e}"
28
 
29
 
 
30
  print("🚀 Creating Gradio interface for Transcription...")
31
  iface = gr.Interface(
32
  fn=transcribe_audio,
33
+ inputs=gr.Audio(type="filepath", label="Upload Audio File"),
34
  outputs=gr.Textbox(label="Transcription"),
35
  title="Audio Transcription Pipeline",
36
  description="Upload an audio file (e.g., MP3, WAV) to get a transcription."
37
  )
38
 
 
39
  print("✨ Launching Gradio interface...")
40
+ iface.launch()
41
+ print("\n✅ Gradio interface launched.")
42
+ """)
43
+
44
+ print("✅ Saved Gradio app code to app.py")