Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
import openai
|
4 |
+
import gradio as gr
|
5 |
+
import os
|
6 |
+
from dotenv import load_dotenv
|
7 |
+
|
8 |
+
# Load environment variables from .env file
|
9 |
+
load_dotenv()
|
10 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
11 |
+
|
12 |
+
# Function to scrape content from a URL
|
13 |
+
def scrape_content(url):
|
14 |
+
response = requests.get(url)
|
15 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
16 |
+
|
17 |
+
# Example of extracting title and body content - modify based on actual structure of the websites
|
18 |
+
title = soup.find('title').get_text()
|
19 |
+
paragraphs = soup.find_all('p')
|
20 |
+
content = '\n'.join([para.get_text() for para in paragraphs])
|
21 |
+
|
22 |
+
return title, content
|
23 |
+
|
24 |
+
# Function to summarize content using OpenAI
|
25 |
+
def summarize_content(content):
|
26 |
+
prompt = f"Summarize the following news article in about 60 words:\n\n{content}\n\n"
|
27 |
+
|
28 |
+
response = openai.chat.completions.create(
|
29 |
+
model="gpt-3.5-turbo",
|
30 |
+
messages=[
|
31 |
+
{"role": "system", "content": "You are a helpful assistant that summarizes news articles in about 60 words."},
|
32 |
+
{"role": "user", "content": prompt}
|
33 |
+
],
|
34 |
+
max_tokens=80,
|
35 |
+
temperature=0.7
|
36 |
+
)
|
37 |
+
|
38 |
+
summary = response.choices[0].message.content.strip()
|
39 |
+
return summary
|
40 |
+
|
41 |
+
# Function to process a single URL and generate a summary
|
42 |
+
def process_url(url):
|
43 |
+
if not url:
|
44 |
+
return "No URL provided."
|
45 |
+
|
46 |
+
title, content = scrape_content(url)
|
47 |
+
summary = summarize_content(content)
|
48 |
+
return f"Title: {title}\n\nSummary:\n{summary}"
|
49 |
+
|
50 |
+
# Gradio interface
|
51 |
+
iface = gr.Interface(
|
52 |
+
fn=process_url,
|
53 |
+
inputs=gr.Textbox(lines=2, placeholder="Enter URL here..."),
|
54 |
+
outputs="text",
|
55 |
+
title="News Article Summarizer",
|
56 |
+
description="Enter a News Site URL to generate a 60-word summary."
|
57 |
+
)
|
58 |
+
|
59 |
+
# Launch the interface
|
60 |
+
if __name__ == "__main__":
|
61 |
+
iface.launch(share=True)
|