Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from moviepy.editor import VideoFileClip
|
| 3 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
+
import whisper
|
| 5 |
+
from keybert import KeyBERT
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
# Load Whisper model and KeyBERT model
|
| 9 |
+
whisper_model = whisper.load_model("base")
|
| 10 |
+
kw_model = KeyBERT()
|
| 11 |
+
|
| 12 |
+
def process_video(video_path, caption="Your Caption"):
|
| 13 |
+
# Extract frame at 5 seconds
|
| 14 |
+
clip = VideoFileClip(video_path)
|
| 15 |
+
frame = clip.get_frame(5) # 5 seconds
|
| 16 |
+
image = Image.fromarray(np.uint8(frame))
|
| 17 |
+
|
| 18 |
+
# Add caption
|
| 19 |
+
draw = ImageDraw.Draw(image)
|
| 20 |
+
font = ImageFont.truetype("arial.ttf", 40) # Make sure Arial.ttf is available
|
| 21 |
+
text_position = (50, image.height - 100)
|
| 22 |
+
draw.text(text_position, caption, (255, 255, 255), font=font)
|
| 23 |
+
|
| 24 |
+
thumbnail_path = "thumbnail.jpg"
|
| 25 |
+
image.save(thumbnail_path)
|
| 26 |
+
|
| 27 |
+
# Extract keywords
|
| 28 |
+
result = whisper_model.transcribe(video_path)
|
| 29 |
+
text = result["text"]
|
| 30 |
+
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english')
|
| 31 |
+
keywords_list = [kw[0] for kw in keywords]
|
| 32 |
+
|
| 33 |
+
return thumbnail_path, ", ".join(keywords_list)
|
| 34 |
+
|
| 35 |
+
# Gradio UI
|
| 36 |
+
with gr.Blocks() as demo:
|
| 37 |
+
gr.Markdown("# Video Thumbnail Generator with SEO Keywords")
|
| 38 |
+
video_input = gr.File(label="Upload Video", type="filepath")
|
| 39 |
+
caption_input = gr.Textbox(label="Enter Caption for Thumbnail", value="Awesome Video!")
|
| 40 |
+
generate_button = gr.Button("Generate Thumbnail & Keywords")
|
| 41 |
+
thumbnail_output = gr.Image(label="Generated Thumbnail")
|
| 42 |
+
keywords_output = gr.Textbox(label="SEO Keywords")
|
| 43 |
+
|
| 44 |
+
generate_button.click(process_video, inputs=[video_input, caption_input], outputs=[thumbnail_output, keywords_output])
|
| 45 |
+
|
| 46 |
+
# Launch in Hugging Face Spaces
|
| 47 |
+
demo.launch()
|