ZeeAI1 commited on
Commit
05bed8e
·
verified ·
1 Parent(s): 81fb804

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -8
app.py CHANGED
@@ -3,10 +3,16 @@ import spacy
3
  from PIL import Image
4
  import os
5
  import cv2
 
6
 
7
- # Load SpaCy model
8
- nlp = spacy.load("en_core_web_sm")
 
 
 
 
9
 
 
10
  ASSET_MAP = {
11
  "man": "assets/characters/man.png",
12
  "woman": "assets/characters/woman.png",
@@ -18,6 +24,7 @@ ASSET_MAP = {
18
  FRAME_FOLDER = "frames"
19
  VIDEO_OUTPUT = "generated_video.mp4"
20
 
 
21
  def extract_entities(prompt):
22
  doc = nlp(prompt)
23
  characters = []
@@ -29,6 +36,7 @@ def extract_entities(prompt):
29
  scenes.append(ent.text.lower())
30
  return characters, scenes
31
 
 
32
  def compose_frame(background_path, character_paths, output_path, char_positions=None):
33
  bg = Image.open(background_path).convert('RGBA')
34
  for idx, char_path in enumerate(character_paths):
@@ -37,6 +45,7 @@ def compose_frame(background_path, character_paths, output_path, char_positions=
37
  bg.paste(char_img, pos, char_img)
38
  bg.save(output_path)
39
 
 
40
  def create_video_from_frames(frame_folder, output_path, fps=24):
41
  images = sorted([img for img in os.listdir(frame_folder) if img.endswith(".png")])
42
  if not images:
@@ -50,31 +59,34 @@ def create_video_from_frames(frame_folder, output_path, fps=24):
50
  video.write(cv2.imread(os.path.join(frame_folder, img)))
51
  video.release()
52
 
 
53
  def generate_video(prompt):
54
  characters, scenes = extract_entities(prompt)
55
 
56
  if not scenes:
57
- return None, "No scene detected! Please mention a place/location in your prompt."
58
 
59
  os.makedirs(FRAME_FOLDER, exist_ok=True)
 
60
  bg_path = ASSET_MAP.get(scenes[0], ASSET_MAP["park"])
61
  char_paths = [ASSET_MAP.get(char, ASSET_MAP["man"]) for char in characters]
62
 
63
- total_frames = 48 # Change to higher for longer video (e.g., 2880 for 2 mins)
64
  for i in range(total_frames):
65
- positions = [(100 + i*2, 200) for _ in char_paths]
66
  frame_path = os.path.join(FRAME_FOLDER, f"frame_{i:03d}.png")
67
  compose_frame(bg_path, char_paths, frame_path, char_positions=positions)
68
 
69
  create_video_from_frames(FRAME_FOLDER, VIDEO_OUTPUT)
70
  return VIDEO_OUTPUT, f"Characters: {characters}, Scenes: {scenes}"
71
 
72
- # Gradio Interface
73
  iface = gr.Interface(
74
  fn=generate_video,
75
  inputs=gr.Textbox(lines=3, placeholder="Describe your scene here..."),
76
  outputs=[gr.Video(), gr.Textbox()],
77
- title="Text to Video AI App (Gradio)"
78
  )
79
 
80
- iface.launch()
 
 
3
  from PIL import Image
4
  import os
5
  import cv2
6
+ import subprocess
7
 
8
+ # --- Dynamic SpaCy model loading (avoids download at build time) ---
9
+ try:
10
+ nlp = spacy.load("en_core_web_sm")
11
+ except OSError:
12
+ subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
13
+ nlp = spacy.load("en_core_web_sm")
14
 
15
+ # --- Asset mapping (character/background library) ---
16
  ASSET_MAP = {
17
  "man": "assets/characters/man.png",
18
  "woman": "assets/characters/woman.png",
 
24
  FRAME_FOLDER = "frames"
25
  VIDEO_OUTPUT = "generated_video.mp4"
26
 
27
+ # --- Extract characters/scenes from prompt ---
28
  def extract_entities(prompt):
29
  doc = nlp(prompt)
30
  characters = []
 
36
  scenes.append(ent.text.lower())
37
  return characters, scenes
38
 
39
+ # --- Compose a single frame ---
40
  def compose_frame(background_path, character_paths, output_path, char_positions=None):
41
  bg = Image.open(background_path).convert('RGBA')
42
  for idx, char_path in enumerate(character_paths):
 
45
  bg.paste(char_img, pos, char_img)
46
  bg.save(output_path)
47
 
48
+ # --- Create video from frames ---
49
  def create_video_from_frames(frame_folder, output_path, fps=24):
50
  images = sorted([img for img in os.listdir(frame_folder) if img.endswith(".png")])
51
  if not images:
 
59
  video.write(cv2.imread(os.path.join(frame_folder, img)))
60
  video.release()
61
 
62
+ # --- Main function triggered by Gradio ---
63
  def generate_video(prompt):
64
  characters, scenes = extract_entities(prompt)
65
 
66
  if not scenes:
67
+ return None, "No scene detected! Please include a place in your prompt."
68
 
69
  os.makedirs(FRAME_FOLDER, exist_ok=True)
70
+
71
  bg_path = ASSET_MAP.get(scenes[0], ASSET_MAP["park"])
72
  char_paths = [ASSET_MAP.get(char, ASSET_MAP["man"]) for char in characters]
73
 
74
+ total_frames = 48 # ~2 seconds at 24fps; increase to 2880 for 2 min
75
  for i in range(total_frames):
76
+ positions = [(100 + i*2, 200) for _ in char_paths] # Simple horizontal movement
77
  frame_path = os.path.join(FRAME_FOLDER, f"frame_{i:03d}.png")
78
  compose_frame(bg_path, char_paths, frame_path, char_positions=positions)
79
 
80
  create_video_from_frames(FRAME_FOLDER, VIDEO_OUTPUT)
81
  return VIDEO_OUTPUT, f"Characters: {characters}, Scenes: {scenes}"
82
 
83
+ # --- Gradio interface ---
84
  iface = gr.Interface(
85
  fn=generate_video,
86
  inputs=gr.Textbox(lines=3, placeholder="Describe your scene here..."),
87
  outputs=[gr.Video(), gr.Textbox()],
88
+ title="Text to Video AI App"
89
  )
90
 
91
+ if __name__ == "__main__":
92
+ iface.launch()