Spaces:
Runtime error
Runtime error
| import cv2 | |
| import numpy as np | |
| import torch | |
| import subprocess | |
| text = input("Enter text to convert to video: ") | |
| # Load pre-trained GPT-2 model | |
| model = torch.hub.load('huggingface/transformers', 'gpt2', tokenizer='gpt2-medium') | |
| device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') | |
| model.to(device) | |
| # Generate text tokens from the input text | |
| input_ids = torch.tensor(model.tokenizer.encode(text)).unsqueeze(0).to(device) | |
| # Generate text sequences from the model | |
| with torch.no_grad(): | |
| output_sequences = model.generate(input_ids=input_ids, max_length=1024, temperature=1.0) | |
| # Convert text sequences to video frames | |
| frames = [] | |
| for sequence in output_sequences: | |
| sequence = sequence.cpu().numpy().tolist() | |
| frame = np.zeros((1080, 1920, 3), dtype=np.uint8) | |
| for i in range(len(sequence)): | |
| color = (255, 255, 255) | |
| if sequence[i] == 0: | |
| break | |
| if sequence[i] == 50256: # <eos> token | |
| continue | |
| cv2.putText(frame, model.tokenizer.decode(sequence[i]), (50, (i+1)*70), cv2.FONT_HERSHEY_SIMPLEX, 2, color, 3) | |
| frames.append(frame) | |
| # Save frames as video | |
| fourcc = cv2.VideoWriter_fourcc(*'mp4v') | |
| video_writer = cv2.VideoWriter("output.mp4", fourcc, 25.0, (1920, 1080)) | |
| for frame in frames: | |
| video_writer.write(frame) | |
| video_writer.release() | |
| # Use FFmpeg to add audio to the video | |
| subprocess.call(['ffmpeg', '-i', 'output.mp4', '-i', 'audio.mp3', '-c:v', 'copy', '-c:a', 'aac', '-strict', 'experimental', '-map', '0:v:0', '-map', '1:a:0', '-shortest', 'final_output.mp4']) | |