# Import necessary libraries import requests from bs4 import BeautifulSoup import openai import gradio as gr import os from dotenv import load_dotenv # Load environment variables from .env file load_dotenv() openai.api_key = os.getenv("OPENAI_API_KEY") # Function to scrape content from a URL def scrape_content(url): try: response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') # Extract title and content title = soup.find('title').get_text() paragraphs = soup.find_all('p') content = '\n'.join([para.get_text() for para in paragraphs]) return title, content except Exception as e: return None, f"Error fetching content: {str(e)}" # Function to summarize content in the selected language def summarize_content(content, language): # Define the prompt based on selected language language_prompts = { "Hindi": "in Hindi", "English": "in English", "Telugu": "in Telugu", "Kannada": "in Kannada", "Marathi": "in Marathi", "Bengali": "in Bengali", "Tamil": "in Tamil", "Gujarati": "in Gujarati", "Malayalam": "in Malayalam" } prompt = f"Summarize the following news article {language_prompts[language]} in about 200 words:\n\n{content}\n\n" try: # Generate summary using the OpenAI Chat API response = openai.chat.completions.create( model="gpt-4o", # Use the latest GPT model messages=[ {"role": "system", "content": f"You are a highly skilled news editor and summarization assistant. Your task is to produce clear, concise, and accurate summaries of news articles {language_prompts[language]}. Focus on capturing the key points, context, and implications while preserving the original tone and intent of the article. Ensure the summary is engaging, fact-based, and suitable for readers seeking a quick yet comprehensive understanding of the news."}, {"role": "user", "content": prompt} ], max_tokens=500, # Allows responses of up to ~200 words temperature=0.2 # More deterministic output ) # Extract the summary from the response summary = response.choices[0].message.content.strip() return summary except Exception as e: return f"Error generating summary: {str(e)}" # Function to process a single URL and generate a summary def process_url(url, language): if not url: return "No URL provided." title, content = scrape_content(url) if content.startswith("Error"): return content summary = summarize_content(content, language) return f"Title: {title}\n\nSummary ({language}):\n{summary}" # Gradio interface iface = gr.Interface( fn=process_url, inputs=[ gr.Textbox(lines=2, placeholder="Enter URL here...", label="News Article URL"), gr.Dropdown( ["Hindi","English", "Telugu", "Kannada", "Marathi", "Bengali", "Gujarati","Tamil","Malayalam"], label="Select Language", value="Hindi" ) ], outputs="text", title="Multilingual News Article Summarizer", description="Enter a News Site URL and select a language to generate a 200-word summary." ) # Launch the interface if __name__ == "__main__": iface.launch(share=True)