D
D
import mysql.connector
from mysql.connector import Error
import json
import os
import bcrypt
app = Flask(__name__)
app.secret_key = 'your_secret_key' # Required for session management
# Database configuration
DB_CONFIG = {
'host': 'localhost',
'database': 'p_db', # Updated database name
'user': 'root',
'password': '' # Replace with your MySQL password if set
}
USER_DATA_FILE = 'users.json'
LOGIN_DATA_FILE = 'login.json'
def save_user_data(data):
with open(USER_DATA_FILE, 'w') as file:
json.dump(data, file, indent=4)
def load_login_data():
with open(LOGIN_DATA_FILE, 'r') as file:
return json.load(file)
def save_login_data(data):
with open(LOGIN_DATA_FILE, 'w') as file:
json.dump(data, file, indent=4)
def load_history_data():
with open(HISTORY_FILE, 'r') as file:
return json.load(file)
def save_history_data(data):
with open(HISTORY_FILE, 'w') as file:
json.dump(data, file, indent=4)
def get_db_connection():
try:
connection = mysql.connector.connect(**DB_CONFIG)
return connection
except Error as e:
print(f"Error: {e}")
return None
@app.route('/')
def home():
return render_template('index.html')
# Login and registration routes
@app.route('/create_account', methods=['POST'])
def create_account():
data = request.json
username = data.get('username')
email = data.get('email')
password = data.get('password')
full_name = data.get('full_name')
@app.route('/login', methods=['POST'])
def login():
data = request.get_json()
email = data.get('email')
password = data.get('password')
cursor = connection.cursor(dictionary=True)
username = session.get('username')
if not username:
return jsonify({"error": "User not logged in"}), 403
if request.method == 'GET':
# Fetch all messages for the conversation
cursor.execute("""
SELECT * FROM chat_messages
WHERE conversation_id = %s
ORDER BY id ASC
""", (conversation_id,))
messages = cursor.fetchall()
cursor.close()
connection.close()
return jsonify(messages)
# Simulated AI response
ai_response = f"You said: {user_message}"
cursor.close()
connection.close()