Download app.py from DivyaSabade/LearningAgentWeblinkSummarizer: direct link, hf CLI and curl.
- Browser
- Download file 1.49 kB
-
https://huggingface.co/spaces/DivyaSabade/LearningAgentWeblinkSummarizer/resolve/main/app.py
- Command line
-
hf download hf://spaces/DivyaSabade/LearningAgentWeblinkSummarizer/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DivyaSabade/LearningAgentWeblinkSummarizer/resolve/main/app.py
1.49 kB
| 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() | |