File size: 1,486 Bytes
9d6d683 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
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] # trim for token limits
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()
|