|
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/<company_name>', methods=['GET']) |
|
def get_company_news(company_name): |
|
""" |
|
Fetch news articles about a specific company |
|
""" |
|
try: |
|
|
|
articles = search_news_articles(company_name) |
|
|
|
if not articles or len(articles) == 0: |
|
return jsonify({"error": "No articles found"}), 404 |
|
|
|
|
|
processed_articles = [] |
|
|
|
for article in articles[:10]: |
|
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 |
|
|
|
|
|
for article in articles: |
|
if 'Summary' in article: |
|
sentiment = perform_sentiment_analysis(article['Summary']) |
|
article['Sentiment'] = sentiment |
|
|
|
|
|
article['Topics'] = extract_topics(article['Summary']) |
|
|
|
|
|
comparative_analysis = generate_comparative_analysis(articles) |
|
|
|
|
|
final_summary = summarize_sentiment(company_name, articles, comparative_analysis) |
|
|
|
|
|
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'] |
|
|
|
language = data.get('language', 'hi') |
|
|
|
|
|
language_map = { |
|
'hi': 'Hindi', |
|
'en': 'English', |
|
'es': 'Spanish', |
|
'fr': 'French', |
|
'de': 'German', |
|
'ja': 'Japanese', |
|
'zh-CN': 'Chinese', |
|
'ru': 'Russian', |
|
'ar': 'Arabic', |
|
'it': 'Italian' |
|
} |
|
|
|
|
|
if language not in language_map: |
|
return jsonify({ |
|
"error": f"Unsupported language code: {language}", |
|
"supported_languages": language_map |
|
}), 400 |
|
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3') |
|
|
|
|
|
tts = gTTS(text=text, lang=language, slow=False) |
|
tts.save(temp_file.name) |
|
|
|
|
|
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) |
|
|