Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| from PyPDF2 import PdfReader | |
| from transformers import pipeline | |
| from gtts import gTTS | |
| from moviepy.editor import TextClip, CompositeVideoClip, concatenate_videoclips | |
| import tempfile | |
| import os | |
| # Streamlit app | |
| st.title("PDF to Video Converter") | |
| st.write("Upload a PDF, and the app will create a video summarizing its content.") | |
| # Upload PDF | |
| uploaded_file = st.file_uploader("Upload your PDF file", type=["pdf"]) | |
| if uploaded_file: | |
| with st.spinner("Processing PDF..."): | |
| # Extract text from PDF | |
| pdf_reader = PdfReader(uploaded_file) | |
| text = "" | |
| for page in pdf_reader.pages: | |
| text += page.extract_text() | |
| st.write("Extracted text:") | |
| st.text_area("Document Content", text, height=200) | |
| # Option to summarize text | |
| summarize = st.checkbox("Summarize the content?") | |
| if summarize: | |
| with st.spinner("Summarizing content..."): | |
| summarizer = pipeline("summarization", model="facebook/bart-large-cnn") | |
| summarized_text = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['summary_text'] | |
| text = summarized_text | |
| st.write("Summarized content:") | |
| st.text_area("Summary", text, height=100) | |
| # Generate audio | |
| with st.spinner("Converting text to audio..."): | |
| tts = gTTS(text) | |
| audio_path = tempfile.NamedTemporaryFile(suffix=".mp3", delete=False).name | |
| tts.save(audio_path) | |
| # Generate video | |
| with st.spinner("Creating video..."): | |
| # Create video clips from text | |
| lines = text.split(". ") | |
| clips = [] | |
| for i, line in enumerate(lines): | |
| txt_clip = TextClip(line, fontsize=24, color='white', size=(1280, 720), method='caption') | |
| txt_clip = txt_clip.set_duration(3 + len(line) // 10) # Duration based on text length | |
| clips.append(txt_clip) | |
| # Concatenate all text clips | |
| final_clip = concatenate_videoclips(clips, method="compose") | |
| # Add audio to the video | |
| final_clip = final_clip.set_audio(audio_path) | |
| # Export video | |
| video_path = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name | |
| final_clip.write_videofile(video_path, codec="libx264", fps=24) | |
| st.video(video_path) | |
| # Option to download video | |
| with open(video_path, "rb") as file: | |
| st.download_button( | |
| label="Download Video", | |
| data=file, | |
| file_name="output_video.mp4", | |
| mime="video/mp4" | |
| ) | |
| import os | |
| os.system('apt-get install -y ffmpeg') | |
| from moviepy.editor import TextClip | |
| # Create a text clip | |
| txt_clip = TextClip("Hello World", fontsize=50, color='white') | |
| # Set the duration | |
| txt_clip = txt_clip.set_duration(3) | |
| # Write the video to a file | |
| txt_clip.write_videofile("simple_test.mp4", fps=24) | |