Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify | |
| from flask_cors import CORS | |
| import os | |
| from sentiment_analysis import perform_sentiment_analysis, comparative_analysis | |
| from tts_hindi import generate_hindi_coqui_tts | |
| import pandas as pd | |
| import zipfile | |
| from waitress import serve | |
| # β Extract the ZIP at runtime | |
| zip_file = "company_news.zip" | |
| extract_folder = "company_news" | |
| if os.path.exists(zip_file): | |
| with zipfile.ZipFile(zip_file, 'r') as zip_ref: | |
| zip_ref.extractall(extract_folder) | |
| print(f"β Extracted {zip_file} to {extract_folder}") | |
| app = Flask(__name__) | |
| CORS(app) # Enable CORS | |
| def analyze(): | |
| """Perform news sentiment analysis and TTS.""" | |
| try: | |
| company_name = request.json.get('company_name') | |
| if not company_name: | |
| return jsonify({"error": "Company name is required"}), 400 | |
| csv_file = f"company_news/{company_name}_news.csv" | |
| if not os.path.exists(csv_file): | |
| return jsonify({"error": f"No data found for {company_name}"}), 404 | |
| # β Perform sentiment analysis | |
| sentiment_df = perform_sentiment_analysis(csv_file) | |
| sentiment_summary = comparative_analysis(sentiment_df) | |
| # β Generate Hindi TTS audio | |
| summary_text = ". ".join(sentiment_df['summary'].tolist()) | |
| audio_file = generate_hindi_coqui_tts(summary_text, company_name) | |
| # β Extract article details | |
| articles = sentiment_df[['title', 'summary', 'url']].to_dict(orient='records') | |
| return jsonify({ | |
| "company": company_name, | |
| "sentiment_summary": sentiment_summary, | |
| "articles": articles, | |
| "audio_file": audio_file | |
| }) | |
| except Exception as e: | |
| print(f"API Error: {e}") | |
| return jsonify({"error": f"Internal server error: {str(e)}"}), 500 | |
| def generate_tts_api(): | |
| data = request.get_json() | |
| text = data.get('text') | |
| company_name = data.get('company_name', 'default_company') | |
| if not text: | |
| return jsonify({"error": "Text is required"}), 400 | |
| audio_file = generate_hindi_coqui_tts(text, company_name) | |
| if audio_file and os.path.exists(audio_file): | |
| return jsonify({ | |
| "message": "β TTS generated successfully", | |
| "audio_file": audio_file | |
| }) | |
| else: | |
| return jsonify({"error": "Failed to generate TTS"}), 500 | |
| if __name__ == '__main__': | |
| print("π Running production server...") | |
| serve(app, host="0.0.0.0", port=7870) | |