RohitCSharp commited on
Commit
b82995c
·
verified ·
1 Parent(s): 5cb4113

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -6
app.py CHANGED
@@ -10,14 +10,15 @@ 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)
16
  llm = HuggingFacePipeline(pipeline=summary_pipe)
17
 
18
- # LangChain summarization prompt
19
  summary_prompt = PromptTemplate.from_template("""
20
- Summarize the following article content in a clear, concise way:
21
 
22
  {text}
23
 
@@ -41,7 +42,7 @@ 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
 
@@ -51,6 +52,7 @@ def generate_video(image_path, audio_path, output_path):
51
  "-loop", "1",
52
  "-i", image_path,
53
  "-i", audio_path,
 
54
  "-c:v", "libx264",
55
  "-tune", "stillimage",
56
  "-c:a", "aac",
@@ -67,6 +69,7 @@ def url_to_av_summary(url):
67
  if article_text.startswith("Error"):
68
  return article_text, None
69
 
 
70
  summary = summary_chain.run(text=article_text)
71
 
72
  tts = gTTS(text=summary)
@@ -84,15 +87,23 @@ def url_to_av_summary(url):
84
  except Exception as e:
85
  return f"Error: {str(e)}", None
86
 
 
 
 
 
 
 
 
 
87
  iface = gr.Interface(
88
- fn=url_to_av_summary,
89
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
90
  outputs=[
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__":
 
10
  import requests
11
  from PIL import Image, ImageDraw, ImageFont
12
  import subprocess
13
+ import concurrent.futures
14
 
15
  # CPU-friendly summarization model
16
  summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
17
  llm = HuggingFacePipeline(pipeline=summary_pipe)
18
 
19
+ # LangChain summarization prompt (short summary)
20
  summary_prompt = PromptTemplate.from_template("""
21
+ Give a crisp and short summary of the following content (under 50 words):
22
 
23
  {text}
24
 
 
42
  img = Image.new("RGB", (1280, 720), color=(0, 0, 0))
43
  draw = ImageDraw.Draw(img)
44
  font = ImageFont.load_default()
45
+ wrapped = summary_text[:512] + ('...' if len(summary_text) > 512 else '')
46
  draw.text((50, 50), wrapped, fill=(255, 255, 255), font=font)
47
  img.save(image_path)
48
 
 
52
  "-loop", "1",
53
  "-i", image_path,
54
  "-i", audio_path,
55
+ "-t", "15",
56
  "-c:v", "libx264",
57
  "-tune", "stillimage",
58
  "-c:a", "aac",
 
69
  if article_text.startswith("Error"):
70
  return article_text, None
71
 
72
+ article_text = article_text[:3000] # Further truncated
73
  summary = summary_chain.run(text=article_text)
74
 
75
  tts = gTTS(text=summary)
 
87
  except Exception as e:
88
  return f"Error: {str(e)}", None
89
 
90
+ def safe_summary_with_timeout(url, timeout_secs=60):
91
+ with concurrent.futures.ThreadPoolExecutor() as executor:
92
+ future = executor.submit(url_to_av_summary, url)
93
+ try:
94
+ return future.result(timeout=timeout_secs)
95
+ except concurrent.futures.TimeoutError:
96
+ return "⏱️ Processing took too long. Try a shorter article.", None
97
+
98
  iface = gr.Interface(
99
+ fn=safe_summary_with_timeout,
100
  inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
101
  outputs=[
102
  gr.Textbox(label="Summary"),
103
  gr.Video(label="Video Summary")
104
  ],
105
+ title="🧠 Short AV Summary from URL",
106
+ description="Extracts clean article content and creates a <15 second narrated video with a short crisp summary. 100% CPU-compatible."
107
  )
108
 
109
  if __name__ == "__main__":