import os import random import gradio as gr from groq import Groq from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip client = Groq( api_key=os.environ.get("Groq_Api_Key") ) def create_history_messages(history): history_messages = [{"role": "user", "content": m[0]} for m in history] history_messages.extend([{"role": "assistant", "content": m[1]} for m in history]) return history_messages def generate_response(prompt, history, model, temperature, max_tokens, top_p, seed): messages = create_history_messages(history) messages.append({"role": "user", "content": prompt}) if seed == 0: seed = random.randint(1, 100000) stream = client.chat.completions.create( messages=messages, model=model, temperature=temperature, max_tokens=max_tokens, top_p=top_p, seed=seed, stop=None, stream=True, ) response = "" for chunk in stream: delta_content = chunk.choices[0].delta.content if delta_content is not None: response += delta_content yield response return response def process_video(text): video_folder = "videos" video_files = [os.path.join(video_folder, f) for f in os.listdir(video_folder) if f.endswith(('mp4', 'mov', 'avi', 'mkv'))] if not video_files: raise FileNotFoundError("No video files found in the specified directory.") selected_video = random.choice(video_files) video = VideoFileClip(selected_video) start_time = random.uniform(0, max(0, video.duration - 60)) video = video.subclip(start_time, min(start_time + 60, video.duration)) video = video.resize(height=1920).crop(x1=video.w // 2 - 540, x2=video.w // 2 + 540) text_lines = text.split() text = "\n".join([" ".join(text_lines[i:i+8]) for i in range(0, len(text_lines), 8)]) text_clip = TextClip(text, fontsize=70, color='white', size=video.size, method='caption') text_clip = text_clip.set_position('center').set_duration(video.duration) final = CompositeVideoClip([video, text_clip]) output_path = "output.mp4" final.write_videofile(output_path, codec="libx264") return output_path additional_inputs = [ gr.Dropdown(choices=["llama3-70b-8192", "llama3-8b-8192", "mixtral-8x7b-32768", "gemma-7b-it"], value="llama3-70b-8192", label="Model"), 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."), gr.Slider(minimum=1, maximum=32192, step=1, value=4096, label="Max Tokens", info="The maximum number of tokens that the model can process in a single response.
Maximums: 8k for gemma 7b, llama 7b & 70b, 32k for mixtral 8x7b."), gr.Slider(minimum=0.0, maximum=1.0, step=0.01, value=0.5, label="Top P", info="A method of text generation where a model will only consider the most probable next tokens that make up the probability p."), gr.Number(precision=0, value=42, label="Seed", info="A starting point to initiate generation, use 0 for random") ] def chat_interface(): return gr.Interface( fn=generate_response, inputs=[ gr.Textbox(label="Prompt"), gr.Textbox(label="History", type="text"), # Assuming history is provided as text gr.Textbox(label="Model"), gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Temperature"), gr.Slider(minimum=1, maximum=32192, step=1, label="Max Tokens"), gr.Slider(minimum=0.0, maximum=1.0, step=0.01, label="Top P"), gr.Number(precision=0, label="Seed") ], outputs=gr.Textbox(label="Response"), title="YTSHorts Maker - Chat Interface", description="Powered by GROQ.", additional_inputs=additional_inputs, live=True # Enable live updates for the chat interface ) def process_video_interface(): text_input = gr.Textbox(lines=5, label="Text (8 words max per line)") video_output = gr.Video(label="Processed Video") def process_video_callback(text): output_path = process_video(text) return output_path # Return the output path directly return gr.Interface( fn=process_video_callback, inputs=text_input, outputs=video_output, title="YTSHorts Maker - Video Processing", description="Select a video file from 'videos' folder, add text, and process.", ) demo = gr.Interface( fn=None, # No main function needed for multi-interface setup interfaces=[chat_interface(), process_video_interface()], title="YTSHorts Maker", description="Powered by GROQ, MoviePy, and other tools.", theme="soft", ) demo.launch()