Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import whisper
|
2 |
+
import openai
|
3 |
+
import gradio as gr
|
4 |
+
from gtts import gTTS
|
5 |
+
from moviepy.editor import VideoFileClip
|
6 |
+
import os
|
7 |
+
|
8 |
+
openai.api_key = "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"
|
9 |
+
|
10 |
+
def transcribe_video(video_path):
|
11 |
+
# Extract audio from video file
|
12 |
+
video = VideoFileClip(video_path)
|
13 |
+
audio_path = "temp_audio.wav"
|
14 |
+
video.audio.write_audiofile(audio_path, codec='pcm_s16le')
|
15 |
+
|
16 |
+
# Load Whisper model and transcribe audio
|
17 |
+
model = whisper.load_model("base")
|
18 |
+
result = model.transcribe(audio_path)
|
19 |
+
transcription = result["text"]
|
20 |
+
|
21 |
+
# Remove temporary audio file
|
22 |
+
os.remove(audio_path)
|
23 |
+
return transcription
|
24 |
+
|
25 |
+
def summarize_text(text):
|
26 |
+
response = openai.Completion.create(
|
27 |
+
engine="text-davinci-003",
|
28 |
+
prompt=f"Summarize the following text:\n\n{text}",
|
29 |
+
max_tokens=150
|
30 |
+
)
|
31 |
+
summary = response.choices[0].text.strip()
|
32 |
+
return summary
|
33 |
+
|
34 |
+
def text_to_speech(text, language="en"):
|
35 |
+
tts = gTTS(text=text, lang=language)
|
36 |
+
tts.save("summary_audio.mp3")
|
37 |
+
return "summary_audio.mp3"
|
38 |
+
|
39 |
+
def process_video(video):
|
40 |
+
# Transcribe the video
|
41 |
+
transcription = transcribe_video(video)
|
42 |
+
|
43 |
+
# Summarize the transcription
|
44 |
+
summary = summarize_text(transcription)
|
45 |
+
|
46 |
+
# Convert summary to speech
|
47 |
+
audio_file = text_to_speech(summary)
|
48 |
+
|
49 |
+
return transcription, summary, audio_file
|
50 |
+
|
51 |
+
# Create Gradio interface
|
52 |
+
iface = gr.Interface(
|
53 |
+
fn=process_video,
|
54 |
+
inputs=gr.Video(label="Upload Video"),
|
55 |
+
outputs=[
|
56 |
+
gr.Textbox(label="Transcription"),
|
57 |
+
gr.Textbox(label="Summary"),
|
58 |
+
gr.Audio(label="Summary Audio")
|
59 |
+
],
|
60 |
+
title="Video Transcription and Summarization",
|
61 |
+
description="Upload a video file to transcribe and summarize its content."
|
62 |
+
)
|
63 |
+
|
64 |
+
# Launch the interface
|
65 |
+
iface.launch()
|