Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,8 @@ from bs4 import BeautifulSoup
|
|
| 8 |
import tempfile
|
| 9 |
import os
|
| 10 |
import requests
|
| 11 |
-
from
|
|
|
|
| 12 |
|
| 13 |
# CPU-friendly summarization model
|
| 14 |
summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
|
@@ -36,6 +37,30 @@ def extract_main_content(url):
|
|
| 36 |
except Exception as e:
|
| 37 |
return f"Error extracting article content: {str(e)}"
|
| 38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
def url_to_av_summary(url):
|
| 40 |
try:
|
| 41 |
article_text = extract_main_content(url)
|
|
@@ -44,18 +69,15 @@ def url_to_av_summary(url):
|
|
| 44 |
|
| 45 |
summary = summary_chain.run(text=article_text)
|
| 46 |
|
| 47 |
-
# Generate speech using gTTS
|
| 48 |
tts = gTTS(text=summary)
|
| 49 |
audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 50 |
tts.save(audio_path)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
video_clip = TextClip(summary, fontsize=32, color='white', bg_color='black', size=(1280, 720), method='caption')
|
| 54 |
-
video_clip = video_clip.set_duration(AudioFileClip(audio_path).duration)
|
| 55 |
-
video_clip = video_clip.set_audio(AudioFileClip(audio_path))
|
| 56 |
-
|
| 57 |
video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
return summary, video_path
|
| 61 |
|
|
@@ -69,8 +91,8 @@ iface = gr.Interface(
|
|
| 69 |
gr.Textbox(label="Summary"),
|
| 70 |
gr.Video(label="Video Summary")
|
| 71 |
],
|
| 72 |
-
title="URL to AV Summary Agent",
|
| 73 |
-
description="Summarizes only article content from a URL and creates a narrated video
|
| 74 |
)
|
| 75 |
|
| 76 |
if __name__ == "__main__":
|
|
|
|
| 8 |
import tempfile
|
| 9 |
import os
|
| 10 |
import requests
|
| 11 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 12 |
+
import subprocess
|
| 13 |
|
| 14 |
# CPU-friendly summarization model
|
| 15 |
summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
|
|
|
| 37 |
except Exception as e:
|
| 38 |
return f"Error extracting article content: {str(e)}"
|
| 39 |
|
| 40 |
+
def create_text_image(summary_text, image_path):
|
| 41 |
+
img = Image.new("RGB", (1280, 720), color=(0, 0, 0))
|
| 42 |
+
draw = ImageDraw.Draw(img)
|
| 43 |
+
font = ImageFont.load_default()
|
| 44 |
+
wrapped = summary_text[:1024] + ('...' if len(summary_text) > 1024 else '')
|
| 45 |
+
draw.text((50, 50), wrapped, fill=(255, 255, 255), font=font)
|
| 46 |
+
img.save(image_path)
|
| 47 |
+
|
| 48 |
+
def generate_video(image_path, audio_path, output_path):
|
| 49 |
+
cmd = [
|
| 50 |
+
"ffmpeg", "-y",
|
| 51 |
+
"-loop", "1",
|
| 52 |
+
"-i", image_path,
|
| 53 |
+
"-i", audio_path,
|
| 54 |
+
"-c:v", "libx264",
|
| 55 |
+
"-tune", "stillimage",
|
| 56 |
+
"-c:a", "aac",
|
| 57 |
+
"-b:a", "192k",
|
| 58 |
+
"-pix_fmt", "yuv420p",
|
| 59 |
+
"-shortest",
|
| 60 |
+
output_path
|
| 61 |
+
]
|
| 62 |
+
subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
| 63 |
+
|
| 64 |
def url_to_av_summary(url):
|
| 65 |
try:
|
| 66 |
article_text = extract_main_content(url)
|
|
|
|
| 69 |
|
| 70 |
summary = summary_chain.run(text=article_text)
|
| 71 |
|
|
|
|
| 72 |
tts = gTTS(text=summary)
|
| 73 |
audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name
|
| 74 |
tts.save(audio_path)
|
| 75 |
|
| 76 |
+
image_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name
|
| 78 |
+
|
| 79 |
+
create_text_image(summary, image_path)
|
| 80 |
+
generate_video(image_path, audio_path, video_path)
|
| 81 |
|
| 82 |
return summary, video_path
|
| 83 |
|
|
|
|
| 91 |
gr.Textbox(label="Summary"),
|
| 92 |
gr.Video(label="Video Summary")
|
| 93 |
],
|
| 94 |
+
title="URL to AV Summary Agent (No MoviePy)",
|
| 95 |
+
description="Summarizes only article content from a URL and creates a narrated video using ffmpeg + PIL."
|
| 96 |
)
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|