dtkne commited on
Commit
949b582
·
verified ·
1 Parent(s): 356bab3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -33
app.py CHANGED
@@ -1,50 +1,53 @@
1
  import gradio as gr
 
2
  import os
 
 
 
3
  from transformers import pipeline
4
 
5
- # Load ASR (Speech-to-Text) pipeline with timestamp handling
6
- asr = pipeline(task="automatic-speech-recognition", model="distil-whisper/distil-small.en")
 
 
 
7
 
8
  # Load Summarization model
9
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
10
 
11
- # Function to transcribe and summarize audio
12
- def transcribe_and_summarize(audio_file):
13
- if audio_file is None:
14
- return "Error: No audio file provided.", ""
15
 
 
16
  try:
17
- # Transcribe audio (handling long-form audio)
18
- transcription_result = asr(audio_file, return_timestamps=True)
19
-
20
- # Extract transcribed text
21
- transcribed_text = " ".join([segment['text'] for segment in transcription_result['chunks']])
22
-
23
- # Ensure the transcribed text isn't too short for summarization
24
- if len(transcribed_text.split()) < 50:
25
- summarized_text = "Text too short to summarize."
26
- else:
27
- # Summarize the transcribed text
28
- summary_result = summarizer(transcribed_text, max_length=100, min_length=30, do_sample=False)
29
- summarized_text = summary_result[0]['summary_text']
30
-
31
- return transcribed_text, summarized_text
 
 
 
32
 
33
  except Exception as e:
34
  return f"Error: {str(e)}", ""
35
 
36
- # Create Gradio interface
 
37
  iface = gr.Interface(
38
- fn=transcribe_and_summarize,
39
- inputs=gr.Audio(type="filepath"), # Accepts an audio file
40
- outputs=[
41
- gr.Textbox(label="Transcribed Text"),
42
- gr.Textbox(label="Summarized Text")
43
- ]
44
  )
45
 
46
- # Get port safely (default to 7860 if not set)
47
- port = int(os.environ.get('PORT1', 7860))
48
-
49
- # Launch Gradio app
50
- iface.launch(share=True, server_port=port)
 
1
  import gradio as gr
2
+ import torch
3
  import os
4
+ import subprocess
5
+ from pytubefix import YouTube
6
+ from moviepy.editor import VideoFileClip
7
  from transformers import pipeline
8
 
9
+ # Ensure required packages are installed inside Hugging Face Spaces
10
+ subprocess.run(["pip", "install", "pytubefix", "moviepy", "transformers", "torchaudio"], check=True)
11
+
12
+ # Load Whisper model for transcription
13
+ asr = pipeline("automatic-speech-recognition", model="distil-whisper/distil-small.en")
14
 
15
  # Load Summarization model
16
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
17
 
 
 
 
 
18
 
19
+ def process_youtube_link(youtube_url):
20
  try:
21
+ # Download YouTube Video
22
+ yt = YouTube(youtube_url)
23
+ video_stream = yt.streams.filter(only_audio=True).first()
24
+ video_path = video_stream.download(filename="video.mp4")
25
+
26
+ # Extract Audio
27
+ audio_path = "audio.wav"
28
+ video = VideoFileClip(video_path)
29
+ video.audio.write_audiofile(audio_path)
30
+
31
+ # Transcribe Audio
32
+ transcription = asr(audio_path)
33
+ transcribed_text = transcription["text"]
34
+
35
+ # Summarize Transcription
36
+ summary = summarizer(transcribed_text, max_length=150, min_length=50, do_sample=False)[0]["summary_text"]
37
+
38
+ return transcribed_text, summary
39
 
40
  except Exception as e:
41
  return f"Error: {str(e)}", ""
42
 
43
+
44
+ # Create Gradio Interface
45
  iface = gr.Interface(
46
+ fn=process_youtube_link,
47
+ inputs=gr.Textbox(label="Enter YouTube URL"),
48
+ outputs=[gr.Textbox(label="Transcription"), gr.Textbox(label="Summary")],
49
+ title="YouTube Video Transcriber & Summarizer",
50
+ description="Enter a YouTube link, and this app will transcribe and summarize the audio.",
 
51
  )
52
 
53
+ iface.launch()