TIMBOVILL commited on
Commit
8b87f76
·
verified ·
1 Parent(s): 162d15d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -56
app.py CHANGED
@@ -4,64 +4,25 @@ import gradio as gr
4
  from groq import Groq
5
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
6
  import numpy as np
7
- from PIL import Image
8
  import subprocess
9
 
10
  # Ensure ImageMagick is installed
11
  def install_imagemagick():
12
  if not os.path.exists('/usr/bin/convert'):
13
- subprocess.run(['sudo', 'apt-get', 'update'])
14
- subprocess.run(['sudo', 'apt-get', 'install', '-y', 'imagemagick'])
15
-
16
- install_imagemagick
17
-
18
- # Initialize client with API key
19
- client = Groq(
20
- api_key=os.getenv("Groq_Api_Key")
21
- )
22
-
23
- if client.api_key is None:
24
- raise EnvironmentError("Groq_Api_Key environment variable is not set.")
25
-
26
- # Helper to create messages from history
27
- def create_history_messages(history):
28
- history_messages = [{"role": "user", "content": m[0]} for m in history]
29
- history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
30
- return history_messages
31
-
32
- # Generate response function
33
- def generate_response(prompt, history, model, temperature, max_tokens, top_p, seed):
34
- messages = create_history_messages(history)
35
- messages.append({"role": "user", "content": prompt})
36
-
37
- if seed == 0:
38
- seed = random.randint(1, 100000)
39
-
40
- stream = client.chat.completions.create(
41
- messages=messages,
42
- model=model,
43
- temperature=temperature,
44
- max_tokens=max_tokens,
45
- top_p=top_p,
46
- seed=seed,
47
- stop=None,
48
- stream=True,
49
- )
50
-
51
- response = ""
52
- for chunk in stream:
53
- delta_content = chunk.choices[0].delta.content
54
- if delta_content is not None:
55
- response += delta_content
56
- yield response
57
 
58
- return response
59
 
60
- # Process video function
61
- from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
62
- from PIL import Image
 
 
 
 
63
 
64
- # Adjusting MoviePy's resize function to use Image.LANCZOS directly
65
  def process_video(text):
66
  video_folder = "videos"
67
  video_files = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith(('mp4', 'mov', 'avi', 'mkv'))]
@@ -72,22 +33,21 @@ def process_video(text):
72
  video = VideoFileClip(selected_video)
73
  start_time = random.uniform(0, max(0, video.duration - 60))
74
  video = video.subclip(start_time, min(start_time + 60, video.duration))
75
-
76
- # Manually resize using PIL to avoid the issue
77
  def resize_image(image, new_size):
78
  pil_image = Image.fromarray(image)
79
  resized_pil = pil_image.resize(new_size[::-1], Image.LANCZOS)
80
  return np.array(resized_pil)
81
-
82
  new_size = (1080, int(video.h * (1080 / video.w)))
83
  video = video.fl_image(lambda image: resize_image(image, new_size))
84
  video = video.crop(x1=video.w // 2 - 540, x2=video.w // 2 + 540)
85
 
86
  text_lines = text.split()
87
  text = "\n".join([" ".join(text_lines[i:i+8]) for i in range(0, len(text_lines), 8)])
88
-
89
- text_clip = TextClip(text, fontsize=70, color='white', size=video.size, method='caption')
90
- text_clip = text_clip.set_position('center').set_duration(video.duration)
91
 
92
  final = CompositeVideoClip([video, text_clip])
93
 
 
4
  from groq import Groq
5
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
6
  import numpy as np
7
+ from PIL import Image, ImageDraw, ImageFont
8
  import subprocess
9
 
10
  # Ensure ImageMagick is installed
11
  def install_imagemagick():
12
  if not os.path.exists('/usr/bin/convert'):
13
+ subprocess.run(['apt-get', 'update'])
14
+ subprocess.run(['apt-get', 'install', '-y', 'imagemagick'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ install_imagemagick()
17
 
18
+ def create_text_clip(text, fontsize, color, size):
19
+ img = Image.new('RGB', size, color='black')
20
+ draw = ImageDraw.Draw(img)
21
+ font = ImageFont.truetype("arial.ttf", fontsize)
22
+ w, h = draw.textsize(text, font=font)
23
+ draw.text(((size[0] - w) / 2, (size[1] - h) / 2), text, font=font, fill=color)
24
+ return np.array(img)
25
 
 
26
  def process_video(text):
27
  video_folder = "videos"
28
  video_files = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith(('mp4', 'mov', 'avi', 'mkv'))]
 
33
  video = VideoFileClip(selected_video)
34
  start_time = random.uniform(0, max(0, video.duration - 60))
35
  video = video.subclip(start_time, min(start_time + 60, video.duration))
36
+
 
37
  def resize_image(image, new_size):
38
  pil_image = Image.fromarray(image)
39
  resized_pil = pil_image.resize(new_size[::-1], Image.LANCZOS)
40
  return np.array(resized_pil)
41
+
42
  new_size = (1080, int(video.h * (1080 / video.w)))
43
  video = video.fl_image(lambda image: resize_image(image, new_size))
44
  video = video.crop(x1=video.w // 2 - 540, x2=video.w // 2 + 540)
45
 
46
  text_lines = text.split()
47
  text = "\n".join([" ".join(text_lines[i:i+8]) for i in range(0, len(text_lines), 8)])
48
+
49
+ text_img = create_text_clip(text, fontsize=70, color='white', size=video.size)
50
+ text_clip = TextClip(img=text_img, size=video.size, duration=video.duration)
51
 
52
  final = CompositeVideoClip([video, text_clip])
53