TIMBOVILL commited on
Commit
c93e6a7
·
verified ·
1 Parent(s): bc69648

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -4,15 +4,27 @@ import gradio as gr
4
  from groq import Groq
5
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
6
 
 
 
 
 
 
 
 
7
  client = Groq(
8
- api_key=os.environ.get("Groq_Api_Key")
9
  )
10
 
 
 
 
 
11
  def create_history_messages(history):
12
  history_messages = [{"role": "user", "content": m[0]} for m in history]
13
  history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
14
  return history_messages
15
 
 
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})
@@ -40,6 +52,7 @@ def generate_response(prompt, history, model, temperature, max_tokens, top_p, se
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'))]
@@ -48,14 +61,15 @@ def process_video(text):
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])
@@ -65,6 +79,7 @@ def process_video(text):
65
 
66
  return output_path
67
 
 
68
  additional_inputs = [
69
  gr.Dropdown(choices=["llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma-7b-it"], value="llama3-70b-8192", label="Model"),
70
  gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Temperature", info="Controls diversity of the generated text. Lower is more deterministic, higher is more creative."),
@@ -73,15 +88,16 @@ additional_inputs = [
73
  gr.Number(precision=0, value=42, label="Seed", info="A starting point to initiate generation, use 0 for random")
74
  ]
75
 
76
- with
77
- gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="pink")) as demo:
78
  with gr.Tabs():
79
  with gr.TabItem("Chat"):
80
  chat_interface = gr.ChatInterface(
81
  fn=generate_response,
82
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
83
- additional_inputs=[],
84
  title="YTSHorts Maker",
 
85
  description="Powered by GROQ, MoviePy, and other tools.",
86
  )
87
  with gr.TabItem("Video Processing"):
@@ -95,4 +111,5 @@ gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="pink")) as demo
95
  outputs=video_output,
96
  )
97
 
 
98
  demo.launch()
 
4
  from groq import Groq
5
  from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
6
 
7
+ ```python
8
+ import gradio as gr
9
+ import os
10
+ import random
11
+ from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
12
+
13
+ # Initialize client with API key
14
  client = Groq(
15
+ api_key=os.getenv("Groq_Api_Key")
16
  )
17
 
18
+ if client.api_key is None:
19
+ raise EnvironmentError("Groq_Api_Key environment variable is not set.")
20
+
21
+ # Helper to create messages from history
22
  def create_history_messages(history):
23
  history_messages = [{"role": "user", "content": m[0]} for m in history]
24
  history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
25
  return history_messages
26
 
27
+ # Generate response function
28
  def generate_response(prompt, history, model, temperature, max_tokens, top_p, seed):
29
  messages = create_history_messages(history)
30
  messages.append({"role": "user", "content": prompt})
 
52
 
53
  return response
54
 
55
+ # Process video function
56
  def process_video(text):
57
  video_folder = "videos"
58
  video_files = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith(('mp4', 'mov', 'avi', 'mkv'))]
 
61
 
62
  selected_video = random.choice(video_files)
63
  video = VideoFileClip(selected_video)
64
+
65
  start_time = random.uniform(0, max(0, video.duration - 60))
66
  video = video.subclip(start_time, min(start_time + 60, video.duration))
67
  video = video.resize(height=1920).crop(x1=video.w // 2 - 540, x2=video.w // 2 + 540)
68
+ text_lines = text.split()
69
+ formatted_text = "
70
+ ".join([" ".join(text_lines[i:i + 8]) for i in range(0, len(text_lines), 8)])
71
 
72
+ text_clip = TextClip(formatted_text, fontsize=70, color='white', size=video.size, method='caption')
 
 
 
73
  text_clip = text_clip.set_position('center').set_duration(video.duration)
74
 
75
  final = CompositeVideoClip([video, text_clip])
 
79
 
80
  return output_path
81
 
82
+ # Additional inputs for the chat interface
83
  additional_inputs = [
84
  gr.Dropdown(choices=["llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma-7b-it"], value="llama3-70b-8192", label="Model"),
85
  gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Temperature", info="Controls diversity of the generated text. Lower is more deterministic, higher is more creative."),
 
88
  gr.Number(precision=0, value=42, label="Seed", info="A starting point to initiate generation, use 0 for random")
89
  ]
90
 
91
+ # Gradio interface with blocks and tabs
92
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="red", secondary_hue="pink")) as demo:
93
  with gr.Tabs():
94
  with gr.TabItem("Chat"):
95
  chat_interface = gr.ChatInterface(
96
  fn=generate_response,
97
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
98
+ additional_inputs=additional_inputs,
99
  title="YTSHorts Maker",
100
+ ```[]
101
  description="Powered by GROQ, MoviePy, and other tools.",
102
  )
103
  with gr.TabItem("Video Processing"):
 
111
  outputs=video_output,
112
  )
113
 
114
+ # Launch the Gradio demo
115
  demo.launch()