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.
This article was published as a part of the Data Science Blogathon.
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:
Whether you’re a busy professional, a commuter, or someone with visual impairments, NewsVoiceAI offers a seamless way to stay informed.
Let’s explore the step-by-step workflow of NewsVoice, integrating code snippets to illustrate each part.
Using The Guardian API, NewsVoiceAI retrieves the top live news articles based on predefined criteria or user preferences.
# 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:
For each article, we fetch the full content to prepare it for summarization.
# 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:
We use OpenAI’s GPT models to summarize the article and then translate the summary into Punjabi.
# 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:
The translated text is converted into audio using the Sarvam TTS API.
# 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 user interface is built using Streamlit, providing an interactive experience.
# 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:
Check out the Audio quality in the recording uploaded in the README of this repository – NewsVoiceAI
To successfully build and run NewsVoice, you’ll need to obtain API keys from the following services:
Below are step-by-step guides on how to acquire these API keys.
The Guardian provides a free API that allows developers to access content from their vast repository of news articles.
OpenAI offers powerful language models like GPT-4o and others, which are used for text summarization and translation in NewsVoiceAI.
Billing Information:
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!
Let us now discuss the technologies behind NewsVoice below:
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.
NewsVoiceAI leverages OpenAI’s powerful multilingual language models to handle both summarization and translation tasks.
To convert translated text into speech, NewsVoiceAI utilizes advanced TTS APIs that support Punjabi.
Streamlit is an open-source app framework used to create the interactive web interface for NewsVoice.
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:
As technology continues to evolve, applications like NewsVoice will play a crucial role in democratizing access to information.
Try it today: NewsVoice GitHub Repository
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.
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.
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.
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.
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.