ZennyKenny commited on
Commit
525ee37
·
verified ·
1 Parent(s): 55f93e7

Fix decorator

Browse files
Files changed (1) hide show
  1. app.py +41 -36
app.py CHANGED
@@ -1,46 +1,51 @@
1
  import gradio as gr
2
  import torch
3
  from transformers import pipeline
 
4
 
5
- @spaces.GPU(duration=60)
 
 
 
6
 
7
- # Check if GPU is available; fallback to CPU if not
8
- # device = 0 if torch.cuda.is_available() else -1
9
-
10
- try:
11
- # Load models with error handling
12
- transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=cuda)
13
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
14
- except Exception as e:
15
- print(f"Error loading models: {e}")
16
- raise
17
-
18
- # Function to process audio
19
- def process_audio(audio_file):
20
  try:
21
- # Transcribe the audio
22
- transcription = transcriber(audio_file)["text"]
23
- # Summarize the transcription
24
- summary = summarizer(transcription, max_length=50, min_length=10, do_sample=False)[0]["summary_text"]
25
- return transcription, summary
26
  except Exception as e:
27
- return f"Error processing audio: {e}", ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
- # Gradio Interface with Horizontal Layout
30
- with gr.Blocks() as interface:
31
- with gr.Row():
32
- with gr.Column():
33
- audio_input = gr.Audio(type="filepath", label="Upload Audio File")
34
- process_button = gr.Button("Process Audio")
35
- with gr.Column():
36
- transcription_output = gr.Textbox(label="Full Transcription", lines=10)
37
- summary_output = gr.Textbox(label="Summary", lines=5)
38
 
39
- process_button.click(
40
- process_audio,
41
- inputs=[audio_input],
42
- outputs=[transcription_output, summary_output]
43
- )
44
 
45
- # Launch the interface with optional public sharing
46
- interface.launch(share=True)
 
 
1
  import gradio as gr
2
  import torch
3
  from transformers import pipeline
4
+ import spaces # Ensure spaces library is imported if using GPU decorator
5
 
6
+ @spaces.GPU(duration=60) # Decorator to allocate GPU for the app
7
+ def main():
8
+ # Force GPU if available, fallback to CPU
9
+ device = 0 if torch.cuda.is_available() else -1
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  try:
12
+ # Load models with explicit device
13
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base", device=device)
14
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
 
 
15
  except Exception as e:
16
+ print(f"Error loading models: {e}")
17
+ raise
18
+
19
+ # Function to process audio
20
+ def process_audio(audio_file):
21
+ try:
22
+ # Transcribe the audio
23
+ transcription = transcriber(audio_file)["text"]
24
+ # Summarize the transcription
25
+ summary = summarizer(transcription, max_length=50, min_length=10, do_sample=False)[0]["summary_text"]
26
+ return transcription, summary
27
+ except Exception as e:
28
+ return f"Error processing audio: {e}", ""
29
+
30
+ # Gradio Interface with Horizontal Layout
31
+ with gr.Blocks() as interface:
32
+ with gr.Row():
33
+ with gr.Column():
34
+ audio_input = gr.Audio(type="filepath", label="Upload Audio File")
35
+ process_button = gr.Button("Process Audio")
36
+ with gr.Column():
37
+ transcription_output = gr.Textbox(label="Full Transcription", lines=10)
38
+ summary_output = gr.Textbox(label="Summary", lines=5)
39
 
40
+ process_button.click(
41
+ process_audio,
42
+ inputs=[audio_input],
43
+ outputs=[transcription_output, summary_output]
44
+ )
 
 
 
 
45
 
46
+ # Launch the interface with optional public sharing
47
+ interface.launch(share=True)
 
 
 
48
 
49
+ # Run the main function
50
+ if __name__ == "__main__":
51
+ main()