Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
| 3 |
-
import openai
|
| 4 |
import gradio as gr
|
| 5 |
import os
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
|
| 10 |
def scrape_and_summarize(url):
|
| 11 |
"""
|
| 12 |
-
Scrapes the given website URL and summarizes its content using GPT-4o
|
| 13 |
"""
|
| 14 |
try:
|
| 15 |
# Fetch website content
|
|
@@ -25,19 +25,25 @@ def scrape_and_summarize(url):
|
|
| 25 |
if not text_content:
|
| 26 |
return "No readable content found on this page."
|
| 27 |
|
| 28 |
-
# Limit text to 4000 characters for summarization
|
| 29 |
text_content = text_content[:4000]
|
| 30 |
|
| 31 |
-
# Call OpenAI GPT-4o
|
| 32 |
-
response =
|
| 33 |
model="gpt-4o-mini",
|
| 34 |
messages=[
|
| 35 |
{"role": "system", "content": "You are a helpful assistant that summarizes webpage content."},
|
| 36 |
{"role": "user", "content": f"Summarize the following webpage content:\n\n{text_content}"}
|
| 37 |
-
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
)
|
| 39 |
|
| 40 |
-
summary = response
|
| 41 |
return summary
|
| 42 |
|
| 43 |
except requests.exceptions.RequestException as e:
|
|
@@ -59,3 +65,4 @@ with gr.Blocks() as demo:
|
|
| 59 |
# Launch Gradio App
|
| 60 |
if __name__ == "__main__":
|
| 61 |
demo.launch()
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from bs4 import BeautifulSoup
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
| 5 |
+
from openai import OpenAI
|
| 6 |
|
| 7 |
+
# Initialize OpenAI client with secure API key handling
|
| 8 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 9 |
|
| 10 |
def scrape_and_summarize(url):
|
| 11 |
"""
|
| 12 |
+
Scrapes the given website URL and summarizes its content using GPT-4o-mini.
|
| 13 |
"""
|
| 14 |
try:
|
| 15 |
# Fetch website content
|
|
|
|
| 25 |
if not text_content:
|
| 26 |
return "No readable content found on this page."
|
| 27 |
|
| 28 |
+
# Limit text to 4000 characters for better summarization
|
| 29 |
text_content = text_content[:4000]
|
| 30 |
|
| 31 |
+
# Call OpenAI GPT-4o-mini for summarization
|
| 32 |
+
response = client.chat.completions.create(
|
| 33 |
model="gpt-4o-mini",
|
| 34 |
messages=[
|
| 35 |
{"role": "system", "content": "You are a helpful assistant that summarizes webpage content."},
|
| 36 |
{"role": "user", "content": f"Summarize the following webpage content:\n\n{text_content}"}
|
| 37 |
+
],
|
| 38 |
+
response_format={"type": "text"},
|
| 39 |
+
temperature=1,
|
| 40 |
+
max_completion_tokens=2048,
|
| 41 |
+
top_p=1,
|
| 42 |
+
frequency_penalty=0,
|
| 43 |
+
presence_penalty=0
|
| 44 |
)
|
| 45 |
|
| 46 |
+
summary = response.choices[0].message.content # Extract response content
|
| 47 |
return summary
|
| 48 |
|
| 49 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 65 |
# Launch Gradio App
|
| 66 |
if __name__ == "__main__":
|
| 67 |
demo.launch()
|
| 68 |
+
|