NLPV commited on
Commit
05d7a1d
·
verified ·
1 Parent(s): 53430f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -29
app.py CHANGED
@@ -6,59 +6,76 @@ 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(
37
- txt=caption,
38
- fontsize=50,
39
- color='white',
40
- font='Arial',
41
- method='caption',
42
- size=(clip.w, None)
43
- ).set_position(("center", "bottom")).set_duration(clip.duration)
44
-
45
- final = CompositeVideoClip([clip, txt_clip])
 
 
 
 
 
 
 
 
46
 
47
- # Save processed video
48
  output_path = "output_with_caption.mp4"
49
- final.write_videofile(output_path, codec='libx264', audio_codec='aac')
50
 
51
  return thumbnail_path, ", ".join(keywords_list), output_path
52
 
53
  # Gradio UI
54
  with gr.Blocks() as demo:
55
- gr.Markdown("# Video Thumbnail Generator with SEO Keywords")
56
- video_input = gr.File(label="Upload Video", type="filepath")
57
- caption_input = gr.Textbox(label="Enter Caption for Thumbnail", value="Awesome Video!")
58
- generate_button = gr.Button("Generate Thumbnail & Keywords")
59
- thumbnail_output = gr.Image(label="Generated Thumbnail")
60
- keywords_output = gr.Textbox(label="SEO Keywords")
61
- video_output = gr.File(label="Download Processed Video")
62
 
63
  generate_button.click(
64
  fn=process_video,
@@ -66,4 +83,4 @@ with gr.Blocks() as demo:
66
  outputs=[thumbnail_output, keywords_output, video_output]
67
  )
68
 
69
- demo.launch()
 
6
  import numpy as np
7
  import os
8
 
9
+ # Load Whisper and KeyBERT models
10
  whisper_model = whisper.load_model("base")
11
  kw_model = KeyBERT()
12
 
13
+ # Path to Hindi-supporting font (adjust if needed)
14
+ FONT_PATH = "/usr/share/fonts/truetype/noto/NotoSansDevanagari-Regular.ttf" # Use system font or bundle locally
15
+
16
  def process_video(video_path, caption="Your Caption"):
17
+ # Load the video
18
  clip = VideoFileClip(video_path)
19
 
20
+ # Extract frame at 5 seconds
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
+ font = ImageFont.truetype(FONT_PATH, size=40)
28
+ except:
29
+ font = ImageFont.load_default()
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
+ # Transcribe audio with Whisper in Hindi
37
  result = whisper_model.transcribe(video_path, language="hi")
38
  text = result["text"]
39
 
40
+ # Extract keywords using KeyBERT
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 caption onto video
45
+ try:
46
+ text_clip = TextClip(
47
+ caption,
48
+ fontsize=50,
49
+ color='white',
50
+ font=FONT_PATH,
51
+ method='caption',
52
+ size=(clip.w, None)
53
+ ).set_position(("center", "bottom")).set_duration(clip.duration)
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
+ ).set_position(("center", "bottom")).set_duration(clip.duration)
63
 
64
+ final = CompositeVideoClip([clip, text_clip])
65
  output_path = "output_with_caption.mp4"
66
+ final.write_videofile(output_path, codec="libx264", audio_codec="aac")
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 Tool (Hindi Supported)")
73
+ video_input = gr.File(label="📁 Upload Video", type="filepath")
74
+ caption_input = gr.Textbox(label="📝 Caption for Thumbnail/Video", value="यह शानदार वीडियो है!")
75
+ generate_button = gr.Button("🚀 Generate Thumbnail, SEO & Video")
76
+ thumbnail_output = gr.Image(label="🖼️ Generated Thumbnail")
77
+ keywords_output = gr.Textbox(label="🔑 Extracted SEO Keywords")
78
+ video_output = gr.File(label="⬇️ Download Final Video")
79
 
80
  generate_button.click(
81
  fn=process_video,
 
83
  outputs=[thumbnail_output, keywords_output, video_output]
84
  )
85
 
86
+ demo.launch(share=True) # share=True creates public link