Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 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
|
|
@@ -7,76 +7,75 @@ import numpy as np
|
|
| 7 |
import warnings
|
| 8 |
import os
|
| 9 |
|
| 10 |
-
# Suppress Whisper FP16 warning
|
| 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 |
-
#
|
| 18 |
-
|
| 19 |
|
| 20 |
def process_video(video_path, caption="Your Caption"):
|
| 21 |
-
# Load
|
| 22 |
clip = VideoFileClip(video_path)
|
| 23 |
|
| 24 |
-
# ===
|
| 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(
|
| 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 |
-
# ===
|
| 39 |
result = whisper_model.transcribe(video_path, language="hi")
|
| 40 |
text = result["text"]
|
| 41 |
|
| 42 |
-
# ===
|
| 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 |
-
# ===
|
| 47 |
-
try:
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
size=(clip.w, None)
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
|
| 69 |
text_clip = text_clip.set_position(("center", "bottom")).set_duration(clip.duration)
|
| 70 |
-
|
| 71 |
final_video = CompositeVideoClip([clip, text_clip])
|
|
|
|
| 72 |
output_path = "output_with_caption.mp4"
|
| 73 |
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
|
| 74 |
|
| 75 |
return thumbnail_path, ", ".join(keywords_list), output_path
|
| 76 |
|
| 77 |
-
# === Gradio
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
-
gr.Markdown("# πΉ Video
|
| 80 |
|
| 81 |
with gr.Row():
|
| 82 |
video_input = gr.File(label="π Upload Video", type="filepath")
|
|
@@ -97,4 +96,3 @@ with gr.Blocks() as demo:
|
|
| 97 |
)
|
| 98 |
|
| 99 |
demo.launch(share=True)
|
| 100 |
-
|
|
|
|
| 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
|
|
|
|
| 7 |
import warnings
|
| 8 |
import os
|
| 9 |
|
| 10 |
+
# Suppress Whisper FP16 CPU warning
|
| 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 |
+
# Optional: Hindi-compatible system font (must be pre-installed or accessible by name)
|
| 18 |
+
SYSTEM_FONT_NAME = "Noto-Sans-Devanagari" # This should be installed on the OS
|
| 19 |
|
| 20 |
def process_video(video_path, caption="Your Caption"):
|
| 21 |
+
# === 1. Load Video ===
|
| 22 |
clip = VideoFileClip(video_path)
|
| 23 |
|
| 24 |
+
# === 2. Extract Frame at 5s for Thumbnail ===
|
| 25 |
frame = clip.get_frame(5)
|
| 26 |
image = Image.fromarray(np.uint8(frame))
|
| 27 |
draw = ImageDraw.Draw(image)
|
| 28 |
|
| 29 |
+
# Use PIL to draw caption on thumbnail
|
| 30 |
try:
|
| 31 |
+
pil_font = ImageFont.truetype("/usr/share/fonts/truetype/noto/NotoSansDevanagari-Regular.ttf", size=40)
|
| 32 |
except:
|
| 33 |
pil_font = ImageFont.load_default()
|
| 34 |
|
| 35 |
draw.text((50, image.height - 100), caption, fill="white", font=pil_font)
|
| 36 |
+
|
| 37 |
thumbnail_path = "thumbnail.jpg"
|
| 38 |
image.save(thumbnail_path)
|
| 39 |
|
| 40 |
+
# === 3. Transcribe Audio in Hindi ===
|
| 41 |
result = whisper_model.transcribe(video_path, language="hi")
|
| 42 |
text = result["text"]
|
| 43 |
|
| 44 |
+
# === 4. Extract SEO Keywords ===
|
| 45 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 2), stop_words='english')
|
| 46 |
keywords_list = [kw[0] for kw in keywords]
|
| 47 |
|
| 48 |
+
# === 5. Burn Caption onto Video ===
|
| 49 |
+
try:
|
| 50 |
+
text_clip = TextClip(
|
| 51 |
+
caption,
|
| 52 |
+
fontsize=50,
|
| 53 |
+
color='white',
|
| 54 |
+
font=SYSTEM_FONT_NAME, # Use font name (not file path)
|
| 55 |
+
method='caption',
|
| 56 |
+
size=(clip.w, None)
|
| 57 |
+
)
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print("Font fallback used due to error:", e)
|
| 60 |
+
text_clip = TextClip(
|
| 61 |
+
caption,
|
| 62 |
+
fontsize=50,
|
| 63 |
+
color='white',
|
| 64 |
+
method='caption',
|
| 65 |
+
size=(clip.w, None)
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
text_clip = text_clip.set_position(("center", "bottom")).set_duration(clip.duration)
|
|
|
|
| 69 |
final_video = CompositeVideoClip([clip, text_clip])
|
| 70 |
+
|
| 71 |
output_path = "output_with_caption.mp4"
|
| 72 |
final_video.write_videofile(output_path, codec="libx264", audio_codec="aac", verbose=False, logger=None)
|
| 73 |
|
| 74 |
return thumbnail_path, ", ".join(keywords_list), output_path
|
| 75 |
|
| 76 |
+
# === Gradio Interface ===
|
| 77 |
with gr.Blocks() as demo:
|
| 78 |
+
gr.Markdown("# πΉ Video Caption & SEO Tool (Hindi Supported)")
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
video_input = gr.File(label="π Upload Video", type="filepath")
|
|
|
|
| 96 |
)
|
| 97 |
|
| 98 |
demo.launch(share=True)
|
|
|