from flask import Flask, request, jsonify, send_file import requests from bs4 import BeautifulSoup import trafilatura import json import os import tempfile from io import BytesIO from gtts import gTTS from utils import ( search_news_articles, extract_article_content, perform_sentiment_analysis, extract_topics, generate_comparative_analysis, summarize_sentiment ) app = Flask(__name__) @app.route('/news/', methods=['GET']) def get_company_news(company_name): """ Fetch news articles about a specific company """ try: # Search for news articles articles = search_news_articles(company_name) if not articles or len(articles) == 0: return jsonify({"error": "No articles found"}), 404 # Process articles to extract content processed_articles = [] for article in articles[:10]: # Limit to 10 articles article_data = extract_article_content(article) if article_data: processed_articles.append(article_data) return jsonify({"articles": processed_articles}) except Exception as e: return jsonify({"error": str(e)}), 500 @app.route('/analyze', methods=['POST']) def analyze_content(): """ Perform sentiment analysis and comparative analysis on articles """ try: data = request.json if not data or 'company' not in data or 'articles' not in data: return jsonify({"error": "Invalid request data"}), 400 company_name = data['company'] articles = data['articles'] if len(articles) == 0: return jsonify({"error": "No articles provided for analysis"}), 400 # Perform sentiment analysis on each article for article in articles: if 'Summary' in article: sentiment = perform_sentiment_analysis(article['Summary']) article['Sentiment'] = sentiment # Extract topics article['Topics'] = extract_topics(article['Summary']) # Generate comparative analysis comparative_analysis = generate_comparative_analysis(articles) # Generate final sentiment summary final_summary = summarize_sentiment(company_name, articles, comparative_analysis) # Construct response response = { "Company": company_name, "Articles": articles, "Comparative Sentiment Score": comparative_analysis, "Final Sentiment Analysis": final_summary } return jsonify(response) except Exception as e: return jsonify({"error": str(e)}), 500 @app.route('/tts', methods=['POST']) def text_to_speech(): """ Convert text to speech in multiple languages Supported languages: Hindi, English, Spanish, French, German, Japanese, Chinese, Russian, Arabic, Italian """ try: data = request.json if not data or 'text' not in data: return jsonify({"error": "No text provided"}), 400 text = data['text'] # Default to Hindi if no language specified language = data.get('language', 'hi') # Map of supported languages language_map = { 'hi': 'Hindi', 'en': 'English', 'es': 'Spanish', 'fr': 'French', 'de': 'German', 'ja': 'Japanese', 'zh-CN': 'Chinese', 'ru': 'Russian', 'ar': 'Arabic', 'it': 'Italian' } # Validate language if language not in language_map: return jsonify({ "error": f"Unsupported language code: {language}", "supported_languages": language_map }), 400 # Create a temporary file to store the audio temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') # Generate TTS in the specified language tts = gTTS(text=text, lang=language, slow=False) tts.save(temp_file.name) # Send the audio file return send_file( temp_file.name, mimetype='audio/mp3', as_attachment=True, download_name=f'speech_{language}.mp3' ) except Exception as e: return jsonify({"error": str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=True)