dtkne commited on
Commit
5cd1d6b
·
verified ·
1 Parent(s): fd02f98

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -18
app.py CHANGED
@@ -1,33 +1,62 @@
1
  import gradio as gr
 
 
 
 
 
2
  from transformers import pipeline
3
 
4
- # Whisper model
5
- asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
6
 
7
- # Summarization model
 
 
 
8
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
9
 
10
- def process_audio(audio_path):
 
 
 
 
 
11
  try:
12
- # Transcribe Audio
13
- transcription = asr(audio_path, return_timestamps=True)
14
- transcribed_text = transcription["text"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- # Summarize Transcription
17
- summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
18
 
19
- return transcribed_text, summary
20
  except Exception as e:
21
- print(f"Error: {str(e)}") # Log errors
22
  return f"Error: {str(e)}", ""
23
 
24
- # Gradio Interface
 
25
  iface = gr.Interface(
26
- fn=process_audio,
27
- inputs=gr.Audio(source="upload", type="filepath"),
28
- outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Summary")],
29
- title="Audio Summarizer",
30
- description="Upload an audio file, and this app will transcribe and summarize its content.",
 
31
  )
32
 
33
- iface.launch()
 
 
 
 
 
1
  import gradio as gr
2
+
3
+ import os
4
+
5
+
6
+
7
  from transformers import pipeline
8
 
9
+ # Load ASR (Speech-to-Text) pipeline with timestamp handling
10
+ asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
11
 
12
+
13
+
14
+
15
+ # Load Summarization model
16
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
17
 
18
+ # Function to transcribe and summarize audio
19
+ def transcribe_and_summarize(audio_file):
20
+ if audio_file is None:
21
+ return "Error: No audio file provided.", ""
22
+
23
+
24
  try:
25
+ # Transcribe audio (handling long-form audio)
26
+ transcription_result = asr(audio_file, return_timestamps=True)
27
+
28
+ # Extract transcribed text
29
+ transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']])
30
+
31
+ # Ensure the transcribed text isn't too short for summarization
32
+ if len(transcribed_text.split()) < 50:
33
+ summarized_text = "Text too short to summarize."
34
+ else:
35
+ # Summarize the transcribed text
36
+ summary_result = summarizer(transcribed_text, max_length=100, min_length=30, do_sample=False)
37
+ summarized_text = summary_result[0]['summary_text']
38
+
39
+ return transcribed_text, summarized_text
40
+
41
+
42
 
 
 
43
 
 
44
  except Exception as e:
 
45
  return f"Error: {str(e)}", ""
46
 
47
+ # Create Gradio interface
48
+
49
  iface = gr.Interface(
50
+ fn=transcribe_and_summarize,
51
+ inputs=gr.Audio(type="filepath"), # Accepts an audio file
52
+ outputs=[
53
+ gr.Textbox(label="Transcribed Text"),
54
+ gr.Textbox(label="Summarized Text")
55
+ ]
56
  )
57
 
58
+ # Get port safely (default to 7860 if not set)
59
+ port = int(os.environ.get('PORT1', 7860))
60
+
61
+ # Launch Gradio app
62
+ iface.launch(share=True, server_port=port)