MansoorSarookh commited on
Commit
9e5d0be
·
verified ·
1 Parent(s): c9a028f

Create app.py

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