MuhammadQASIM111 commited on
Commit
a9392c2
·
verified ·
1 Parent(s): 88f4a60

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import whisper
2
+ from moviepy.editor import VideoFileClip
3
+ from transformers import BartForConditionalGeneration, BartTokenizer
4
+ import gradio as gr
5
+ import os
6
+
7
+
8
+ # Load models
9
+ whisper_model = whisper.load_model("base")
10
+ tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
11
+ summarizer_model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
12
+
13
+ def transcribe_and_summarize(video_file):
14
+ try:
15
+ # Step 1: Process the video
16
+ clip = VideoFileClip(video_file)
17
+ audio_file = "audio.wav"
18
+ clip.audio.write_audiofile(audio_file)
19
+
20
+ # Step 2: Transcribe audio
21
+ result = whisper_model.transcribe(audio_file)
22
+ transcribed_text = result['text']
23
+
24
+ # Step 3: Summarize text
25
+ inputs = tokenizer(transcribed_text, return_tensors="pt", max_length=1024, truncation=True)
26
+ summary_ids = summarizer_model.generate(inputs["input_ids"], max_length=150, min_length=30, length_penalty=2.0, num_beams=4, early_stopping=True)
27
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
28
+
29
+ # Clean up the audio file
30
+ os.remove(audio_file)
31
+
32
+ return transcribed_text, summary
33
+
34
+ except Exception as e:
35
+ return str(e), "Error occurred during processing."
36
+
37
+ # Create Gradio interface
38
+ iface = gr.Interface(
39
+ fn=transcribe_and_summarize,
40
+ inputs=gr.Video(label="Upload Video"),
41
+ outputs=[
42
+ gr.Textbox(label="Transcribed Text", lines=10),
43
+ gr.Textbox(label="Summary", lines=5)
44
+ ],
45
+ title="Video Transcription and Summarization",
46
+ description="Upload a video file to transcribe its audio and generate a summary of the transcribed text."
47
+ )
48
+
49
+ # Launch the interface
50
+ iface.launch()