2.1 C
New York
Tuesday, December 3, 2024

Get Fast AI Information with NewsVoiceAI


In at this time’s fast-paced world, staying knowledgeable is essential, however discovering the time to learn prolonged information articles might be difficult. Moreover, consuming information in a single’s native language enhances understanding and engagement. Enter NewsVoiceAI, an revolutionary utility designed to revolutionize the way in which we entry information. By harnessing the facility of synthetic intelligence, NewsVoiceAI transforms reside English information articles into concise Punjabi audio summaries, making it simpler than ever to remain up to date whereas on the go.

On this complete article, we’ll delve into the workings of NewsVoiceAI, discover the cutting-edge applied sciences behind it, present a step-by-step information on how one can create an identical utility, and combine the precise code that brings this utility to life.

Studying Outcomes

  • Find out how NewsVoiceAI leverages AI to remodel reside information articles into concise, native-language audio summaries.
  • Perceive the applied sciences behind NewsVoiceAI, together with The Guardian API, OpenAI fashions, and text-to-speech conversion.
  • Achieve hands-on data of integrating APIs and utilizing Streamlit for constructing interactive purposes.
  • Uncover the way to summarize and translate English information articles into Punjabi utilizing AI-driven fashions.
  • Discover the method of constructing a multilingual information summarizer with real-time information fetching and audio output.

This text was printed as part of the Information Science Blogathon.

What’s NewsVoice?

Think about catching up on the newest information throughout your morning commute with out having to learn via prolonged articles or battle with language obstacles. NewsVoice was born out of the need to make information extra accessible, particularly for Punjabi-speaking audiences preferring consuming content material of their native language. The appliance bridges a number of gaps:

  • Time Constraints: Gives concise summaries, saving customers time.
  • Language Accessibility: Interprets content material into Punjabi for native audio system.
  • Ease of Consumption: Converts textual content to speech, permitting for hands-free listening.

Whether or not you’re a busy skilled, a commuter, or somebody with visible impairments, NewsVoiceAI gives a seamless solution to keep knowledgeable.

Key Options of NewsVoice

  • Reside Information Fetching: Pulls the newest information articles from respected sources like The Guardian utilizing their API.
  • AI-Powered Summarization: Makes use of superior AI fashions from OpenAI to condense prolonged articles into transient summaries.
  • Punjabi Translation: Ensures regional accessibility by precisely translating summaries into Punjabi.
  • Textual content-to-Speech Conversion: Generates clear and natural-sounding Punjabi audio utilizing state-of-the-art TTS applied sciences.
  • Person-Pleasant Interface: Constructed with Streamlit, providing an interactive and intuitive consumer expertise.

How NewsVoice Works?

Let’s discover the step-by-step workflow of NewsVoice, integrating code snippets as an instance every half.

Step1: Fetch Information Articles

Utilizing The Guardian API, NewsVoiceAI retrieves the highest reside information articles primarily based on predefined standards or consumer preferences.

Implementation

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

Rationalization:

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

Step2: Fetch Article Content material

For every article, we fetch the complete content material to organize it for summarization.

Implementation

# Operate to fetch article content material
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()
        physique = article_data.get("response", {}).get("content material", {}).get("blocks", {}).get("physique", [])
        if physique:
            return " ".be a part of(block.get("bodyTextSummary", "") for block in physique)
    return None

Rationalization:

  • The perform sends a GET request to the article’s API URL with parameters to retrieve the complete content material.
  • It parses the JSON response to extract the textual content of the article.

Step3: Summarize and Translate Content material Utilizing OpenAI API

We use OpenAI’s GPT fashions to summarize the article after which translate the abstract into Punjabi.

Implementation

# Operate to summarize and translate content material utilizing OpenAI API
def summarize_and_translate(content material, api_key):
    import openai
    openai.api_key = api_key
    
    # Summarization Immediate
    summary_response = openai.ChatCompletion.create(
        mannequin="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:nn{content}"}
        ],
        max_tokens=100
    )
    abstract = summary_response.selections[0].message.content material.strip()
    
    # Translation Immediate
    translation_response = openai.ChatCompletion.create(
        mannequin="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:nn{summary}"}
        ],
        max_tokens=150
    )
    translation = translation_response.selections[0].message.content material.strip()
    
    return abstract, translation

Rationalization:

  • We initialize the OpenAI API with the supplied key.
  • The summarize_and_translate perform first summarizes the article content material.
  • It then interprets the abstract into Punjabi.
  • Each operations use the ChatCompletion endpoint with applicable prompts.

Step4: Convert Punjabi Textual content to Speech Utilizing Sarvam TTS API

The translated textual content is transformed into audio utilizing the Sarvam TTS API.

Implementation

# Operate to transform Punjabi textual content to speech utilizing Sarvam TTS API
def punjabi_text_to_speech(punjabi_text, sarvam_api_key):
    url = "https://api.sarvam.ai/text-to-speech"
    
    # Cut up textual content into chunks of as much as 500 characters
    chunks = [punjabi_text[i:i+500] for i in vary(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,
            "tempo": 1.0,
            "loudness": 1.2,
            "speech_sample_rate": 8000,
            "enable_preprocessing": True,
            "mannequin": "bulbul:v1"
        }
        headers = {
            "Content material-Kind": "utility/json",
            "API-Subscription-Key": sarvam_api_key
        }
    
        response = requests.put up(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 present in response for chunk {i+1}.")
                return None
        else:
            st.error(f"Did not convert chunk {i+1} to speech: {response.status_code} - {response.textual content}")
            return None
    
    # Mix all Base64 audio chunks right into a single string
    return "".be a part of(audio_clips)

Rationalization:

  • The perform handles the API’s character restrict by splitting the textual content into chunks.
  • It sends every chunk to the TTS API and collects the Base64-encoded audio.
  • The audio chunks are concatenated to kind the whole audio file.

Step5: Construct the Streamlit Interface

The consumer interface is constructed utilizing Streamlit, offering an interactive expertise.

Implementation

# Most important Streamlit App
def essential():
    st.set_page_config(
        page_title="NewsVoice: Multilingual Information Summaries",
        page_icon="📰",
        structure="broad"
    )
    
    # Customized CSS for improved styling
    st.markdown("""
    
    """, unsafe_allow_html=True)
    
    # App Title
    st.markdown('', unsafe_allow_html=True)
    
    # Sidebar for configuration
    st.sidebar.header("Information Configuration")
    num_articles = st.sidebar.slider("Variety of Articles", 1, 5, 3)
    
    # Most important Content material Space
    st.markdown('', unsafe_allow_html=True)
    
    # Fetch and Course of Button
    if st.button("Fetch & Translate Information"):
        with st.spinner("Fetching and processing information..."):
            # Fetch information articles
            articles = fetch_news(GUARDIAN_API_KEY)
    
            if not articles:
                st.warning("No articles discovered. Please strive once more.")
            else:
                # Course of prime chosen variety of articles
                for article in articles[:num_articles]:
                    english_title = article.get('webTitle', 'No title out there')
                    article_url = article.get('apiUrl')
    
                    # Create a container for every article
                    with st.container():
                        st.subheader(english_title)
    
                        # Fetch article content material
                        article_content = fetch_article_content(article_url, GUARDIAN_API_KEY) if article_url else None
                        
                        if not article_content:
                            st.write("No content material out there for this text.")
                            proceed
    
                        strive:
                            # Summarize and Translate
                            abstract, punjabi_translation = summarize_and_translate(article_content, OPENAI_API_KEY)
                            
                            # Show English Abstract
                            st.markdown("**English Abstract:**")
                            st.write(abstract)
    
                            # Show Punjabi Translation
                            st.markdown("**Punjabi Translation:**")
                            st.write(punjabi_translation)
    
                            # Textual content-to-Speech
                            st.write("Producing 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("---")
    
                        besides 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__":
    essential()

Rationalization:

  • The principle perform units up the Streamlit app, together with the web page configuration and styling.
  • The sidebar permits customers to pick the variety of articles.
  • The principle space shows the fetched information, summaries, translations, and audio.
  • Error dealing with ensures the app informs the consumer of any points throughout processing.
Step5: Build the Streamlit Interface: NewVoice AI

Take a look at the Audio high quality within the recording uploaded within the README of this repository – NewsVoiceAI

How one can Get hold of the APIs?

To efficiently construct and run NewsVoice, you’ll must get hold of API keys from the next companies:

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

Under are step-by-step guides on the way to purchase these API keys.

Guardian Open Platform API

The Guardian offers a free API that enables builders to entry content material from their huge repository of reports articles.

Steps to Get hold of The Guardian API Key:

  • Go to The Guardian Open Platform.
  • Scroll down and search for ‘Free immediate entry for builders’.
  • Click on on ‘Register for a developer key’.
  • Fill out the shape and Conform to the Phrases and Circumstances.
  • Submit Your Utility.
  • Retailer Your API Key Securely.

OpenAI API

OpenAI gives highly effective language fashions like GPT-4o and others, that are used for textual content summarization and translation in NewsVoiceAI.

Steps to Get hold of OpenAI API Key

  • Create an OpenAI Account.
  • Confirm Your E mail and Cellphone Quantity.
  • Entry the API Dashboard
  • Generate a New API Key.
  • Retailer Your API Key Securely.

Vital Notes

Billing Info:

  • OpenAI’s APIs are paid companies. You’ll must arrange a cost technique below the
  • New customers could obtain free credit that expire after a sure interval.

Sarvam TTS API

Sarvam AI offers text-to-speech companies supporting a number of Indian languages, together with Punjabi with about ₹ 1,000 of free credit. Make good use of it!

Steps to Get hold of Sarvam TTS API Key:

  • Go to Sarvam AI‘s Web site.
  • Signal Up for an Account and Verify Your E mail.
  • Entry the Developer Dashboard.
  • Generate an API Key.
  • Retailer Your API Key Securely.

Applied sciences Behind NewsVoice

Allow us to now talk about the applied sciences behind NewsVoice beneath:

The Guardian API

The Guardian API offers entry to a wealth of reside information articles and metadata. By integrating this API, NewsVoice ensures that customers obtain essentially the most present and related information content material.

  • Customizable Queries: Fetch articles primarily based on sections, tags, or search phrases.
  • Wealthy Metadata: Entry article headlines, authors, publication dates, and extra.
  • Reliability: A trusted supply of reports with international protection.

OpenAI’s GPT Fashions

NewsVoiceAI leverages OpenAI’s highly effective multilingual language fashions to deal with each summarization and translation duties.

Summarization

  • Mannequin Used: Both gpt-4o’s November 2024 launch or gpt-4o-mini, relying on the specified stability between velocity and accuracy.
  • Performance: Converts prolonged information articles into concise summaries, capturing the essence of the content material.
  • Advantages: Reduces studying time whereas preserving key data.

Translation

  • Mannequin Used: The identical OpenAI fashions are employed for translation duties.
  • Performance: Interprets English summaries into Punjabi with excessive linguistic accuracy.
  • Advantages: Allows native Punjabi audio system to devour content material comfortably.

Textual content-to-Speech (TTS) Conversion

To transform translated textual content into speech, NewsVoiceAI makes use of superior TTS APIs that help Punjabi.

  • API Used: Sarvam TTS API or some other supplier that helps Punjabi.
  • Options:
    • Pure-Sounding Voice: Gives a nice listening expertise.
    • Giant Textual content Dealing with: Able to processing prolonged texts by splitting them into manageable chunks.
  • Advantages: Makes information accessible to those that want auditory studying or have visible impairments.

Streamlit

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

  • Options:
    • Simple to Use: Simplifies the method of turning Python scripts into internet apps.
    • Interactive Widgets: Permits customers to work together with the app seamlessly.
    • Speedy Improvement: Allows fast prototyping and deployment.
  • Advantages: Gives a user-friendly platform for customers to fetch, course of, and take heed to information effortlessly.

Conclusion

NewsVoiceAI represents a big step towards making information extra accessible and tailor-made to particular person wants. By integrating superior AI applied sciences from OpenAI and leveraging dependable information sources like The Guardian, the app transforms the way in which we devour information:

  • Accessibility: Breaks down language obstacles and caters to these with visible impairments.
  • Effectivity: Saves time by offering concise summaries.
  • Comfort: Permits for hands-free consumption via audio playback.

As expertise continues to evolve, purposes like NewsVoice will play an important position in democratizing entry to data.

Strive it at this time: NewsVoice GitHub Repository

Acknowledgments

  • The Guardian API: For offering reside information content material.
  • OpenAI: For the highly effective language fashions utilized in summarization and translation.
  • Sarvam TTS API: For enabling text-to-speech conversion in Punjabi.
  • Streamlit Group: For the open-source framework that powers the app’s interface.

Extra Assets

Key Takeaways

  • NewsVoiceAI revolutionizes information consumption by offering concise Punjabi audio summaries of reside English information articles.
  • The appliance makes use of AI-powered summarization, translation, and text-to-speech applied sciences to make information accessible in real-time.
  • NewsVoiceAI helps time-saving, language accessibility, and hands-free listening for customers on the go.
  • The system integrates The Guardian API, OpenAI’s GPT fashions, Sarvam TTS, and Streamlit for a seamless consumer expertise.
  • NewsVoiceAI is right for busy professionals, commuters, and people with visible impairments who want quick access to present information.

Often Requested Questions

Q1. What’s NewsVoiceAI and the way does it profit me?

A. NewsVoiceAI is an AI-powered utility that transforms reside English information articles into concise Punjabi audio summaries. It advantages customers by saving time, breaking language obstacles, and offering an accessible, hands-free solution to devour information.

Q2. Do I would like any technical experience to make use of NewsVoice?

A. No technical experience is required to make use of the applying as an end-user. You possibly can merely work together with the user-friendly interface to fetch, learn, and take heed to information summaries. Nevertheless, if you happen to want to construct or customise the applying, fundamental data of Python and API integration could be useful.

Q3. Is NewsVoiceAI free to make use of?

A. The NewsVoiceAI utility itself is open-source and free to make use of. Nevertheless, accessing sure APIs like OpenAI and Sarvam TTS could contain prices, particularly past their free utilization tiers. It’s essential to evaluate the pricing particulars of every API service you intend to make use of. Though, if you happen to plan on utilizing it regionally chances are you’ll use open supply LLM’s utilizing Ollama.

This autumn. How correct are the translations and summaries?

A. NewsVoiceAI makes use of superior AI fashions from OpenAI, recognized for his or her excessive accuracy in language duties. Whereas the translations and summaries are typically dependable, they is probably not excellent because of the nuances of language processing. Suggestions is welcome to assist enhance future variations.

Q5. Can I customise the variety of articles or choose particular information subjects?

A. Sure, the applying lets you choose the variety of articles to fetch via the sidebar slider. At the moment, matter choice isn’t out there, however future enhancements could embody the flexibility to filter information by classes or key phrases.

The media proven on this article will not be owned by Analytics Vidhya and is used on the Writer’s discretion.

Hello! I am Adarsh, a Enterprise Analytics graduate from ISB, at the moment deep into analysis and exploring new frontiers. I am tremendous keen about information science, AI, and all of the revolutionary methods they will rework industries. Whether or not it is constructing fashions, engaged on information pipelines, or diving into machine studying, I like experimenting with the newest tech. AI is not simply my curiosity, it is the place I see the long run heading, and I am all the time excited to be part of that journey!

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles