Class-Corder / setup.py
setup.py
Raw
import sys
import streamlit as st
from google.cloud import bigquery, storage

def create_table(client, project_id, dataset_name, table_id, schema):
    """
    Creates a table in BigQuery if it does not already exist.
    """
    table = bigquery.Table(f"{project_id}.{dataset_name}.{table_id}", schema=schema)
    table = client.create_table(table, exists_ok=True)
    print(f"Created table: {table.full_table_id}")

def create_bucket(project_id, bucket_name):
    """
    Creates a Cloud Storage bucket if it does not already exist.
    """
    storage_client = storage.Client()
    bucket = storage_client.create_bucket(bucket_name)

client = bigquery.Client()
project_id = st.secrets.project
bucket_name = st.secrets.bucket
dataset_name = st.secrets.dataset

create_bucket(project_id, bucket_name)

tables = {
    "Quizzes": [
        bigquery.SchemaField("Quiz_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Quiz_name", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Quiz_completed", "BOOL"),
        bigquery.SchemaField("Lecture_id", "STRING"),
        bigquery.SchemaField("Created_at", "TIMESTAMP")
    ],
    "Questions": [
        bigquery.SchemaField("Question_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Quiz_id", "STRING"),
        bigquery.SchemaField("Text", "STRING"),
        bigquery.SchemaField("Correct_answer_id", "STRING")
    ],
    "Answers": [
        bigquery.SchemaField("Answer_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Question_id", "STRING"),
        bigquery.SchemaField("Text", "STRING")
    ],
    "UserResponses": [
        bigquery.SchemaField("Response_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Question_id", "STRING"),
        bigquery.SchemaField("Selected_answer_id", "STRING"),
        bigquery.SchemaField("Is_correct", "BOOLEAN")
    ],
    "UserScores": [
        bigquery.SchemaField("Score_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Quiz_id", "STRING"),
        bigquery.SchemaField("Score", "FLOAT64"),
        bigquery.SchemaField("Completed_at", "TIMESTAMP")
    ],
    "Feedback": [
        bigquery.SchemaField("Feedback_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Quiz_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Score_id", "STRING"),
        bigquery.SchemaField("Text", "STRING")
    ],
    "Users": [
        bigquery.SchemaField("username", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("password", "STRING", mode="REQUIRED")
    ],
    "Lectures": [
        bigquery.SchemaField("Lecture_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Username", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Title", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Summary", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Created_at", "TIMESTAMP", mode="REQUIRED"),
        bigquery.SchemaField("Audio_url", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Thumbnail_url", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Transcript", "STRING", mode="REQUIRED")
    ],
    "Chats": [
        bigquery.SchemaField("Lecture_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Chat_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Chat_name", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Sequence_number", "STRING", mode="REQUIRED")
    ],
    "ChatMessages": [
        bigquery.SchemaField("Chat_id", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("User_query", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Gemini_response", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Sequence_number", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("Image_url", "STRING")
    ]
}

# Create each table
for table_id, schema in tables.items():
    create_table(client, project_id, dataset_name, table_id, schema)