RohitCSharp commited on
Commit
fa05c61
·
verified ·
1 Parent(s): 911c558

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -28
app.py CHANGED
@@ -10,7 +10,6 @@ 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
 
@@ -24,18 +23,14 @@ Summary:
24
  """)
25
  summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
26
 
27
- # Extract article content
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()
@@ -44,8 +39,8 @@ def fetch_unsplash_image(query):
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
 
51
  def download_asset(url):
@@ -53,16 +48,14 @@ def download_asset(url):
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
 
67
  chunks = textwrap.wrap(text, width=36)
68
  slides = ["\n".join(chunks[i:i+max_lines]) for i in range(0, len(chunks), max_lines)]
@@ -72,29 +65,24 @@ def create_slides(text, duration, output_folder, max_lines=6):
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)
81
-
82
  for line in lines:
83
  w = font.getbbox(line)[2] - font.getbbox(line)[0]
84
- text_x = (1280 - w) // 2
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,7 +90,6 @@ def create_slides(text, duration, output_folder, max_lines=6):
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,8 +135,8 @@ iface = gr.Interface(
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__':
 
10
  import textwrap
11
  import random
12
  from urllib.request import urlretrieve
 
13
 
14
  UNSPLASH_KEY = "-7tFgMCy_pwrouZrC8mmEIBpyskEyP25e3_Y4vWSvBs"
15
 
 
23
  """)
24
  summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
25
 
 
26
  def extract_main_content(url):
27
  resp = requests.get(url, timeout=10)
28
  soup = BeautifulSoup(resp.content, "html.parser")
29
+ for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]): tag.decompose()
 
30
  paras = [p.get_text() for p in soup.find_all("p") if len(p.get_text()) > 60]
31
  return "\n".join(paras[:20]) or None
32
 
 
33
  def fetch_unsplash_image(query):
 
34
  url = f"https://api.unsplash.com/photos/random?query={query}&orientation=landscape&client_id={UNSPLASH_KEY}"
35
  try:
36
  resp = requests.get(url).json()
 
39
  return "https://img.freepik.com/free-photo/blue-abstract-gradient-wave-wallpaper_53876-102605.jpg"
40
 
41
  ASSETS = {
42
+ "logo": "https://huggingface.co/spaces/csccorner/Link-to-video/resolve/main/csharplogo.png",
43
+ "illustration": "https://img.freepik.com/free-vector/startup-launch-concept-with-rocket_23-2147866180.jpg"
44
  }
45
 
46
  def download_asset(url):
 
48
  urlretrieve(url, local_path)
49
  return local_path
50
 
 
51
  def create_slides(text, duration, output_folder, max_lines=6):
52
  font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
53
  font = ImageFont.truetype(font_path, 48)
54
  logo_path = download_asset(ASSETS["logo"])
55
+ graphic_path = download_asset(ASSETS["illustration"])
56
 
57
+ query_keywords = random.choice(["blockchain", "AI", "programming", "startup"])
58
+ bg_path = download_asset(fetch_unsplash_image(query_keywords))
 
 
59
 
60
  chunks = textwrap.wrap(text, width=36)
61
  slides = ["\n".join(chunks[i:i+max_lines]) for i in range(0, len(chunks), max_lines)]
 
65
  for i, slide_text in enumerate(slides):
66
  bg = Image.open(bg_path).resize((1280, 720)).convert("RGBA")
67
  enhancer = ImageEnhance.Brightness(bg)
68
+ bg = enhancer.enhance(0.3)
69
  draw = ImageDraw.Draw(bg)
70
 
71
  lines = slide_text.split("\n")
72
  total_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in lines]) + (len(lines)-1)*20
73
  y = max((720 - total_height) // 2, 20)
 
74
  for line in lines:
75
  w = font.getbbox(line)[2] - font.getbbox(line)[0]
76
+ draw.text(((1280 - w) // 2, y), line, font=font, fill="white")
 
77
  y += font.getbbox(line)[3] - font.getbbox(line)[1] + 20
78
 
 
79
  logo = Image.open(logo_path).convert("RGBA")
80
+ logo = logo.resize((160, int(160 * logo.size[1] / logo.size[0])))
81
+ bg.paste(logo, (30, 630 - logo.size[1]), logo)
 
 
82
 
83
+ graphic = Image.open(graphic_path).convert("RGBA")
84
+ graphic = graphic.resize((200, 200))
85
+ bg.paste(graphic, (1040, 40), graphic)
86
 
87
  frame_path = os.path.join(output_folder, f"slide_{i}.png")
88
  bg.convert("RGB").save(frame_path)
 
90
 
91
  return slide_paths
92
 
 
93
  def url_to_av_summary(url, duration):
94
  content = extract_main_content(url)
95
  if not content:
 
135
  gr.Textbox(label="Summary"),
136
  gr.Video(label="Generated AV Summary")
137
  ],
138
+ title="\ud83c\udfae AV Summary Generator (Visual Promo Style)",
139
+ description="Generates a 5/10 sec video summary from article URL with clean typography, visuals, logo and illustration."
140
  )
141
 
142
  if __name__ == '__main__':