from flask import Flask, render_template, request, jsonify, send_file import csv import os from datetime import datetime app = Flask(__name__) CSV_FILE = '/tmp/data_collection.csv' # Create CSV file if it doesn't exist try: if not os.path.exists(CSV_FILE): with open(CSV_FILE, 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['Human', 'AI', 'Timestamp']) except Exception as e: print(f"Warning: Could not create CSV file: {e}") @app.route('/') def index(): return render_template('index.html') @app.route('/add_data', methods=['POST']) def add_data(): try: data = request.json person1 = data.get('person1', '').strip() person2 = data.get('person2', '').strip() if not person1 or not person2: return jsonify({'success': False, 'message': 'Both fields are required'}), 400 # Ensure CSV file exists if not os.path.exists(CSV_FILE): with open(CSV_FILE, 'w', newline='') as f: writer = csv.writer(f) writer.writerow(['Human', 'AI', 'Timestamp']) # Append to CSV with open(CSV_FILE, 'a', newline='') as f: writer = csv.writer(f) timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S') writer.writerow([person1, person2, timestamp]) return jsonify({'success': True, 'message': 'Data added successfully'}) except Exception as e: return jsonify({'success': False, 'message': str(e)}), 500 @app.route('/verify_password', methods=['POST']) def verify_password(): try: data = request.json password = data.get('password', '') # Convert to int and compare if int(password) == 8767575090: return jsonify({'success': True}) else: return jsonify({'success': False, 'message': 'Incorrect password'}), 401 except ValueError: return jsonify({'success': False, 'message': 'Invalid password format'}), 400 except Exception as e: return jsonify({'success': False, 'message': str(e)}), 500 @app.route('/download_csv') def download_csv(): try: return send_file(CSV_FILE, as_attachment=True, download_name='data_collection.csv') except Exception as e: return jsonify({'success': False, 'message': str(e)}), 500 if __name__ == '__main__': app.run(host='0.0.0.0', port=7860, debug=False)