nezihtopaloglu commited on
Commit
cd94723
·
1 Parent(s): 96714cf

video size choice and more debugging metrics

Browse files
Files changed (3) hide show
  1. app.py +18 -11
  2. sample_text.txt +15 -0
  3. sample_text_short.txt +3 -0
app.py CHANGED
@@ -23,6 +23,9 @@ def estimate_chunk_durations(text, words_per_second=2.5, min_sec=5, max_sec=10):
23
  current_duration = 0
24
  if current_chunk:
25
  chunks.append(" ".join(current_chunk))
 
 
 
26
  return chunks
27
 
28
  def generate_speech(text):
@@ -31,47 +34,51 @@ def generate_speech(text):
31
  tts.tts_to_file(text=text, file_path=wav_path)
32
  return wav_path
33
 
34
- def generate_images(chunks):
35
  pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
36
  pipe.to("cuda" if torch.cuda.is_available() else "cpu")
37
  image_paths = []
38
  for i, chunk in enumerate(chunks):
 
39
  image = pipe(chunk).images[0]
 
40
  img_path = f"image_{i}.png"
41
  image.save(img_path)
42
  image_paths.append(img_path)
43
  return image_paths
44
 
45
- def create_video(images, durations, speech_path):
46
- clips = [mp.ImageClip(img).set_duration(dur) for img, dur in zip(images, durations)]
47
- black_start = mp.ColorClip((512, 512), color=(0,0,0), duration=1)
48
- black_end = mp.ColorClip((512, 512), color=(0,0,0), duration=2)
49
  video = mp.concatenate_videoclips([black_start] + clips + [black_end])
50
  audio = mp.AudioFileClip(speech_path)
51
  final_video = video.set_audio(audio)
52
  final_video.write_videofile("output.mp4", fps=24)
53
  return "output.mp4"
54
 
55
- def process_text(text):
56
  chunks = estimate_chunk_durations(text)
57
  speech_path = generate_speech(text)
58
- image_paths = generate_images(chunks)
59
  durations = [min(10, max(5, len(chunk.split()) / 2.5)) for chunk in chunks]
60
- video_path = create_video(image_paths, durations, speech_path)
61
  return video_path
62
 
63
  with gr.Blocks() as demo:
64
  gr.Markdown("# Text-to-Video Generator using AI 🎥")
65
  text_input = gr.Textbox(label="Enter your text")
66
  file_input = gr.File(label="Or upload a .txt file")
 
67
  process_btn = gr.Button("Generate Video")
68
  output_video = gr.Video()
69
 
70
- def handle_request(text, file):
71
  if file is not None:
72
  text = open(file.name, "r").read()
73
- return process_text(text)
 
74
 
75
- process_btn.click(handle_request, inputs=[text_input, file_input], outputs=output_video)
76
 
77
  demo.launch()
 
23
  current_duration = 0
24
  if current_chunk:
25
  chunks.append(" ".join(current_chunk))
26
+
27
+ total_time = sum([min(max(len(chunk.split()) / words_per_second, min_sec), max_sec) for chunk in chunks])
28
+ print(f"Total estimated time for video: {total_time:.2f} seconds")
29
  return chunks
30
 
31
  def generate_speech(text):
 
34
  tts.tts_to_file(text=text, file_path=wav_path)
35
  return wav_path
36
 
37
+ def generate_images(chunks, image_size=(640, 480)):
38
  pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
39
  pipe.to("cuda" if torch.cuda.is_available() else "cpu")
40
  image_paths = []
41
  for i, chunk in enumerate(chunks):
42
+ print(f"Generating image for chunk {i + 1} of {len(chunks)}: {chunk[:50]}...") # Printing part of the chunk
43
  image = pipe(chunk).images[0]
44
+ image = image.resize(image_size)
45
  img_path = f"image_{i}.png"
46
  image.save(img_path)
47
  image_paths.append(img_path)
48
  return image_paths
49
 
50
+ def create_video(images, durations, speech_path, image_size=(640, 480)):
51
+ clips = [mp.ImageClip(img).set_duration(dur).resize(image_size) for img, dur in zip(images, durations)]
52
+ black_start = mp.ColorClip(image_size, color=(0,0,0), duration=1)
53
+ black_end = mp.ColorClip(image_size, color=(0,0,0), duration=2)
54
  video = mp.concatenate_videoclips([black_start] + clips + [black_end])
55
  audio = mp.AudioFileClip(speech_path)
56
  final_video = video.set_audio(audio)
57
  final_video.write_videofile("output.mp4", fps=24)
58
  return "output.mp4"
59
 
60
+ def process_text(text, image_size):
61
  chunks = estimate_chunk_durations(text)
62
  speech_path = generate_speech(text)
63
+ image_paths = generate_images(chunks, image_size)
64
  durations = [min(10, max(5, len(chunk.split()) / 2.5)) for chunk in chunks]
65
+ video_path = create_video(image_paths, durations, speech_path, image_size)
66
  return video_path
67
 
68
  with gr.Blocks() as demo:
69
  gr.Markdown("# Text-to-Video Generator using AI 🎥")
70
  text_input = gr.Textbox(label="Enter your text")
71
  file_input = gr.File(label="Or upload a .txt file")
72
+ image_size_input = gr.Radio(choices=["640x480", "800x600", "1024x768"], label="Select Image Size", value="640x480")
73
  process_btn = gr.Button("Generate Video")
74
  output_video = gr.Video()
75
 
76
+ def handle_request(text, file, image_size):
77
  if file is not None:
78
  text = open(file.name, "r").read()
79
+ image_size_dict = {"640x480": (640, 480), "800x600": (800, 600), "1024x768": (1024, 768)}
80
+ return process_text(text, image_size_dict[image_size])
81
 
82
+ process_btn.click(handle_request, inputs=[text_input, file_input, image_size_input], outputs=output_video)
83
 
84
  demo.launch()
sample_text.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Ancient Egypt: A Glimpse into a Lost Civilization
2
+
3
+ Ancient Egypt, one of the most fascinating and enduring civilizations in history, flourished along the Nile River for over 3,000 years. Its civilization developed around 3100 BCE and lasted until Alexander the Great conquered Egypt in 332 BCE. Egypt is famous for its monumental achievements in architecture, art, and culture, many of which have had a lasting influence on the world.
4
+
5
+ The civilization was known for its unique religious beliefs, with gods and goddesses playing a central role in daily life. The ancient Egyptians believed in a complex system of deities, each representing various aspects of life and nature. One of the most famous gods was Ra, the sun god, believed to be the ruler of all gods. The Egyptians also believed in the afterlife, where the soul would continue to exist if one lived a righteous life.
6
+
7
+ The Egyptians’ architectural achievements are also awe-inspiring, with the Great Pyramids of Giza standing as one of the Seven Wonders of the Ancient World. These massive structures were built as tombs for the pharaohs, the god-kings who ruled Egypt. The pyramids symbolize the Egyptians’ belief in the afterlife, where the pharaohs would need a place to rest for eternity.
8
+
9
+ Hieroglyphics, the writing system of Ancient Egypt, is another remarkable achievement. This system of pictorial symbols allowed the Egyptians to record important events, religious texts, and administrative details. Many of the texts written in hieroglyphics have been preserved on tombs, monuments, and papyri, providing us with a window into the daily lives and beliefs of the ancient Egyptians.
10
+
11
+ Another major aspect of Ancient Egypt was its economy, which was primarily based on agriculture. The Nile River’s predictable flooding provided fertile soil for farming, allowing the Egyptians to grow crops like wheat, barley, and flax. The abundance of food helped Egypt maintain a stable and prosperous society.
12
+
13
+ Throughout its long history, Ancient Egypt experienced periods of political stability and turmoil. It was divided into the Old, Middle, and New Kingdoms, with each period characterized by different rulers and societal changes. Some of the most famous pharaohs, including Tutankhamun, Ramses II, and Cleopatra, lived during these different periods.
14
+
15
+ The ancient Egyptians left a profound legacy that still influences modern culture. From the grandeur of the pyramids to the enduring mystery of the Sphinx, Egypt's monuments continue to awe and inspire people around the world.
sample_text_short.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ Ancient Egypt: A Glimpse into a Lost Civilization
2
+
3
+ Ancient Egypt, one of the most fascinating and enduring civilizations in history, flourished along the Nile River for over 3,000 years. Its civilization developed around 3100 BCE and lasted until Alexander the Great conquered Egypt in 332 BCE. Egypt is famous for its monumental achievements in architecture, art, and culture, many of which have had a lasting influence on the world.