TIMBOVILL commited on
Commit
70e2653
·
verified ·
1 Parent(s): 4e9051c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -16,7 +16,6 @@ def create_history_messages(history):
16
  def generate_response(prompt, history, model, temperature, max_tokens, top_p, seed):
17
  messages = create_history_messages(history)
18
  messages.append({"role": "user", "content": prompt})
19
- print(messages)
20
 
21
  if seed == 0:
22
  seed = random.randint(1, 100000)
@@ -41,21 +40,29 @@ def generate_response(prompt, history, model, temperature, max_tokens, top_p, se
41
 
42
  return response
43
 
44
- def process_video(video_path, text):
45
- video = VideoFileClip(video_path).subclip(0, min(60, VideoFileClip(video_path).duration))
 
 
 
 
 
 
 
 
46
  video = video.resize(height=1920).crop(x1=video.w // 2 - 540, x2=video.w // 2 + 540)
47
-
48
  text_lines = text.split()
49
  text = "\n".join([" ".join(text_lines[i:i+8]) for i in range(0, len(text_lines), 8)])
50
 
51
  text_clip = TextClip(text, fontsize=70, color='white', size=video.size, method='caption')
52
  text_clip = text_clip.set_position('center').set_duration(video.duration)
53
-
54
  final = CompositeVideoClip([video, text_clip])
55
-
56
  output_path = "output.mp4"
57
  final.write_videofile(output_path, codec="libx264")
58
-
59
  return output_path
60
 
61
  additional_inputs = [
@@ -69,7 +76,7 @@ additional_inputs = [
69
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="pink")) as demo:
70
  with gr.Tabs():
71
  with gr.TabItem("Chat"):
72
- gr.ChatInterface(
73
  fn=generate_response,
74
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
75
  additional_inputs=additional_inputs,
@@ -77,14 +84,13 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="pink")) as
77
  description="Powered by GROQ, MoviePy, and other tools.",
78
  )
79
  with gr.TabItem("Video Processing"):
80
- video_input = gr.Video(label="Upload Video")
81
  text_input = gr.Textbox(lines=5, label="Text (8 words max per line)")
82
  process_button = gr.Button("Process Video")
83
  video_output = gr.Video(label="Processed Video")
84
 
85
  process_button.click(
86
  fn=process_video,
87
- inputs=[video_input, text_input],
88
  outputs=video_output,
89
  )
90
 
 
16
  def generate_response(prompt, history, model, temperature, max_tokens, top_p, seed):
17
  messages = create_history_messages(history)
18
  messages.append({"role": "user", "content": prompt})
 
19
 
20
  if seed == 0:
21
  seed = random.randint(1, 100000)
 
40
 
41
  return response
42
 
43
+ def process_video(text):
44
+ video_folder = "videos"
45
+ video_files = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith(('mp4', 'mov', 'avi', 'mkv'))]
46
+ if not video_files:
47
+ raise FileNotFoundError("No video files found in the specified directory.")
48
+
49
+ selected_video = random.choice(video_files)
50
+ video = VideoFileClip(selected_video)
51
+ start_time = random.uniform(0, max(0, video.duration - 60))
52
+ video = video.subclip(start_time, min(start_time + 60, video.duration))
53
  video = video.resize(height=1920).crop(x1=video.w // 2 - 540, x2=video.w // 2 + 540)
54
+
55
  text_lines = text.split()
56
  text = "\n".join([" ".join(text_lines[i:i+8]) for i in range(0, len(text_lines), 8)])
57
 
58
  text_clip = TextClip(text, fontsize=70, color='white', size=video.size, method='caption')
59
  text_clip = text_clip.set_position('center').set_duration(video.duration)
60
+
61
  final = CompositeVideoClip([video, text_clip])
62
+
63
  output_path = "output.mp4"
64
  final.write_videofile(output_path, codec="libx264")
65
+
66
  return output_path
67
 
68
  additional_inputs = [
 
76
  with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="pink")) as demo:
77
  with gr.Tabs():
78
  with gr.TabItem("Chat"):
79
+ chat_interface = gr.ChatInterface(
80
  fn=generate_response,
81
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
82
  additional_inputs=additional_inputs,
 
84
  description="Powered by GROQ, MoviePy, and other tools.",
85
  )
86
  with gr.TabItem("Video Processing"):
 
87
  text_input = gr.Textbox(lines=5, label="Text (8 words max per line)")
88
  process_button = gr.Button("Process Video")
89
  video_output = gr.Video(label="Processed Video")
90
 
91
  process_button.click(
92
  fn=process_video,
93
+ inputs=[text_input],
94
  outputs=video_output,
95
  )
96