|
|
import streamlit as st |
|
|
import torch |
|
|
from transformers import pipeline |
|
|
from moviepy.editor import * |
|
|
from faster_whisper import WhisperModel |
|
|
import os |
|
|
import random |
|
|
import requests |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="AI Text-to-Video Generator", layout="wide") |
|
|
|
|
|
|
|
|
st.title("π AI Text-to-Video Generator") |
|
|
st.markdown("Generate videos from text using AI-powered tools. π₯ποΈ") |
|
|
|
|
|
|
|
|
text_to_video = pipeline("text-to-video", model="damo-vilab/modelscope-text-to-video-synthesis") |
|
|
|
|
|
|
|
|
tts_pipeline = pipeline("text-to-speech", model="coqui-ai/TTS") |
|
|
|
|
|
|
|
|
whisper_model = WhisperModel("base", device="cuda" if torch.cuda.is_available() else "cpu") |
|
|
|
|
|
|
|
|
def generate_video(text_script, use_ai_material=True): |
|
|
output_video_path = f"output_{random.randint(1000,9999)}.mp4" |
|
|
|
|
|
if use_ai_material: |
|
|
video = text_to_video(text_script) |
|
|
video_path = video["video"] |
|
|
else: |
|
|
video_path = "local_video.mp4" |
|
|
|
|
|
return video_path |
|
|
|
|
|
|
|
|
def generate_voice(text_script, voice_choice="default"): |
|
|
audio_path = f"voice_{random.randint(1000,9999)}.wav" |
|
|
tts_audio = tts_pipeline(text_script) |
|
|
|
|
|
with open(audio_path, "wb") as f: |
|
|
f.write(tts_audio["audio"]) |
|
|
|
|
|
return audio_path |
|
|
|
|
|
|
|
|
def generate_captions(audio_path): |
|
|
segments, _ = whisper_model.transcribe(audio_path) |
|
|
captions = "\n".join([segment.text for segment in segments]) |
|
|
|
|
|
return captions |
|
|
|
|
|
|
|
|
text_script = st.text_area("βοΈ Enter your script:", "Type your video script here...") |
|
|
use_ai_material = st.checkbox("Use AI-Generated Material", value=True) |
|
|
voice_choice = st.selectbox("ποΈ Choose Voice Type:", ["default", "robotic", "natural"]) |
|
|
|
|
|
|
|
|
if st.button("π¬ Generate Video"): |
|
|
if text_script: |
|
|
with st.spinner("Generating Video... π"): |
|
|
video_path = generate_video(text_script, use_ai_material) |
|
|
audio_path = generate_voice(text_script, voice_choice) |
|
|
|
|
|
|
|
|
video_clip = VideoFileClip(video_path) |
|
|
audio_clip = AudioFileClip(audio_path) |
|
|
final_clip = video_clip.set_audio(audio_clip) |
|
|
|
|
|
final_video_path = f"final_{random.randint(1000,9999)}.mp4" |
|
|
final_clip.write_videofile(final_video_path, codec="libx264") |
|
|
|
|
|
|
|
|
captions = generate_captions(audio_path) |
|
|
|
|
|
|
|
|
st.video(final_video_path) |
|
|
st.markdown("### π Auto-Generated Captions:") |
|
|
st.write(captions) |
|
|
|
|
|
else: |
|
|
st.warning("Please enter a script to generate a video.") |
|
|
|
|
|
|
|
|
st.markdown("---") |
|
|
st.markdown("## π Search AI-Generated Videos") |
|
|
|
|
|
search_query = st.text_input("Search for a video by script content:") |
|
|
if st.button("π Search"): |
|
|
|
|
|
dummy_videos = { |
|
|
"AI Video 1": "final_1234.mp4", |
|
|
"AI Video 2": "final_5678.mp4" |
|
|
} |
|
|
|
|
|
found_videos = {title: path for title, path in dummy_videos.items() if search_query.lower() in title.lower()} |
|
|
|
|
|
if found_videos: |
|
|
for title, path in found_videos.items(): |
|
|
st.markdown(f"### π₯ {title}") |
|
|
st.video(path) |
|
|
else: |
|
|
st.write("β No videos found matching your search.") |