Skip to content

This Python code retrieves an article from a provided URL, extracts its title and text, and then utilizes the ChatOpenAI library (assuming access) to generate a bulleted summary using the GPT-4 model.

Notifications You must be signed in to change notification settings

naveens800/news_article_summarizer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Title: AI News Article Summarizer with ChatOpenAI

Description:

This Python code retrieves an article from a provided URL, extracts its title and text, and then utilizes the ChatOpenAI library (assuming access) to generate a bulleted summary using the GPT-4 model.

Install Dependencies:

pip install requests newspaper dotenv langchain chat-openai

Replace Placeholder URL:

Update article_url with the actual URL of the article you want to summarize. Run the Script:

Execute the Python script using a command like:

python your_script_name.py

Code Breakdown:

Imports:

  • json: For potential JSON data handling (not explicitly used in this code).
  • requests: To fetch web content from the provided URL.
  • newspaper: To extract title and text from the downloaded article.
  • dotenv: To manage environment variables (not used in the current version).
  • langchain.schema.HumanMessage: Defines the message format for ChatOpenAI.
  • langchain_community.chat_models.ChatOpenAI: Enables interaction with ChatOpenAI.

Headers:

  • Defines a user agent string for the HTTP request headers.

Article URL and Session:

  • Sets the article_url variable to hold the article's URL.
  • Creates a requests.Session object for managing HTTP requests.

Fetching Article Content:

  • Attempts to retrieve the article using requests.get.
  • If successful (status code 200), creates a newspaper.Article object, downloads and parses the article.
  • Extracts title and text using article.title and article.text.
  • Prints an error message if fetching fails.

Prompt Template:

  • Constructs a template string using f-strings for formatted text insertion.
  • Inserts the retrieved article_title and article_text into the template.

ChatOpenAI Interaction:

  • (Assuming Access) Creates a ChatOpenAI object with the gpt-4 model (if available) and sets the temperature parameter (controls randomness).
  • (Assuming Access) Creates a list of HumanMessage objects containing the prompt.
  • (Assuming Access) Generates a summary using chat(messages).
  • (Assuming Access) Prints the generated summary content.

Additional Notes:

  • Consider exploring alternative summarization libraries or techniques (e.g., Gensim, spaCy) for broader applicability if access to ChatOpenAI's GPT-4 model is limited.
  • The code can be further enhanced with additional features like:
    • User input for the article URL.
    • Support for different summarization models.
    • Output formatting options (bullet points, text, etc.).

About

This Python code retrieves an article from a provided URL, extracts its title and text, and then utilizes the ChatOpenAI library (assuming access) to generate a bulleted summary using the GPT-4 model.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages