Manishkumaryadav commited on
Commit
ce8edae
Β·
verified Β·
1 Parent(s): 19d3e82

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +76 -66
api.py CHANGED
@@ -1,66 +1,76 @@
1
- from flask import Flask, request, jsonify
2
- import os
3
- from sentiment_analysis import perform_sentiment_analysis, comparative_analysis
4
- from tts_hindi import generate_hindi_coqui_tts
5
- import pandas as pd
6
-
7
- app = Flask(__name__)
8
-
9
- @app.route('/analyze', methods=['POST'])
10
- def analyze():
11
- """Perform news sentiment analysis and TTS."""
12
- try:
13
- company_name = request.json.get('company_name')
14
-
15
- if not company_name:
16
- return jsonify({"error": "Company name is required"}), 400
17
-
18
- # CSV file with extracted articles
19
- csv_file = f"company_news/{company_name}_news.csv"
20
-
21
- if not os.path.exists(csv_file):
22
- return jsonify({"error": f"No data found for {company_name}"}), 404
23
-
24
- # Perform sentiment analysis
25
- sentiment_df = perform_sentiment_analysis(csv_file)
26
- sentiment_summary = comparative_analysis(sentiment_df)
27
-
28
- # βœ… Generate Hindi TTS audio
29
- summary_text = ". ".join(sentiment_df['summary'].tolist())
30
- audio_file = generate_hindi_coqui_tts(summary_text, company_name)
31
-
32
- # Extract article details
33
- articles = sentiment_df[['title', 'summary', 'url']].to_dict(orient='records')
34
-
35
- return jsonify({
36
- "company": company_name,
37
- "sentiment_summary": sentiment_summary,
38
- "articles": articles,
39
- "audio_file": audio_file
40
- })
41
-
42
- except Exception as e:
43
- print(f"API Error: {e}")
44
- return jsonify({"error": "Internal server error"}), 500
45
- @app.route('/generate-tts', methods=['POST'])
46
- def generate_tts_api():
47
- data = request.get_json()
48
-
49
- text = data.get('text')
50
- company_name = data.get('company_name', 'default_company')
51
-
52
- if not text:
53
- return jsonify({"error": "Text is required"}), 400
54
-
55
- audio_file = generate_hindi_coqui_tts(text, company_name)
56
-
57
- if audio_file and os.path.exists(audio_file):
58
- return jsonify({
59
- "message": "βœ… TTS generated successfully",
60
- "audio_file": audio_file
61
- })
62
- else:
63
- return jsonify({"error": "Failed to generate TTS"}), 500
64
-
65
- if __name__ == '__main__':
66
- app.run(debug=True)
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ import os
3
+ from sentiment_analysis import perform_sentiment_analysis, comparative_analysis
4
+ from tts_hindi import generate_hindi_coqui_tts
5
+ import pandas as pd
6
+ import zipfile
7
+
8
+ # Extract the ZIP at runtime
9
+ zip_file = "company_news.zip"
10
+ extract_folder = "company_news"
11
+
12
+ if os.path.exists(zip_file) and not os.path.exists(extract_folder):
13
+ with zipfile.ZipFile(zip_file, 'r') as zip_ref:
14
+ zip_ref.extractall(extract_folder)
15
+ print(f"βœ… Extracted {zip_file} to {extract_folder}")
16
+
17
+ app = Flask(__name__)
18
+
19
+ @app.route('/analyze', methods=['POST'])
20
+ def analyze():
21
+ """Perform news sentiment analysis and TTS."""
22
+ try:
23
+ company_name = request.json.get('company_name')
24
+
25
+ if not company_name:
26
+ return jsonify({"error": "Company name is required"}), 400
27
+
28
+ # CSV file with extracted articles
29
+ csv_file = f"company_news/{company_name}_news.csv"
30
+
31
+ if not os.path.exists(csv_file):
32
+ return jsonify({"error": f"No data found for {company_name}"}), 404
33
+
34
+ # Perform sentiment analysis
35
+ sentiment_df = perform_sentiment_analysis(csv_file)
36
+ sentiment_summary = comparative_analysis(sentiment_df)
37
+
38
+ # βœ… Generate Hindi TTS audio
39
+ summary_text = ". ".join(sentiment_df['summary'].tolist())
40
+ audio_file = generate_hindi_coqui_tts(summary_text, company_name)
41
+
42
+ # Extract article details
43
+ articles = sentiment_df[['title', 'summary', 'url']].to_dict(orient='records')
44
+
45
+ return jsonify({
46
+ "company": company_name,
47
+ "sentiment_summary": sentiment_summary,
48
+ "articles": articles,
49
+ "audio_file": audio_file
50
+ })
51
+
52
+ except Exception as e:
53
+ print(f"API Error: {e}")
54
+ return jsonify({"error": "Internal server error"}), 500
55
+ @app.route('/generate-tts', methods=['POST'])
56
+ def generate_tts_api():
57
+ data = request.get_json()
58
+
59
+ text = data.get('text')
60
+ company_name = data.get('company_name', 'default_company')
61
+
62
+ if not text:
63
+ return jsonify({"error": "Text is required"}), 400
64
+
65
+ audio_file = generate_hindi_coqui_tts(text, company_name)
66
+
67
+ if audio_file and os.path.exists(audio_file):
68
+ return jsonify({
69
+ "message": "βœ… TTS generated successfully",
70
+ "audio_file": audio_file
71
+ })
72
+ else:
73
+ return jsonify({"error": "Failed to generate TTS"}), 500
74
+
75
+ if __name__ == '__main__':
76
+ app.run(debug=True)