Spaces:
Sleeping
Sleeping
import gradio as gr | |
from langchain.chains import LLMChain | |
from langchain.prompts import PromptTemplate | |
from langchain.llms import HuggingFacePipeline | |
from transformers import pipeline | |
from gtts import gTTS | |
from bs4 import BeautifulSoup | |
import tempfile | |
import os | |
import requests | |
from moviepy.editor import * | |
# CPU-friendly summarization model | |
summary_pipe = pipeline("text2text-generation", model="google/flan-t5-base", device=-1) | |
llm = HuggingFacePipeline(pipeline=summary_pipe) | |
# LangChain summarization prompt | |
summary_prompt = PromptTemplate.from_template(""" | |
Summarize the following article content in a clear, concise way: | |
{text} | |
Summary: | |
""") | |
summary_chain = LLMChain(llm=llm, prompt=summary_prompt) | |
def extract_main_content(url): | |
try: | |
response = requests.get(url, timeout=10) | |
soup = BeautifulSoup(response.content, "html.parser") | |
for tag in soup(["nav", "header", "footer", "aside", "script", "style", "noscript"]): | |
tag.decompose() | |
paragraphs = soup.find_all("p") | |
content = "\n".join([p.get_text() for p in paragraphs if len(p.get_text()) > 60]) | |
return content.strip() | |
except Exception as e: | |
return f"Error extracting article content: {str(e)}" | |
def url_to_av_summary(url): | |
try: | |
article_text = extract_main_content(url) | |
if article_text.startswith("Error"): | |
return article_text, None | |
summary = summary_chain.run(text=article_text) | |
# Generate speech using gTTS | |
tts = gTTS(text=summary) | |
audio_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3").name | |
tts.save(audio_path) | |
# Create video clip (text overlay + audio) | |
video_clip = TextClip(summary, fontsize=32, color='white', bg_color='black', size=(1280, 720), method='caption') | |
video_clip = video_clip.set_duration(AudioFileClip(audio_path).duration) | |
video_clip = video_clip.set_audio(AudioFileClip(audio_path)) | |
video_path = tempfile.NamedTemporaryFile(delete=False, suffix=".mp4").name | |
video_clip.write_videofile(video_path, fps=24, codec='libx264') | |
return summary, video_path | |
except Exception as e: | |
return f"Error: {str(e)}", None | |
iface = gr.Interface( | |
fn=url_to_av_summary, | |
inputs=gr.Textbox(label="Article URL", placeholder="Paste a news/blog URL here..."), | |
outputs=[ | |
gr.Textbox(label="Summary"), | |
gr.Video(label="Video Summary") | |
], | |
title="URL to AV Summary Agent", | |
description="Summarizes only article content from a URL and creates a narrated video. CPU-only." | |
) | |
if __name__ == "__main__": | |
iface.launch() | |