Quick News, Punjabi Voices: The Power of NewsVoiceAI

Adarsh Balan Last Updated : 04 Dec, 2024
8 min read

In today’s fast-paced world, staying informed is crucial, but finding the time to read lengthy news articles can be challenging. Additionally, consuming news in one’s native language enhances understanding and engagement. Enter NewsVoiceAI, an innovative application designed to revolutionize the way we access news. By harnessing the power of artificial intelligence, NewsVoiceAI transforms live English news articles into concise Punjabi audio summaries, making it easier than ever to stay updated while on the go.

In this comprehensive article, we’ll delve into the workings of NewsVoiceAI, explore the cutting-edge technologies behind it, provide a step-by-step guide on how you can create a similar application, and integrate the actual code that brings this application to life.

Learning Outcomes

  • Learn how NewsVoiceAI leverages AI to transform live news articles into concise, native-language audio summaries.
  • Understand the technologies behind NewsVoiceAI, including The Guardian API, OpenAI models, and text-to-speech conversion.
  • Gain hands-on knowledge of integrating APIs and using Streamlit for building interactive applications.
  • Discover how to summarize and translate English news articles into Punjabi using AI-driven models.
  • Explore the process of building a multilingual news summarizer with real-time news fetching and audio output.

This article was published as a part of the Data Science Blogathon.

What is NewsVoice?

Imagine catching up on the latest news during your morning commute without having to read through lengthy articles or struggle with language barriers. NewsVoice was born out of the desire to make news more accessible, especially for Punjabi-speaking audiences who prefer consuming content in their native language. The application bridges several gaps:

  • Time Constraints: Provides concise summaries, saving users time.
  • Language Accessibility: Translates content into Punjabi for native speakers.
  • Ease of Consumption: Converts text to speech, allowing for hands-free listening.

Whether you’re a busy professional, a commuter, or someone with visual impairments, NewsVoiceAI offers a seamless way to stay informed.

Key Features of NewsVoice

  • Live News Fetching: Pulls the latest news articles from reputable sources like The Guardian using their API.
  • AI-Powered Summarization: Utilizes advanced AI models from OpenAI to condense lengthy articles into brief summaries.
  • Punjabi Translation: Ensures regional accessibility by accurately translating summaries into Punjabi.
  • Text-to-Speech Conversion: Generates clear and natural-sounding Punjabi audio using state-of-the-art TTS technologies.
  • User-Friendly Interface: Built with Streamlit, offering an interactive and intuitive user experience.

How NewsVoice Works?

Let’s explore the step-by-step workflow of NewsVoice, integrating code snippets to illustrate each part.

Step1: Fetch News Articles

Using The Guardian API, NewsVoiceAI retrieves the top live news articles based on predefined criteria or user preferences.

Implementation

# Function to fetch news articles from The Guardian API
def fetch_news(api_key):
    from theguardian import theguardian_content
    content = theguardian_content.Content(api=api_key)
    json_content = content.get_content_response()
    try:
        return content.get_results(json_content)
    except KeyError as e:
        st.error(f"Error fetching articles: {e}")
        return []

Explanation:

  • We use the theguardian library to interact with The Guardian API.
  • The fetch_news function retrieves articles and handles any potential errors.

Step2: Fetch Article Content

For each article, we fetch the full content to prepare it for summarization.

Implementation

# Function to fetch article content
def fetch_article_content(article_url, api_key):
    response = requests.get(article_url, params={"api-key": api_key, "show-blocks": "all"})
    if response.status_code == 200:
        article_data = response.json()
        body = article_data.get("response", {}).get("content", {}).get("blocks", {}).get("body", [])
        if body:
            return " ".join(block.get("bodyTextSummary", "") for block in body)
    return None

Explanation:

  • The function sends a GET request to the article’s API URL with parameters to retrieve the full content.
  • It parses the JSON response to extract the text of the article.

Step3: Summarize and Translate Content Using OpenAI API

We use OpenAI’s GPT models to summarize the article and then translate the summary into Punjabi.

Implementation

# Function to summarize and translate content using OpenAI API
def summarize_and_translate(content, api_key):
    import openai
    openai.api_key = api_key
    
    # Summarization Prompt
    summary_response = openai.ChatCompletion.create(
        model="gpt-4", 
        messages=[
            {"role": "system", "content": "You are a helpful assistant that summarizes news articles concisely."},
            {"role": "user", "content": f"Summarize the following article content in 2-3 sentences:\n\n{content}"}
        ],
        max_tokens=100
    )
    summary = summary_response.choices[0].message.content.strip()
    
    # Translation Prompt
    translation_response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a professional translator specializing in English to Punjabi translations."},
            {"role": "user", "content": f"Translate the following text to Punjabi:\n\n{summary}"}
        ],
        max_tokens=150
    )
    translation = translation_response.choices[0].message.content.strip()
    
    return summary, translation

Explanation:

  • We initialize the OpenAI API with the provided key.
  • The summarize_and_translate function first summarizes the article content.
  • It then translates the summary into Punjabi.
  • Both operations use the ChatCompletion endpoint with appropriate prompts.

Step4: Convert Punjabi Text to Speech Using Sarvam TTS API

The translated text is converted into audio using the Sarvam TTS API.

Implementation

# Function to convert Punjabi text to speech using Sarvam TTS API
def punjabi_text_to_speech(punjabi_text, sarvam_api_key):
    url = "https://api.sarvam.ai/text-to-speech"
    
    # Split text into chunks of up to 500 characters
    chunks = [punjabi_text[i:i+500] for i in range(0, len(punjabi_text), 500)]
    audio_clips = []
    
    for i, chunk in enumerate(chunks):
        payload = {
            "inputs": [chunk],
            "target_language_code": "pa-IN",
            "speaker": "meera",
            "pitch": 0,
            "pace": 1.0,
            "loudness": 1.2,
            "speech_sample_rate": 8000,
            "enable_preprocessing": True,
            "model": "bulbul:v1"
        }
        headers = {
            "Content-Type": "application/json",
            "API-Subscription-Key": sarvam_api_key
        }
    
        response = requests.post(url, headers=headers, json=payload)
    
        if response.status_code == 200:
            audio_base64 = response.json().get("audios")
            if audio_base64 and len(audio_base64) > 0:
                audio_clips.append(audio_base64[0])
            else:
                st.error(f"No audio found in response for chunk {i+1}.")
                return None
        else:
            st.error(f"Failed to convert chunk {i+1} to speech: {response.status_code} - {response.text}")
            return None
    
    # Combine all Base64 audio chunks into a single string
    return "".join(audio_clips)

Explanation:

  • The function handles the API’s character limit by splitting the text into chunks.
  • It sends each chunk to the TTS API and collects the Base64-encoded audio.
  • The audio chunks are concatenated to form the complete audio file.

Step5: Build the Streamlit Interface

The user interface is built using Streamlit, providing an interactive experience.

Implementation

# Main Streamlit App
def main():
    st.set_page_config(
        page_title="NewsVoice: Multilingual News Summaries",
        page_icon="📰",
        layout="wide"
    )
    
    # Custom CSS for improved styling
    st.markdown("""
    <style>
    .main-title {
        font-size: 36px;
        color: #2C3E50;
        text-align: center;
        margin-bottom: 30px;
    }
    .section-header {
        color: #3498DB;
        border-bottom: 2px solid #3498DB;
        padding-bottom: 10px;
    }
    </style>
    """, unsafe_allow_html=True)
    
    # App Title
    st.markdown('<h1 class="main-title">🌍 NewsVoice: Multilingual News Summaries</h1>', unsafe_allow_html=True)
    
    # Sidebar for configuration
    st.sidebar.header("News Configuration")
    num_articles = st.sidebar.slider("Number of Articles", 1, 5, 3)
    
    # Main Content Area
    st.markdown('<h2 class="section-header">Latest News Summaries</h2>', unsafe_allow_html=True)
    
    # Fetch and Process Button
    if st.button("Fetch & Translate News"):
        with st.spinner("Fetching and processing news..."):
            # Fetch news articles
            articles = fetch_news(GUARDIAN_API_KEY)
    
            if not articles:
                st.warning("No articles found. Please try again.")
            else:
                # Process top selected number of articles
                for article in articles[:num_articles]:
                    english_title = article.get('webTitle', 'No title available')
                    article_url = article.get('apiUrl')
    
                    # Create a container for each article
                    with st.container():
                        st.subheader(english_title)
    
                        # Fetch article content
                        article_content = fetch_article_content(article_url, GUARDIAN_API_KEY) if article_url else None
                        
                        if not article_content:
                            st.write("No content available for this article.")
                            continue
    
                        try:
                            # Summarize and Translate
                            summary, punjabi_translation = summarize_and_translate(article_content, OPENAI_API_KEY)
                            
                            # Display English Summary
                            st.markdown("**English Summary:**")
                            st.write(summary)
    
                            # Display Punjabi Translation
                            st.markdown("**Punjabi Translation:**")
                            st.write(punjabi_translation)
    
                            # Text-to-Speech
                            st.write("Generating Punjabi audio...")
                            audio_base64 = punjabi_text_to_speech(punjabi_translation, SARVAM_API_KEY)
    
                            if audio_base64:
                                audio_bytes = base64.b64decode(audio_base64)
                                st.audio(audio_bytes, format="audio/wav")
                            
                            # Add a divider between articles
                            st.markdown("---")
    
                        except Exception as e:
                            st.error(f"Error processing article: {e}")
    
    # Footer
    st.markdown("""
    ---
    Powered by The Guardian API, OpenAI, and Sarvam TTS
    """)
    
# Run the Streamlit app
if __name__ == "__main__":
    main()

Explanation:

  • The main function sets up the Streamlit app, including the page configuration and styling.
  • The sidebar allows users to select the number of articles.
  • The main area displays the fetched news, summaries, translations, and audio.
  • Error handling ensures the app informs the user of any issues during processing.
Step5: Build the Streamlit Interface: NewVoice AI

Check out the Audio quality in the recording uploaded in the README of this repository – NewsVoiceAI

How to Obtain the APIs?

To successfully build and run NewsVoice, you’ll need to obtain API keys from the following services:

  • The Guardian Open Platform API
  • OpenAI API
  • Sarvam TTS API

Below are step-by-step guides on how to acquire these API keys.

Guardian Open Platform API

The Guardian provides a free API that allows developers to access content from their vast repository of news articles.

Steps to Obtain The Guardian API Key:

  • Visit The Guardian Open Platform.
  • Scroll down and look for ‘Free instant access for developers’.
  • Click on ‘Register for a developer key’.
  • Fill out the form and Agree to the Terms and Conditions.
  • Submit Your Application.
  • Store Your API Key Securely.

OpenAI API

OpenAI offers powerful language models like GPT-4o and others, which are used for text summarization and translation in NewsVoiceAI.

Steps to Obtain OpenAI API Key

  • Create an OpenAI Account.
  • Verify Your Email and Phone Number.
  • Access the API Dashboard
  • Generate a New API Key.
  • Store Your API Key Securely.

Important Notes

Billing Information:

  • OpenAI’s APIs are paid services. You’ll need to set up a payment method under the
  • New users may receive free credits that expire after a certain period.

Sarvam TTS API

Sarvam AI provides text-to-speech services supporting multiple Indian languages, including Punjabi with about ₹ 1,000 of free credits. Make good use of it!

Steps to Obtain Sarvam TTS API Key:

  • Visit Sarvam AI‘s Website.
  • Sign Up for an Account and Confirm Your Email.
  • Access the Developer Dashboard.
  • Generate an API Key.
  • Store Your API Key Securely.

Technologies Behind NewsVoice

Let us now discuss the technologies behind NewsVoice below:

The Guardian API

The Guardian API provides access to a wealth of live news articles and metadata. By integrating this API, NewsVoice ensures that users receive the most current and relevant news content.

  • Customizable Queries: Fetch articles based on sections, tags, or search terms.
  • Rich Metadata: Access article headlines, authors, publication dates, and more.
  • Reliability: A trusted source of news with global coverage.

OpenAI’s GPT Models

NewsVoiceAI leverages OpenAI’s powerful multilingual language models to handle both summarization and translation tasks.

Summarization

  • Model Used: Either gpt-4o’s November 2024 release or gpt-4o-mini, depending on the desired balance between speed and accuracy.
  • Functionality: Converts lengthy news articles into concise summaries, capturing the essence of the content.
  • Benefits: Reduces reading time while preserving key information.

Translation

  • Model Used: The same OpenAI models are employed for translation tasks.
  • Functionality: Translates English summaries into Punjabi with high linguistic accuracy.
  • Benefits: Enables native Punjabi speakers to consume content comfortably.

Text-to-Speech (TTS) Conversion

To convert translated text into speech, NewsVoiceAI utilizes advanced TTS APIs that support Punjabi.

  • API Used: Sarvam TTS API or any other provider that supports Punjabi.
  • Features:
    • Natural-Sounding Voice: Provides a pleasant listening experience.
    • Large Text Handling: Capable of processing lengthy texts by splitting them into manageable chunks.
  • Benefits: Makes news accessible to those who prefer auditory learning or have visual impairments.

Streamlit

Streamlit is an open-source app framework used to create the interactive web interface for NewsVoice.

  • Features:
    • Easy to Use: Simplifies the process of turning Python scripts into web apps.
    • Interactive Widgets: Allows users to interact with the app seamlessly.
    • Rapid Development: Enables quick prototyping and deployment.
  • Benefits: Provides a user-friendly platform for users to fetch, process, and listen to news effortlessly.

Conclusion

NewsVoiceAI represents a significant step toward making news more accessible and tailored to individual needs. By integrating advanced AI technologies from OpenAI and leveraging reliable news sources like The Guardian, the app transforms the way we consume news:

  • Accessibility: Breaks down language barriers and caters to those with visual impairments.
  • Efficiency: Saves time by providing concise summaries.
  • Convenience: Allows for hands-free consumption through audio playback.

As technology continues to evolve, applications like NewsVoice will play a crucial role in democratizing access to information.

Try it today: NewsVoice GitHub Repository

Acknowledgments

  • The Guardian API: For providing live news content.
  • OpenAI: For the powerful language models used in summarization and translation.
  • Sarvam TTS API: For enabling text-to-speech conversion in Punjabi.
  • Streamlit Community: For the open-source framework that powers the app’s interface.

Additional Resources

Key Takeaways

  • NewsVoiceAI revolutionizes news consumption by providing concise Punjabi audio summaries of live English news articles.
  • The application utilizes AI-powered summarization, translation, and text-to-speech technologies to make news accessible in real-time.
  • NewsVoiceAI supports time-saving, language accessibility, and hands-free listening for users on the go.
  • The system integrates The Guardian API, OpenAI’s GPT models, Sarvam TTS, and Streamlit for a seamless user experience.
  • NewsVoiceAI is ideal for busy professionals, commuters, and those with visual impairments who need easy access to current news.

Frequently Asked Questions

Q1. What is NewsVoiceAI and how does it benefit me?

A. NewsVoiceAI is an AI-powered application that transforms live English news articles into concise Punjabi audio summaries. It benefits users by saving time, breaking language barriers, and providing an accessible, hands-free way to consume news.

Q2. Do I need any technical expertise to use NewsVoice?

A. No technical expertise is required to use the application as an end-user. You can simply interact with the user-friendly interface to fetch, read, and listen to news summaries. However, if you wish to build or customize the application, basic knowledge of Python and API integration would be helpful.

Q3. Is NewsVoiceAI free to use?

A. The NewsVoiceAI application itself is open-source and free to use. However, accessing certain APIs like OpenAI and Sarvam TTS may involve costs, especially beyond their free usage tiers. It’s important to review the pricing details of each API service you plan to use. Although, if you plan on using it locally you may use open source LLM’s using Ollama.

Q4. How accurate are the translations and summaries?

A. NewsVoiceAI utilizes advanced AI models from OpenAI, known for their high accuracy in language tasks. While the translations and summaries are generally reliable, they may not be perfect due to the nuances of language processing. Feedback is welcome to help improve future versions.

Q5. Can I customize the number of articles or select specific news topics?

A. Yes, the application allows you to select the number of articles to fetch through the sidebar slider. Currently, topic selection isn’t available, but future enhancements may include the ability to filter news by categories or keywords.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Hi! I'm Adarsh, a Business Analytics graduate from ISB, currently deep into research and exploring new frontiers. I'm super passionate about data science, AI, and all the innovative ways they can transform industries. Whether it's building models, working on data pipelines, or diving into machine learning, I love experimenting with the latest tech. AI isn't just my interest, it's where I see the future heading, and I'm always excited to be a part of that journey!

Responses From Readers

Clear

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details