Spaces:
Sleeping
Sleeping
Update api.py
Browse files
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 |
-
|
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 |
-
return jsonify({"error": "
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|