Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import youtube_dl
|
3 |
+
from moviepy.editor import VideoFileClip
|
4 |
+
import speech_recognition as sr
|
5 |
+
from transformers import pipeline
|
6 |
+
|
7 |
+
# Initialize the Hugging Face text generation pipeline (using GPT-Neo or GPT-2)
|
8 |
+
generator = pipeline("text-generation", model="EleutherAI/gpt-neo-2.7B")
|
9 |
+
|
10 |
+
def download_video(url):
|
11 |
+
ydl_opts = {
|
12 |
+
'format': 'bestvideo+bestaudio/best',
|
13 |
+
'outtmpl': 'downloaded_video.mp4'
|
14 |
+
}
|
15 |
+
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
16 |
+
ydl.download([url])
|
17 |
+
|
18 |
+
def trim_video(video_path, start_time, end_time):
|
19 |
+
video = VideoFileClip(video_path)
|
20 |
+
trimmed_video = video.subclip(start_time, end_time)
|
21 |
+
trimmed_video.write_videofile("trimmed_video.mp4")
|
22 |
+
|
23 |
+
def transcribe_audio(video_path):
|
24 |
+
recognizer = sr.Recognizer()
|
25 |
+
with sr.AudioFile(video_path) as source:
|
26 |
+
audio = recognizer.record(source)
|
27 |
+
try:
|
28 |
+
text = recognizer.recognize_google(audio) # This uses Google's free Web Speech API
|
29 |
+
return text
|
30 |
+
except sr.UnknownValueError:
|
31 |
+
return "Sorry, I could not understand the audio."
|
32 |
+
except sr.RequestError:
|
33 |
+
return "Error with the API request."
|
34 |
+
|
35 |
+
def generate_subtitles(text):
|
36 |
+
# Use Hugging Face's GPT-Neo model for text generation
|
37 |
+
generated_text = generator(text, max_length=150, num_return_sequences=1)
|
38 |
+
return generated_text[0]['generated_text']
|
39 |
+
|
40 |
+
# Streamlit Interface
|
41 |
+
st.title("YouTube Video Trimmer and Subtitle Generator")
|
42 |
+
url = st.text_input("Enter YouTube URL")
|
43 |
+
start_time = st.selectbox("Select Start Time", [0, 15, 30, 60])
|
44 |
+
end_time = st.selectbox("Select End Time", [15, 30, 60, 90])
|
45 |
+
|
46 |
+
font_style = st.selectbox("Select Font Style", ["Arial", "Helvetica", "Times New Roman", "Courier"])
|
47 |
+
|
48 |
+
if st.button("Generate"):
|
49 |
+
# Download and trim the video
|
50 |
+
download_video(url)
|
51 |
+
trim_video("downloaded_video.mp4", start_time, end_time)
|
52 |
+
|
53 |
+
# Transcribe audio to text
|
54 |
+
transcription = transcribe_audio("trimmed_video.mp4")
|
55 |
+
|
56 |
+
# Generate subtitles using Hugging Face's GPT model
|
57 |
+
subtitles = generate_subtitles(transcription)
|
58 |
+
|
59 |
+
# Show trimmed video and generated subtitles
|
60 |
+
st.video("trimmed_video.mp4")
|
61 |
+
st.subheader("Subtitles:")
|
62 |
+
st.write(subtitles)
|