File size: 5,873 Bytes
47af8ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"""

Healthcheck script to verify the functionality of all components of the application.

Run this script to check if all dependencies are correctly installed and working.

"""

import sys
import os
import time
import traceback

def run_checks():
    print("Starting health check for News Summarization and TTS Application...")
    checks_passed = 0
    checks_failed = 0
    
    # Check 1: Verify imports
    print("\n1. Checking imports...")
    try:
        # Standard libraries
        import json
        import re
        
        # Web and API dependencies
        import requests
        import fastapi
        import uvicorn
        import streamlit
        
        # Data processing
        import pandas
        import numpy
        import bs4
        
        # NLP
        import nltk
        import networkx
        from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
        
        # ML and Transformers
        import torch
        import transformers
        from transformers import pipeline
        
        # TTS and Translation
        import deep_translator
        from deep_translator import GoogleTranslator
        import gtts
        import pyttsx3
        
        print("βœ… All imports successful.")
        checks_passed += 1
    except ImportError as e:
        print(f"❌ Import error: {str(e)}")
        print(f"Traceback: {traceback.format_exc()}")
        checks_failed += 1
    
    # Check 2: Verify NLTK data
    print("\n2. Checking NLTK data...")
    try:
        import nltk
        nltk.data.find('tokenizers/punkt')
        nltk.data.find('corpora/stopwords')
        print("βœ… NLTK data verified.")
        checks_passed += 1
    except LookupError as e:
        print(f"❌ NLTK data error: {str(e)}")
        print("Trying to download necessary NLTK data...")
        try:
            nltk.download('punkt')
            nltk.download('stopwords')
            print("βœ… NLTK data downloaded successfully.")
            checks_passed += 1
        except Exception as e:
            print(f"❌ Failed to download NLTK data: {str(e)}")
            checks_failed += 1
    
    # Check 3: Test translation
    print("\n3. Testing translation service...")
    try:
        from deep_translator import GoogleTranslator
        translator = GoogleTranslator(source='en', target='hi')
        text = "Hello, this is a test."
        translated = translator.translate(text)
        print(f"Original text: {text}")
        print(f"Translated text: {translated}")
        if translated and len(translated) > 0:
            print("βœ… Translation service working.")
            checks_passed += 1
        else:
            print("❌ Translation returned empty result.")
            checks_failed += 1
    except Exception as e:
        print(f"❌ Translation error: {str(e)}")
        print(f"Traceback: {traceback.format_exc()}")
        checks_failed += 1
    
    # Check 4: Test TTS
    print("\n4. Testing Text-to-Speech service...")
    try:
        from gtts import gTTS
        test_text = "ΰ€ͺΰ€°ΰ₯€ΰ€•ΰ₯ΰ€·ΰ€£ ΰ€ͺΰ€Ύΰ€ "  # "Test text" in Hindi
        test_file = 'test_audio.mp3'
        
        # Try gTTS
        tts = gTTS(text=test_text, lang='hi', slow=False)
        tts.save(test_file)
        
        if os.path.exists(test_file) and os.path.getsize(test_file) > 0:
            print("βœ… gTTS service working.")
            # Clean up test file
            try:
                os.remove(test_file)
            except:
                pass
            checks_passed += 1
        else:
            print("❌ gTTS failed to generate a valid audio file.")
            checks_failed += 1
    except Exception as e:
        print(f"❌ Text-to-Speech error: {str(e)}")
        print(f"Traceback: {traceback.format_exc()}")
        checks_failed += 1
    
    # Check 5: Test sentiment analysis
    print("\n5. Testing sentiment analysis...")
    try:
        from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
        analyzer = SentimentIntensityAnalyzer()
        test_text = "This product is excellent and I love it!"
        scores = analyzer.polarity_scores(test_text)
        print(f"Sentiment scores for '{test_text}': {scores}")
        if 'compound' in scores:
            print("βœ… Sentiment analysis working.")
            checks_passed += 1
        else:
            print("❌ Sentiment analysis returned unexpected result.")
            checks_failed += 1
    except Exception as e:
        print(f"❌ Sentiment analysis error: {str(e)}")
        print(f"Traceback: {traceback.format_exc()}")
        checks_failed += 1
    
    # Check 6: Test Transformers
    print("\n6. Testing Transformer models...")
    try:
        from transformers import pipeline
        sentiment_task = pipeline("sentiment-analysis", return_all_scores=False)
        result = sentiment_task("I love using this application!")
        print(f"Transformer sentiment analysis result: {result}")
        print("βœ… Transformer models working.")
        checks_passed += 1
    except Exception as e:
        print(f"❌ Transformer models error: {str(e)}")
        print(f"Traceback: {traceback.format_exc()}")
        checks_failed += 1
    
    # Summary
    print("\n" + "="*50)
    print(f"Health Check Summary: {checks_passed} checks passed, {checks_failed} checks failed")
    
    if checks_failed == 0:
        print("\nβœ… All systems operational! The application should run correctly.")
        return True
    else:
        print("\n❌ Some checks failed. Please review the errors above.")
        return False

if __name__ == "__main__":
    success = run_checks()
    if not success:
        sys.exit(1)