Spaces:
Sleeping
Sleeping
Update api.py
Browse files
api.py
CHANGED
@@ -1,20 +1,23 @@
|
|
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)
|
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():
|
@@ -31,7 +34,7 @@ def analyze():
|
|
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 |
|
@@ -39,7 +42,7 @@ def analyze():
|
|
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({
|
@@ -51,7 +54,8 @@ def analyze():
|
|
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()
|
@@ -73,4 +77,5 @@ def generate_tts_api():
|
|
73 |
return jsonify({"error": "Failed to generate TTS"}), 500
|
74 |
|
75 |
if __name__ == '__main__':
|
76 |
-
|
|
|
|
1 |
from flask import Flask, request, jsonify
|
2 |
+
from flask_cors import CORS
|
3 |
import os
|
4 |
from sentiment_analysis import perform_sentiment_analysis, comparative_analysis
|
5 |
from tts_hindi import generate_hindi_coqui_tts
|
6 |
import pandas as pd
|
7 |
import zipfile
|
8 |
+
from waitress import serve
|
9 |
|
10 |
+
# β
Extract the ZIP at runtime (always extract)
|
11 |
zip_file = "company_news.zip"
|
12 |
extract_folder = "company_news"
|
13 |
|
14 |
+
if os.path.exists(zip_file):
|
15 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
16 |
zip_ref.extractall(extract_folder)
|
17 |
print(f"β
Extracted {zip_file} to {extract_folder}")
|
18 |
|
19 |
app = Flask(__name__)
|
20 |
+
CORS(app) # Enable CORS
|
21 |
|
22 |
@app.route('/analyze', methods=['POST'])
|
23 |
def analyze():
|
|
|
34 |
if not os.path.exists(csv_file):
|
35 |
return jsonify({"error": f"No data found for {company_name}"}), 404
|
36 |
|
37 |
+
# β
Perform sentiment analysis
|
38 |
sentiment_df = perform_sentiment_analysis(csv_file)
|
39 |
sentiment_summary = comparative_analysis(sentiment_df)
|
40 |
|
|
|
42 |
summary_text = ". ".join(sentiment_df['summary'].tolist())
|
43 |
audio_file = generate_hindi_coqui_tts(summary_text, company_name)
|
44 |
|
45 |
+
# β
Extract article details
|
46 |
articles = sentiment_df[['title', 'summary', 'url']].to_dict(orient='records')
|
47 |
|
48 |
return jsonify({
|
|
|
54 |
|
55 |
except Exception as e:
|
56 |
print(f"API Error: {e}")
|
57 |
+
return jsonify({"error": f"Internal server error: {str(e)}"}), 500
|
58 |
+
|
59 |
@app.route('/generate-tts', methods=['POST'])
|
60 |
def generate_tts_api():
|
61 |
data = request.get_json()
|
|
|
77 |
return jsonify({"error": "Failed to generate TTS"}), 500
|
78 |
|
79 |
if __name__ == '__main__':
|
80 |
+
print("π Running production server...")
|
81 |
+
serve(app, host="0.0.0.0", port=7860)
|