Upload 2 files
Browse files- app.py +58 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import openai
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
def fetch_text_from_url(url):
|
| 10 |
+
try:
|
| 11 |
+
response = requests.get(url, timeout=10)
|
| 12 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 13 |
+
paragraphs = soup.find_all('p')
|
| 14 |
+
content = ' '.join([p.get_text() for p in paragraphs])
|
| 15 |
+
return content[:4000] # trim for token limits
|
| 16 |
+
except Exception as e:
|
| 17 |
+
return f"Error fetching content: {str(e)}"
|
| 18 |
+
|
| 19 |
+
def summarize_with_takeaways(content):
|
| 20 |
+
prompt = f"""
|
| 21 |
+
You are a helpful AI agent. Summarize the article and extract 3–5 key takeaways.
|
| 22 |
+
|
| 23 |
+
Article:
|
| 24 |
+
\"\"\"
|
| 25 |
+
{content}
|
| 26 |
+
\"\"\"
|
| 27 |
+
|
| 28 |
+
Return in this format:
|
| 29 |
+
|
| 30 |
+
Summary:
|
| 31 |
+
<your summary>
|
| 32 |
+
|
| 33 |
+
Key Takeaways:
|
| 34 |
+
- ...
|
| 35 |
+
- ...
|
| 36 |
+
- ...
|
| 37 |
+
"""
|
| 38 |
+
response = openai.ChatCompletion.create(
|
| 39 |
+
model="gpt-4o",
|
| 40 |
+
messages=[{"role": "user", "content": prompt}]
|
| 41 |
+
)
|
| 42 |
+
return response.choices[0].message.content.strip()
|
| 43 |
+
|
| 44 |
+
def summarize_url(url):
|
| 45 |
+
content = fetch_text_from_url(url)
|
| 46 |
+
if content.startswith("Error"):
|
| 47 |
+
return content
|
| 48 |
+
return summarize_with_takeaways(content)
|
| 49 |
+
|
| 50 |
+
demo = gr.Interface(
|
| 51 |
+
fn=summarize_url,
|
| 52 |
+
inputs=gr.Textbox(label="Enter Weblink (URL)"),
|
| 53 |
+
outputs=gr.Textbox(label="Summary & Takeaways"),
|
| 54 |
+
title="🌐 Learning Agent: Weblink Summarizer",
|
| 55 |
+
description="Get a concise summary + key takeaways from any article."
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
requests
|
| 3 |
+
beautifulsoup4
|
| 4 |
+
openai
|