NLPV commited on
Commit
5c19528
Β·
verified Β·
1 Parent(s): 75291ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -35
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 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
- # Try to use font name if available on system
49
- text_clip = TextClip(
50
- caption,
51
- fontsize=50,
52
- color='white',
53
- font="Noto-Sans-Devanagari", # Use system-registered font name (must be installed)
54
- method='caption',
55
- size=(clip.w, None)
56
- )
57
- except Exception as e:
58
- print("TextClip fallback triggered:", str(e))
59
- # Fallback: don't specify font at all
60
- text_clip = TextClip(
61
- caption,
62
- fontsize=50,
63
- color='white',
64
- method='caption',
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 UI ===
78
  with gr.Blocks() as demo:
79
- gr.Markdown("# πŸ“Ή Video Thumbnail & SEO Keyword Generator (Hindi Supported)")
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)