Update app.py
Browse files
app.py
CHANGED
@@ -1,47 +1,49 @@
|
|
1 |
import gradio as gr
|
2 |
-
from moviepy 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 |
-
#
|
|
|
|
|
|
|
10 |
whisper_model = whisper.load_model("base")
|
11 |
kw_model = KeyBERT()
|
12 |
|
13 |
-
# Path to Hindi-supporting font
|
14 |
-
FONT_PATH = "/usr/share/fonts/truetype/noto/NotoSansDevanagari-Regular.ttf"
|
15 |
|
16 |
def process_video(video_path, caption="Your Caption"):
|
17 |
-
# Load
|
18 |
clip = VideoFileClip(video_path)
|
19 |
|
20 |
-
#
|
21 |
frame = clip.get_frame(5)
|
22 |
image = Image.fromarray(np.uint8(frame))
|
23 |
draw = ImageDraw.Draw(image)
|
24 |
|
25 |
-
# Load PIL font (for thumbnail)
|
26 |
try:
|
27 |
-
|
28 |
except:
|
29 |
-
|
30 |
-
|
31 |
-
draw.text((50, image.height - 100), caption, fill="white", font=font)
|
32 |
|
|
|
33 |
thumbnail_path = "thumbnail.jpg"
|
34 |
image.save(thumbnail_path)
|
35 |
|
36 |
-
#
|
37 |
result = whisper_model.transcribe(video_path, language="hi")
|
38 |
text = result["text"]
|
39 |
|
40 |
-
# Extract
|
41 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english')
|
42 |
keywords_list = [kw[0] for kw in keywords]
|
43 |
|
44 |
-
# Burn
|
45 |
try:
|
46 |
text_clip = TextClip(
|
47 |
caption,
|
@@ -50,31 +52,38 @@ def process_video(video_path, caption="Your Caption"):
|
|
50 |
font=FONT_PATH,
|
51 |
method='caption',
|
52 |
size=(clip.w, None)
|
53 |
-
)
|
54 |
except:
|
55 |
-
# fallback in case font doesn't support Hindi
|
56 |
text_clip = TextClip(
|
57 |
caption,
|
58 |
fontsize=50,
|
59 |
color='white',
|
60 |
method='caption',
|
61 |
size=(clip.w, None)
|
62 |
-
)
|
63 |
|
64 |
-
|
|
|
|
|
65 |
output_path = "output_with_caption.mp4"
|
66 |
-
|
67 |
|
68 |
return thumbnail_path, ", ".join(keywords_list), output_path
|
69 |
|
70 |
-
# Gradio UI
|
71 |
with gr.Blocks() as demo:
|
72 |
-
gr.Markdown("# 📹 Video Thumbnail & SEO
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
78 |
video_output = gr.File(label="⬇️ Download Final Video")
|
79 |
|
80 |
generate_button.click(
|
@@ -83,4 +92,5 @@ with gr.Blocks() as demo:
|
|
83 |
outputs=[thumbnail_output, keywords_output, video_output]
|
84 |
)
|
85 |
|
86 |
-
demo.launch(share=True)
|
|
|
|
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 warnings
|
8 |
import os
|
9 |
|
10 |
+
# Suppress Whisper FP16 warning on CPU
|
11 |
+
warnings.filterwarnings("ignore", category=UserWarning, module="whisper")
|
12 |
+
|
13 |
+
# Load models
|
14 |
whisper_model = whisper.load_model("base")
|
15 |
kw_model = KeyBERT()
|
16 |
|
17 |
+
# Path to Hindi-supporting TTF font
|
18 |
+
FONT_PATH = "/usr/share/fonts/truetype/noto/NotoSansDevanagari-Regular.ttf"
|
19 |
|
20 |
def process_video(video_path, caption="Your Caption"):
|
21 |
+
# Load video
|
22 |
clip = VideoFileClip(video_path)
|
23 |
|
24 |
+
# === 1. Extract Thumbnail ===
|
25 |
frame = clip.get_frame(5)
|
26 |
image = Image.fromarray(np.uint8(frame))
|
27 |
draw = ImageDraw.Draw(image)
|
28 |
|
|
|
29 |
try:
|
30 |
+
pil_font = ImageFont.truetype(FONT_PATH, size=40)
|
31 |
except:
|
32 |
+
pil_font = ImageFont.load_default()
|
|
|
|
|
33 |
|
34 |
+
draw.text((50, image.height - 100), caption, fill="white", font=pil_font)
|
35 |
thumbnail_path = "thumbnail.jpg"
|
36 |
image.save(thumbnail_path)
|
37 |
|
38 |
+
# === 2. Transcribe Audio in Hindi ===
|
39 |
result = whisper_model.transcribe(video_path, language="hi")
|
40 |
text = result["text"]
|
41 |
|
42 |
+
# === 3. Extract SEO Keywords ===
|
43 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english')
|
44 |
keywords_list = [kw[0] for kw in keywords]
|
45 |
|
46 |
+
# === 4. Burn Caption onto Video ===
|
47 |
try:
|
48 |
text_clip = TextClip(
|
49 |
caption,
|
|
|
52 |
font=FONT_PATH,
|
53 |
method='caption',
|
54 |
size=(clip.w, None)
|
55 |
+
)
|
56 |
except:
|
|
|
57 |
text_clip = TextClip(
|
58 |
caption,
|
59 |
fontsize=50,
|
60 |
color='white',
|
61 |
method='caption',
|
62 |
size=(clip.w, None)
|
63 |
+
)
|
64 |
|
65 |
+
text_clip = text_clip.set_position(("center", "bottom")).set_duration(clip.duration)
|
66 |
+
|
67 |
+
final_video = CompositeVideoClip([clip, text_clip])
|
68 |
output_path = "output_with_caption.mp4"
|
69 |
+
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
|
70 |
|
71 |
return thumbnail_path, ", ".join(keywords_list), output_path
|
72 |
|
73 |
+
# === Gradio UI ===
|
74 |
with gr.Blocks() as demo:
|
75 |
+
gr.Markdown("# 📹 Video Thumbnail & SEO Keyword Generator (Hindi Supported)")
|
76 |
+
|
77 |
+
with gr.Row():
|
78 |
+
video_input = gr.File(label="📁 Upload Video", type="filepath")
|
79 |
+
caption_input = gr.Textbox(label="📝 Caption for Thumbnail & Video", value="यह शानदार वीडियो है!")
|
80 |
+
|
81 |
+
generate_button = gr.Button("🚀 Generate")
|
82 |
+
|
83 |
+
with gr.Row():
|
84 |
+
thumbnail_output = gr.Image(label="🖼️ Generated Thumbnail")
|
85 |
+
keywords_output = gr.Textbox(label="🔑 Extracted SEO Keywords")
|
86 |
+
|
87 |
video_output = gr.File(label="⬇️ Download Final Video")
|
88 |
|
89 |
generate_button.click(
|
|
|
92 |
outputs=[thumbnail_output, keywords_output, video_output]
|
93 |
)
|
94 |
|
95 |
+
demo.launch(share=True)
|
96 |
+
|