Spaces:
Running
Running
| import os | |
| import uuid | |
| import threading | |
| from dotenv import load_dotenv | |
| from flask import Flask, request, jsonify, render_template, send_from_directory | |
| from werkzeug.utils import secure_filename | |
| from engine import ( | |
| init_db, create_task, get_task, run_ai_engine_worker, | |
| generate_script_with_ai, UPLOAD_FOLDER, OUTPUT_FOLDER | |
| ) | |
| load_dotenv() | |
| app = Flask(__name__) | |
| app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER | |
| app.config['OUTPUT_FOLDER'] = OUTPUT_FOLDER | |
| init_db() | |
| # ============================================================================== | |
| # PWA, Static File, and Favicon Routes | |
| # ============================================================================== | |
| # Favicon ka naya aur sahi route | |
| def favicon(): | |
| # Hum static folder se favicon.png file bhej rahe hain | |
| # Iske liye static folder me 'favicon.png' naam ki file honi chahiye | |
| return send_from_directory('static', 'favicon.png') | |
| def serve_manifest(): | |
| return send_from_directory('static', 'manifest.json') | |
| def serve_sw(): | |
| return send_from_directory('static', 'sw.js') | |
| def serve_icon_512(): | |
| return send_from_directory('static', 'icon-512x512.png') | |
| def serve_icon_192(): | |
| return send_from_directory('static', 'icon-192x192.png') | |
| # ============================================================================== | |
| # Main Application Routes | |
| # ============================================================================== | |
| def home(): | |
| return render_template('home.html') | |
| def process_video(): | |
| task_id = str(uuid.uuid4()) | |
| create_task(task_id) | |
| script_text = request.form.get('script_text') | |
| script_file = request.files.get('script_file') | |
| orientation = request.form.get('orientation', 'horizontal') | |
| max_clip_length = int(request.form.get('max_clip_length', 15)) | |
| mute_final_video = request.form.get('mute_final_video') == 'true' | |
| script_file_path = None | |
| if script_file and script_file.filename: | |
| filename = secure_filename(script_file.filename) | |
| task_upload_dir = os.path.join(app.config['UPLOAD_FOLDER'], task_id) | |
| os.makedirs(task_upload_dir, exist_ok=True) | |
| script_file_path = os.path.join(task_upload_dir, filename) | |
| script_file.save(script_file_path) | |
| if not script_text and not script_file_path: | |
| return "Please provide a script text or an audio file.", 400 | |
| thread = threading.Thread( | |
| target=run_ai_engine_worker, | |
| args=(task_id, script_text, script_file_path, orientation, max_clip_length, mute_final_video) | |
| ) | |
| thread.start() | |
| return render_template('processing.html', task_id=task_id) | |
| def generate_script(): | |
| data = request.get_json() | |
| topic = data.get('topic') | |
| video_length = data.get('video_length') | |
| if not topic or not video_length: | |
| return jsonify({'error': 'Topic and video length are required.'}), 400 | |
| try: | |
| generated_script = generate_script_with_ai(topic, video_length) | |
| return jsonify({'script': generated_script}) | |
| except Exception as e: | |
| print(f"Error during script generation: {e}") | |
| return jsonify({'error': f"AI से संपर्क करने में विफल: {str(e)}"}), 500 | |
| def progress(task_id): | |
| task = get_task(task_id) | |
| if not task: | |
| return jsonify({'status': 'error', 'log': 'Task not found.'}) | |
| return jsonify(dict(task)) | |
| def result(filename): | |
| return render_template('result.html', filename=filename) | |
| def serve_output_file(filename): | |
| return send_from_directory(app.config['OUTPUT_FOLDER'], filename) | |
| def history(): | |
| output_dir = app.config['OUTPUT_FOLDER'] | |
| history_items = [] | |
| try: | |
| all_files = sorted(os.listdir(output_dir), reverse=True) | |
| for filename in all_files: | |
| if filename.endswith('_final_video.mp4'): | |
| task_id = filename.replace('_final_video.mp4', '') | |
| report_filename = f"{task_id}_report.json" | |
| item = { | |
| 'video': filename, | |
| 'task_id': task_id, | |
| 'report': report_filename if report_filename in all_files else None | |
| } | |
| history_items.append(item) | |
| except FileNotFoundError: | |
| print(f"'{output_dir}' directory not found.") | |
| return render_template('history.html', items=history_items) | |
| if __name__ == '__main__': | |
| app.run(host='0.0.0.0', port=5000, debug=True) | |