changes for deploment

parent cc67c7eb
...@@ -8,4 +8,8 @@ RUN pip install --no-cache-dir -r requirements.txt ...@@ -8,4 +8,8 @@ RUN pip install --no-cache-dir -r requirements.txt
COPY . . COPY . .
#just keep the container running without doing anything #just keep the container running without doing anything
CMD ["sh", "-c", "while :; do sleep 10; done"] #CMD ["sh", "-c", "while :; do sleep 10; done"]
# Run apply_test_schema.py, then insert_csv_embeddings.py
CMD ["sh", "-c", "python apply_test_schema.py && python insert_csv_embeddings.py"]
...@@ -48,22 +48,35 @@ BEGIN ...@@ -48,22 +48,35 @@ BEGIN
END $$; END $$;
""" """
conn = psycopg2.connect( def setup_database(drop_existing_tables: bool = False):
"""
Sets up the database schema and tables.
Args:
drop_existing_tables: If True, drops all existing tables before creating them.
"""
try:
conn = psycopg2.connect(
host=os.getenv("POSTGRES_HOST", "localhost"), host=os.getenv("POSTGRES_HOST", "localhost"),
port=os.getenv("POSTGRES_PORT", "5432"), port=os.getenv("POSTGRES_PORT", "5432"),
user=os.getenv("POSTGRES_USER"), user=os.getenv("POSTGRES_USER"),
password=os.getenv("POSTGRES_PASSWORD"), password=os.getenv("POSTGRES_PASSWORD"),
dbname=os.getenv("POSTGRES_DB") dbname=os.getenv("POSTGRES_DB")
) )
conn.autocommit = True conn.autocommit = True
with conn.cursor() as cur: with conn.cursor() as cur:
# Drop all existing tables (uncomment if needed) if drop_existing_tables:
print("Dropping all existing tables...")
cur.execute(drop_all_tables_sql) cur.execute(drop_all_tables_sql)
print("All tables dropped.")
print("Setting up schema and inserting data...")
cur.execute(schema_sql) cur.execute(schema_sql)
print("Database setup complete. Verifying data...")
# Verifications: Select from students and chat_history tables # Verifications: Select from students and chat_history tables
print("Students table rows:") print("\nStudents table rows:")
cur.execute("SELECT * FROM students ORDER BY id;") cur.execute("SELECT * FROM students ORDER BY id;")
students = cur.fetchall() students = cur.fetchall()
for row in students: for row in students:
...@@ -75,4 +88,19 @@ with conn.cursor() as cur: ...@@ -75,4 +88,19 @@ with conn.cursor() as cur:
for row in chat_history: for row in chat_history:
print(row) print(row)
conn.close() except psycopg2.OperationalError as e:
print(f"Database connection failed: {e}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if 'conn' in locals() and conn:
conn.close()
print("Database connection closed.")
if __name__ == "__main__":
# To run with a clean slate, pass True
# setup_database(drop_existing_tables=True)
# To run without dropping tables (default)
setup_database()
\ No newline at end of file
...@@ -21,9 +21,8 @@ def get_db_connection(): ...@@ -21,9 +21,8 @@ def get_db_connection():
register_vector(conn) register_vector(conn)
return conn return conn
def create_schema_and_table(): def create_schema_and_table(conn, drop_existing_table: bool):
create_extension = "CREATE EXTENSION IF NOT EXISTS vector;" create_extension = "CREATE EXTENSION IF NOT EXISTS vector;"
drop_table = "DROP TABLE IF EXISTS educational_chunks;"
create_table = """ create_table = """
CREATE TABLE IF NOT EXISTS educational_chunks ( CREATE TABLE IF NOT EXISTS educational_chunks (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
...@@ -48,12 +47,15 @@ def create_schema_and_table(): ...@@ -48,12 +47,15 @@ def create_schema_and_table():
"CREATE INDEX IF NOT EXISTS idx_grade_is_arabic ON educational_chunks (grade, is_arabic);" "CREATE INDEX IF NOT EXISTS idx_grade_is_arabic ON educational_chunks (grade, is_arabic);"
] ]
conn = get_db_connection()
cur = conn.cursor() cur = conn.cursor()
cur.execute(create_extension) cur.execute(create_extension)
print("CREATE EXTENSION vector operation fine.") print("CREATE EXTENSION vector operation fine.")
if drop_existing_table:
drop_table = "DROP TABLE IF EXISTS educational_chunks;"
cur.execute(drop_table) cur.execute(drop_table)
print("DROP TABLE educational_chunks operation fine.") print("DROP TABLE educational_chunks operation fine.")
cur.execute(create_table) cur.execute(create_table)
print("CREATE TABLE educational_chunks operation fine.") print("CREATE TABLE educational_chunks operation fine.")
for idx_query in create_indexes: for idx_query in create_indexes:
...@@ -61,7 +63,7 @@ def create_schema_and_table(): ...@@ -61,7 +63,7 @@ def create_schema_and_table():
print(f"CREATE INDEX operation fine for: {idx_query}") print(f"CREATE INDEX operation fine for: {idx_query}")
conn.commit() conn.commit()
cur.close() cur.close()
conn.close()
def insert_chunks_from_csv(csv_file: str): def insert_chunks_from_csv(csv_file: str):
df = pd.read_csv(csv_file) df = pd.read_csv(csv_file)
...@@ -125,8 +127,16 @@ def insert_chunks_from_csv(csv_file: str): ...@@ -125,8 +127,16 @@ def insert_chunks_from_csv(csv_file: str):
conn.close() conn.close()
print("All data inserted successfully.") print("All data inserted successfully.")
if __name__ == "__main__": def setup_embeddings_database(drop_existing_tables: bool = False):
create_schema_and_table() """
Sets up the educational chunks table and populates it with embeddings from CSV files.
Args:
drop_existing_tables: If True, drops the existing table before creating it.
"""
try:
conn = get_db_connection()
create_schema_and_table(conn, drop_existing_tables)
csv_files = ["prime4_ar_embeddings.csv", "Prime5_en_chunked_with_embeddings.csv", "prime6_ar_embeddings.csv", "Prime6_en_chunked_with_embeddings.csv"] csv_files = ["prime4_ar_embeddings.csv", "Prime5_en_chunked_with_embeddings.csv", "prime6_ar_embeddings.csv", "Prime6_en_chunked_with_embeddings.csv"]
for file in csv_files: for file in csv_files:
if os.path.exists(file): if os.path.exists(file):
...@@ -134,3 +144,19 @@ if __name__ == "__main__": ...@@ -134,3 +144,19 @@ if __name__ == "__main__":
insert_chunks_from_csv(file) insert_chunks_from_csv(file)
else: else:
print(f"File not found: {file}") print(f"File not found: {file}")
except psycopg2.OperationalError as e:
print(f"Database connection failed: {e}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if 'conn' in locals() and conn:
conn.close()
print("Database connection closed.")
if __name__ == "__main__":
# To run with a clean slate, pass True
# setup_embeddings_database(drop_existing_tables=True)
# To run without dropping the table (default)
setup_embeddings_database()
\ No newline at end of file
...@@ -35,10 +35,8 @@ services: ...@@ -35,10 +35,8 @@ services:
timeout: 20s timeout: 20s
retries: 3 retries: 3
voice-agent: data-handler:
image: salmamohammedhamedmustafa/voice-agent:latest image: salmamohammedhamedmustafa/data-handler:latest
ports:
- "8000:8000"
environment: environment:
MINIO_ENDPOINT: "http://minio:9000" MINIO_ENDPOINT: "http://minio:9000"
MINIO_ACCESS_KEY: "${MINIO_ROOT_USER}" MINIO_ACCESS_KEY: "${MINIO_ROOT_USER}"
...@@ -49,13 +47,19 @@ services: ...@@ -49,13 +47,19 @@ services:
POSTGRES_USER: "${POSTGRES_USER}" POSTGRES_USER: "${POSTGRES_USER}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}" POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
POSTGRES_DB: "${POSTGRES_DB}" POSTGRES_DB: "${POSTGRES_DB}"
volumes:
- ./uploads:/app/uploads
depends_on: depends_on:
- minio - minio
- postgres
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
interval: 10s
retries: 3
timeout: 5s
data-handler: voice-agent:
image: salmamohammedhamedmustafa/data-handler:latest image: salmamohammedhamedmustafa/voice-agent:latest
ports:
- "8000:8000"
environment: environment:
MINIO_ENDPOINT: "http://minio:9000" MINIO_ENDPOINT: "http://minio:9000"
MINIO_ACCESS_KEY: "${MINIO_ROOT_USER}" MINIO_ACCESS_KEY: "${MINIO_ROOT_USER}"
...@@ -66,9 +70,11 @@ services: ...@@ -66,9 +70,11 @@ services:
POSTGRES_USER: "${POSTGRES_USER}" POSTGRES_USER: "${POSTGRES_USER}"
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}" POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
POSTGRES_DB: "${POSTGRES_DB}" POSTGRES_DB: "${POSTGRES_DB}"
volumes:
- ./uploads:/app/uploads
depends_on: depends_on:
- minio - minio
- postgres - data-handler
volumes: volumes:
pgdata: pgdata:
......
...@@ -8,7 +8,7 @@ RUN pip install --no-cache-dir -r requirements.txt ...@@ -8,7 +8,7 @@ RUN pip install --no-cache-dir -r requirements.txt
COPY . . COPY . .
#just keep the container running without doing anything #just keep the container running without doing anything
CMD ["sh", "-c", "while :; do sleep 10; done"] #CMD ["sh", "-c", "while :; do sleep 10; done"]
#run the app automatically when the container starts #run the app automatically when the container starts
#CMD ["python", "main.py"] CMD ["python", "main.py"]
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>الدردشة</title> <title>SSLabs AI Testing</title>
<style> <style>
body { body {
font-family: 'Arial', sans-serif; font-family: 'Arial', sans-serif;
...@@ -161,7 +161,7 @@ ...@@ -161,7 +161,7 @@
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<h1>الدردشة</h1> <h1>SSLabs AI Testing</h1>
<div class="controls"> <div class="controls">
<div class="student-id-controls"> <div class="student-id-controls">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment