Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from moviepy import VideoFileClip
|
| 4 |
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
import whisper
|
| 6 |
from keybert import KeyBERT
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
-
# Load
|
| 10 |
whisper_model = whisper.load_model("base")
|
| 11 |
kw_model = KeyBERT()
|
| 12 |
|
| 13 |
def process_video(video_path, caption="Your Caption"):
|
| 14 |
-
#
|
| 15 |
clip = VideoFileClip(video_path)
|
| 16 |
-
frame = clip.get_frame(5) # 5 seconds
|
| 17 |
-
image = Image.fromarray(np.uint8(frame))
|
| 18 |
|
| 19 |
-
#
|
|
|
|
|
|
|
| 20 |
draw = ImageDraw.Draw(image)
|
| 21 |
font = ImageFont.load_default()
|
| 22 |
-
font = ImageFont.load_default()
|
| 23 |
-
# Make sure Arial.ttf is available
|
| 24 |
text_position = (50, image.height - 100)
|
| 25 |
-
draw.text(text_position, caption,
|
| 26 |
-
|
| 27 |
thumbnail_path = "thumbnail.jpg"
|
| 28 |
image.save(thumbnail_path)
|
| 29 |
|
| 30 |
-
#
|
| 31 |
-
result = whisper_model.transcribe(video_path)
|
| 32 |
text = result["text"]
|
|
|
|
|
|
|
| 33 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english')
|
| 34 |
keywords_list = [kw[0] for kw in keywords]
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
# Gradio UI
|
| 39 |
with gr.Blocks() as demo:
|
|
@@ -43,8 +50,12 @@ with gr.Blocks() as demo:
|
|
| 43 |
generate_button = gr.Button("Generate Thumbnail & Keywords")
|
| 44 |
thumbnail_output = gr.Image(label="Generated Thumbnail")
|
| 45 |
keywords_output = gr.Textbox(label="SEO Keywords")
|
|
|
|
| 46 |
|
| 47 |
-
generate_button.click(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
# Launch in Hugging Face Spaces
|
| 50 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from moviepy.editor import VideoFileClip, TextClip, CompositeVideoClip
|
|
|
|
| 3 |
from PIL import Image, ImageDraw, ImageFont
|
| 4 |
import whisper
|
| 5 |
from keybert import KeyBERT
|
| 6 |
import numpy as np
|
| 7 |
+
import os
|
| 8 |
|
| 9 |
+
# Load models
|
| 10 |
whisper_model = whisper.load_model("base")
|
| 11 |
kw_model = KeyBERT()
|
| 12 |
|
| 13 |
def process_video(video_path, caption="Your Caption"):
|
| 14 |
+
# Load video
|
| 15 |
clip = VideoFileClip(video_path)
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Extract a frame at 5 seconds and save thumbnail
|
| 18 |
+
frame = clip.get_frame(5)
|
| 19 |
+
image = Image.fromarray(np.uint8(frame))
|
| 20 |
draw = ImageDraw.Draw(image)
|
| 21 |
font = ImageFont.load_default()
|
|
|
|
|
|
|
| 22 |
text_position = (50, image.height - 100)
|
| 23 |
+
draw.text(text_position, caption, fill="white", font=font)
|
|
|
|
| 24 |
thumbnail_path = "thumbnail.jpg"
|
| 25 |
image.save(thumbnail_path)
|
| 26 |
|
| 27 |
+
# Transcribe Hindi audio
|
| 28 |
+
result = whisper_model.transcribe(video_path, language="hi")
|
| 29 |
text = result["text"]
|
| 30 |
+
|
| 31 |
+
# Extract keywords from Hindi text
|
| 32 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english')
|
| 33 |
keywords_list = [kw[0] for kw in keywords]
|
| 34 |
|
| 35 |
+
# Burn caption into video
|
| 36 |
+
txt_clip = TextClip(caption, fontsize=50, color='white', font="Arial", bg_color='black').set_position(("center", "bottom")).set_duration(clip.duration)
|
| 37 |
+
final = CompositeVideoClip([clip, txt_clip])
|
| 38 |
+
|
| 39 |
+
# Save processed video
|
| 40 |
+
output_path = "output_with_caption.mp4"
|
| 41 |
+
final.write_videofile(output_path, codec='libx264', audio_codec='aac')
|
| 42 |
+
|
| 43 |
+
return thumbnail_path, ", ".join(keywords_list), output_path
|
| 44 |
|
| 45 |
# Gradio UI
|
| 46 |
with gr.Blocks() as demo:
|
|
|
|
| 50 |
generate_button = gr.Button("Generate Thumbnail & Keywords")
|
| 51 |
thumbnail_output = gr.Image(label="Generated Thumbnail")
|
| 52 |
keywords_output = gr.Textbox(label="SEO Keywords")
|
| 53 |
+
video_output = gr.File(label="Download Processed Video")
|
| 54 |
|
| 55 |
+
generate_button.click(
|
| 56 |
+
fn=process_video,
|
| 57 |
+
inputs=[video_input, caption_input],
|
| 58 |
+
outputs=[thumbnail_output, keywords_output, video_output]
|
| 59 |
+
)
|
| 60 |
|
|
|
|
| 61 |
demo.launch()
|