File size: 4,621 Bytes
45a93f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
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:
        # 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=8000, debug=True)