Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,15 +5,15 @@ from langchain.prompts import PromptTemplate
|
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
from gtts import gTTS
|
7 |
from bs4 import BeautifulSoup
|
8 |
-
from PIL import Image, ImageDraw, ImageFont
|
9 |
import ffmpeg
|
10 |
import textwrap
|
11 |
import random
|
12 |
from urllib.request import urlretrieve
|
|
|
13 |
|
14 |
UNSPLASH_KEY = "-7tFgMCy_pwrouZrC8mmEIBpyskEyP25e3_Y4vWSvBs"
|
15 |
|
16 |
-
# OpenAI LLM
|
17 |
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.3)
|
18 |
summary_prompt = PromptTemplate.from_template("""
|
19 |
Provide a crisp, promotional-style summary (under 50 words) of the following:
|
@@ -28,12 +28,14 @@ summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
|
|
28 |
def extract_main_content(url):
|
29 |
resp = requests.get(url, timeout=10)
|
30 |
soup = BeautifulSoup(resp.content, "html.parser")
|
31 |
-
for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]):
|
|
|
32 |
paras = [p.get_text() for p in soup.find_all("p") if len(p.get_text()) > 60]
|
33 |
return "\n".join(paras[:20]) or None
|
34 |
|
35 |
-
# Unsplash
|
36 |
def fetch_unsplash_image(query):
|
|
|
37 |
url = f"https://api.unsplash.com/photos/random?query={query}&orientation=landscape&client_id={UNSPLASH_KEY}"
|
38 |
try:
|
39 |
resp = requests.get(url).json()
|
@@ -41,9 +43,8 @@ def fetch_unsplash_image(query):
|
|
41 |
except:
|
42 |
return "https://img.freepik.com/free-photo/blue-abstract-gradient-wave-wallpaper_53876-102605.jpg"
|
43 |
|
44 |
-
# Load external image assets
|
45 |
ASSETS = {
|
46 |
-
"logo": "https://
|
47 |
"fallback_graphic": "https://img.freepik.com/free-vector/startup-launch-concept-with-rocket_23-2147866180.jpg"
|
48 |
}
|
49 |
|
@@ -52,14 +53,14 @@ def download_asset(url):
|
|
52 |
urlretrieve(url, local_path)
|
53 |
return local_path
|
54 |
|
55 |
-
# Create image slides
|
56 |
def create_slides(text, duration, output_folder, max_lines=6):
|
57 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
58 |
font = ImageFont.truetype(font_path, 48)
|
59 |
logo_path = download_asset(ASSETS["logo"])
|
60 |
|
61 |
-
|
62 |
-
bg_url = fetch_unsplash_image(
|
63 |
bg_path = download_asset(bg_url)
|
64 |
graphic_path = download_asset(ASSETS["fallback_graphic"])
|
65 |
|
@@ -70,7 +71,10 @@ def create_slides(text, duration, output_folder, max_lines=6):
|
|
70 |
|
71 |
for i, slide_text in enumerate(slides):
|
72 |
bg = Image.open(bg_path).resize((1280, 720)).convert("RGBA")
|
|
|
|
|
73 |
draw = ImageDraw.Draw(bg)
|
|
|
74 |
lines = slide_text.split("\n")
|
75 |
total_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in lines]) + (len(lines)-1)*20
|
76 |
y = max((720 - total_height) // 2, 20)
|
@@ -81,15 +85,16 @@ def create_slides(text, duration, output_folder, max_lines=6):
|
|
81 |
draw.text((text_x, y), line, font=font, fill="white")
|
82 |
y += font.getbbox(line)[3] - font.getbbox(line)[1] + 20
|
83 |
|
|
|
84 |
logo = Image.open(logo_path).convert("RGBA")
|
85 |
-
logo_width = min(180, int(0.
|
86 |
logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
|
87 |
logo = logo.resize((logo_width, logo_height))
|
88 |
-
bg.paste(logo, (
|
89 |
|
90 |
-
|
91 |
-
graphic =
|
92 |
-
bg.paste(graphic, (
|
93 |
|
94 |
frame_path = os.path.join(output_folder, f"slide_{i}.png")
|
95 |
bg.convert("RGB").save(frame_path)
|
@@ -97,7 +102,7 @@ def create_slides(text, duration, output_folder, max_lines=6):
|
|
97 |
|
98 |
return slide_paths
|
99 |
|
100 |
-
# Generate AV
|
101 |
def url_to_av_summary(url, duration):
|
102 |
content = extract_main_content(url)
|
103 |
if not content:
|
@@ -143,8 +148,8 @@ iface = gr.Interface(
|
|
143 |
gr.Textbox(label="Summary"),
|
144 |
gr.Video(label="Generated AV Summary")
|
145 |
],
|
146 |
-
title="
|
147 |
-
description="Generates a 5/10 sec video summary from article URL with
|
148 |
)
|
149 |
|
150 |
if __name__ == '__main__':
|
|
|
5 |
from langchain.chat_models import ChatOpenAI
|
6 |
from gtts import gTTS
|
7 |
from bs4 import BeautifulSoup
|
8 |
+
from PIL import Image, ImageDraw, ImageFont, ImageEnhance
|
9 |
import ffmpeg
|
10 |
import textwrap
|
11 |
import random
|
12 |
from urllib.request import urlretrieve
|
13 |
+
import re
|
14 |
|
15 |
UNSPLASH_KEY = "-7tFgMCy_pwrouZrC8mmEIBpyskEyP25e3_Y4vWSvBs"
|
16 |
|
|
|
17 |
llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.3)
|
18 |
summary_prompt = PromptTemplate.from_template("""
|
19 |
Provide a crisp, promotional-style summary (under 50 words) of the following:
|
|
|
28 |
def extract_main_content(url):
|
29 |
resp = requests.get(url, timeout=10)
|
30 |
soup = BeautifulSoup(resp.content, "html.parser")
|
31 |
+
for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]):
|
32 |
+
tag.decompose()
|
33 |
paras = [p.get_text() for p in soup.find_all("p") if len(p.get_text()) > 60]
|
34 |
return "\n".join(paras[:20]) or None
|
35 |
|
36 |
+
# Fetch Unsplash image
|
37 |
def fetch_unsplash_image(query):
|
38 |
+
query = re.sub(r"[^a-zA-Z0-9 ]", "", query)
|
39 |
url = f"https://api.unsplash.com/photos/random?query={query}&orientation=landscape&client_id={UNSPLASH_KEY}"
|
40 |
try:
|
41 |
resp = requests.get(url).json()
|
|
|
43 |
except:
|
44 |
return "https://img.freepik.com/free-photo/blue-abstract-gradient-wave-wallpaper_53876-102605.jpg"
|
45 |
|
|
|
46 |
ASSETS = {
|
47 |
+
"logo": "https://raw.githubusercontent.com/csccorner/media/main/logos/csharpcorner_black.png",
|
48 |
"fallback_graphic": "https://img.freepik.com/free-vector/startup-launch-concept-with-rocket_23-2147866180.jpg"
|
49 |
}
|
50 |
|
|
|
53 |
urlretrieve(url, local_path)
|
54 |
return local_path
|
55 |
|
56 |
+
# Create image slides with clean layout
|
57 |
def create_slides(text, duration, output_folder, max_lines=6):
|
58 |
font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
|
59 |
font = ImageFont.truetype(font_path, 48)
|
60 |
logo_path = download_asset(ASSETS["logo"])
|
61 |
|
62 |
+
keyword = random.choice(text.split())
|
63 |
+
bg_url = fetch_unsplash_image(keyword)
|
64 |
bg_path = download_asset(bg_url)
|
65 |
graphic_path = download_asset(ASSETS["fallback_graphic"])
|
66 |
|
|
|
71 |
|
72 |
for i, slide_text in enumerate(slides):
|
73 |
bg = Image.open(bg_path).resize((1280, 720)).convert("RGBA")
|
74 |
+
enhancer = ImageEnhance.Brightness(bg)
|
75 |
+
bg = enhancer.enhance(0.4) # darken bg for better contrast
|
76 |
draw = ImageDraw.Draw(bg)
|
77 |
+
|
78 |
lines = slide_text.split("\n")
|
79 |
total_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in lines]) + (len(lines)-1)*20
|
80 |
y = max((720 - total_height) // 2, 20)
|
|
|
85 |
draw.text((text_x, y), line, font=font, fill="white")
|
86 |
y += font.getbbox(line)[3] - font.getbbox(line)[1] + 20
|
87 |
|
88 |
+
# Add logo
|
89 |
logo = Image.open(logo_path).convert("RGBA")
|
90 |
+
logo_width = min(180, int(0.12 * bg.width))
|
91 |
logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
|
92 |
logo = logo.resize((logo_width, logo_height))
|
93 |
+
bg.paste(logo, (bg.width - logo_width - 40, bg.height - logo_height - 30), logo)
|
94 |
|
95 |
+
# Add decorative graphic
|
96 |
+
graphic = Image.open(graphic_path).convert("RGBA").resize((240, 240))
|
97 |
+
bg.paste(graphic, (30, 30), graphic)
|
98 |
|
99 |
frame_path = os.path.join(output_folder, f"slide_{i}.png")
|
100 |
bg.convert("RGB").save(frame_path)
|
|
|
102 |
|
103 |
return slide_paths
|
104 |
|
105 |
+
# Generate AV Summary
|
106 |
def url_to_av_summary(url, duration):
|
107 |
content = extract_main_content(url)
|
108 |
if not content:
|
|
|
148 |
gr.Textbox(label="Summary"),
|
149 |
gr.Video(label="Generated AV Summary")
|
150 |
],
|
151 |
+
title="\U0001F39E️ AV Summary Generator (Visual Promo Style)",
|
152 |
+
description="Generates a 5/10 sec video summary from article URL with summary text, Unsplash images, C# Corner branding, and visuals."
|
153 |
)
|
154 |
|
155 |
if __name__ == '__main__':
|