Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain.chains import LLMChain
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain.llms import HuggingFacePipeline
|
5 |
+
from transformers import pipeline
|
6 |
+
from gtts import gTTS
|
7 |
+
from bs4 import BeautifulSoup
|
8 |
+
import tempfile
|
9 |
+
import os
|
10 |
+
import requests
|
11 |
+
from moviepy.editor import *
|
12 |
+
|
13 |
+
# CPU-friendly summarization model
|
14 |
+
summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1)
|
15 |
+
llm = HuggingFacePipeline(pipeline=summary_pipe)
|
16 |
+
|
17 |
+
# LangChain summarization prompt
|
18 |
+
summary_prompt = PromptTemplate.from_template("""
|
19 |
+
Summarize the following article content in a clear, concise way:
|
20 |
+
|
21 |
+
{text}
|
22 |
+
|
23 |
+
Summary:
|
24 |
+
""")
|
25 |
+
summary_chain = LLMChain(llm=llm, prompt=summary_prompt)
|
26 |
+
|
27 |
+
def extract_main_content(url):
|
28 |
+
try:
|
29 |
+
response = requests.get(url, timeout=10)
|
30 |
+
soup = BeautifulSoup(response.content, "html.parser")
|
31 |
+
for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]):
|
32 |
+
tag.decompose()
|
33 |
+
paragraphs = soup.find_all("p")
|
34 |
+
content = "\n".join([p.get_text() for p in paragraphs if len(p.get_text()) > 60])
|
35 |
+
return content.strip()
|
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)
|
42 |
+
if article_text.startswith("Error"):
|
43 |
+
return article_text, None
|
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 |
+
# Create video clip (text overlay + audio)
|
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 |
+
video_clip.write_videofile(video_path, fps=24, codec='libx264')
|
59 |
+
|
60 |
+
return summary, video_path
|
61 |
+
|
62 |
+
except Exception as e:
|
63 |
+
return f"Error: {str(e)}", None
|
64 |
+
|
65 |
+
iface = gr.Interface(
|
66 |
+
fn=url_to_av_summary,
|
67 |
+
inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."),
|
68 |
+
outputs=[
|
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. CPU-only."
|
74 |
+
)
|
75 |
+
|
76 |
+
if __name__ == "__main__":
|
77 |
+
iface.launch()
|