RohitCSharp commited on
Commit
4a1e71c
·
verified ·
1 Parent(s): d9287c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -9,6 +9,7 @@ from PIL import Image, ImageDraw, ImageFont
9
  import ffmpeg
10
  import textwrap
11
  import random
 
12
 
13
  # OpenAI LLM
14
  llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.3)
@@ -29,31 +30,23 @@ def extract_main_content(url):
29
  paras = [p.get_text() for p in soup.find_all("p") if len(p.get_text()) > 60]
30
  return "\n".join(paras[:20]) or None
31
 
32
- # Convert uploaded PNG logo to local use
33
- def get_uploaded_logo():
34
- from_path = "CSHARP logo.png"
35
- logo_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
36
- with open(from_path, 'rb') as src, open(logo_path, 'wb') as dst:
37
- dst.write(src.read())
38
- return logo_path
39
-
40
- # Get or generate fallback emoji image
41
- def get_emoji_image():
42
- emoji_path = "/home/user/app/emoji_fire.png"
43
- if os.path.exists(emoji_path):
44
- return Image.open(emoji_path).resize((48, 48)).convert("RGBA")
45
- else:
46
- fallback = Image.new("RGBA", (48, 48), (255, 0, 0, 0))
47
- draw = ImageDraw.Draw(fallback)
48
- draw.ellipse((4, 4, 44, 44), fill=(255, 69, 0, 255))
49
- return fallback
50
-
51
- # Create image slides from text chunks with emojis and logo
52
  def create_slides(text, duration, output_folder, max_lines=6):
53
- font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
54
  font = ImageFont.truetype(font_path, 48)
55
- logo_path = get_uploaded_logo()
56
- emoji_img = get_emoji_image()
57
 
58
  chunks = textwrap.wrap(text, width=36)
59
  slides = ["\n".join(chunks[i:i+max_lines]) for i in range(0, len(chunks), max_lines)]
@@ -61,9 +54,8 @@ def create_slides(text, duration, output_folder, max_lines=6):
61
  slide_paths = []
62
 
63
  for i, slide_text in enumerate(slides):
64
- img = Image.new("RGB", (1280, 720), color=(20, 30, 60))
65
- draw = ImageDraw.Draw(img)
66
-
67
  lines = slide_text.split("\n")
68
  total_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in lines]) + (len(lines)-1)*20
69
  y = max((720 - total_height) // 2, 20)
@@ -72,18 +64,16 @@ def create_slides(text, duration, output_folder, max_lines=6):
72
  w = font.getbbox(line)[2] - font.getbbox(line)[0]
73
  text_x = (1280 - w) // 2
74
  draw.text((text_x, y), line, font=font, fill="white")
75
- img.paste(emoji_img, (text_x - 60, y), emoji_img)
76
- img.paste(emoji_img, (text_x + w + 12, y), emoji_img)
77
  y += font.getbbox(line)[3] - font.getbbox(line)[1] + 20
78
 
79
  logo = Image.open(logo_path).convert("RGBA")
80
- logo_width = min(180, int(0.15 * img.width))
81
  logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
82
  logo = logo.resize((logo_width, logo_height))
83
- img.paste(logo, (img.width - logo_width - 30, img.height - logo_height - 30), logo)
84
 
85
  frame_path = os.path.join(output_folder, f"slide_{i}.png")
86
- img.save(frame_path)
87
  slide_paths.append((frame_path, per_slide_time))
88
 
89
  return slide_paths
@@ -134,8 +124,8 @@ iface = gr.Interface(
134
  gr.Textbox(label="Summary"),
135
  gr.Video(label="Generated AV Summary")
136
  ],
137
- title="🎞️ AV Summary Generator (Multislide with Logo & Emojis)",
138
- description="Generates a 5/10 sec video summary from article URL with clean text, animated slides, logo, and image-based emojis."
139
  )
140
 
141
  if __name__ == '__main__':
 
9
  import ffmpeg
10
  import textwrap
11
  import random
12
+ from urllib.request import urlretrieve
13
 
14
  # OpenAI LLM
15
  llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0.3)
 
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
+ # Load external image assets like logo or graphics from URL
34
+ ASSETS = {
35
+ "logo": "CSHARP logo.png", # Free C# Corner-style logo
36
+ "bg": "https://img.freepik.com/free-photo/blue-abstract-gradient-wave-wallpaper_53876-102605.jpg"
37
+ }
38
+
39
+ def download_asset(url):
40
+ local_path = tempfile.NamedTemporaryFile(delete=False, suffix=".png").name
41
+ urlretrieve(url, local_path)
42
+ return local_path
43
+
44
+ # Create image slides from text chunks
 
 
 
 
 
 
 
 
45
  def create_slides(text, duration, output_folder, max_lines=6):
46
+ font_path = "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf"
47
  font = ImageFont.truetype(font_path, 48)
48
+ logo_path = download_asset(ASSETS["logo"])
49
+ bg_path = download_asset(ASSETS["bg"])
50
 
51
  chunks = textwrap.wrap(text, width=36)
52
  slides = ["\n".join(chunks[i:i+max_lines]) for i in range(0, len(chunks), max_lines)]
 
54
  slide_paths = []
55
 
56
  for i, slide_text in enumerate(slides):
57
+ bg = Image.open(bg_path).resize((1280, 720)).convert("RGBA")
58
+ draw = ImageDraw.Draw(bg)
 
59
  lines = slide_text.split("\n")
60
  total_height = sum([font.getbbox(line)[3] - font.getbbox(line)[1] for line in lines]) + (len(lines)-1)*20
61
  y = max((720 - total_height) // 2, 20)
 
64
  w = font.getbbox(line)[2] - font.getbbox(line)[0]
65
  text_x = (1280 - w) // 2
66
  draw.text((text_x, y), line, font=font, fill="white")
 
 
67
  y += font.getbbox(line)[3] - font.getbbox(line)[1] + 20
68
 
69
  logo = Image.open(logo_path).convert("RGBA")
70
+ logo_width = min(180, int(0.15 * bg.width))
71
  logo_height = int(logo.size[1] * (logo_width / logo.size[0]))
72
  logo = logo.resize((logo_width, logo_height))
73
+ bg.paste(logo, (bg.width - logo_width - 30, bg.height - logo_height - 30), logo)
74
 
75
  frame_path = os.path.join(output_folder, f"slide_{i}.png")
76
+ bg.convert("RGB").save(frame_path)
77
  slide_paths.append((frame_path, per_slide_time))
78
 
79
  return slide_paths
 
124
  gr.Textbox(label="Summary"),
125
  gr.Video(label="Generated AV Summary")
126
  ],
127
+ title="🎞️ AV Summary Generator (Enhanced Slides with Background & Logo)",
128
+ description="Generates a 5/10 sec video summary from article URL with clean text, background visuals, logo, and better visuals."
129
  )
130
 
131
  if __name__ == '__main__':