|
import gradio as gr
|
|
import requests
|
|
from bs4 import BeautifulSoup
|
|
import openai
|
|
import os
|
|
|
|
openai.api_key = os.getenv("OPENAI_API_KEY")
|
|
|
|
def fetch_text_from_url(url):
|
|
try:
|
|
response = requests.get(url, timeout=10)
|
|
soup = BeautifulSoup(response.text, 'html.parser')
|
|
paragraphs = soup.find_all('p')
|
|
content = ' '.join([p.get_text() for p in paragraphs])
|
|
return content[:4000]
|
|
except Exception as e:
|
|
return f"Error fetching content: {str(e)}"
|
|
|
|
def summarize_with_takeaways(content):
|
|
prompt = f"""
|
|
You are a helpful AI agent. Summarize the article and extract 3β5 key takeaways.
|
|
|
|
Article:
|
|
\"\"\"
|
|
{content}
|
|
\"\"\"
|
|
|
|
Return in this format:
|
|
|
|
Summary:
|
|
<your summary>
|
|
|
|
Key Takeaways:
|
|
- ...
|
|
- ...
|
|
- ...
|
|
"""
|
|
response = openai.ChatCompletion.create(
|
|
model="gpt-4o",
|
|
messages=[{"role": "user", "content": prompt}]
|
|
)
|
|
return response.choices[0].message.content.strip()
|
|
|
|
def summarize_url(url):
|
|
content = fetch_text_from_url(url)
|
|
if content.startswith("Error"):
|
|
return content
|
|
return summarize_with_takeaways(content)
|
|
|
|
demo = gr.Interface(
|
|
fn=summarize_url,
|
|
inputs=gr.Textbox(label="Enter Weblink (URL)"),
|
|
outputs=gr.Textbox(label="Summary & Takeaways"),
|
|
title="π Learning Agent: Weblink Summarizer",
|
|
description="Get a concise summary + key takeaways from any article."
|
|
)
|
|
|
|
demo.launch()
|
|
|