Spaces:
Sleeping
Sleeping
Download scripts/setup_database.py from OctusTech/cartalogo-api: direct link, hf CLI and curl.
- Browser
- Download file 3.11 kB
-
https://huggingface.co/spaces/OctusTech/cartalogo-api/resolve/fef4203a8e525970ba9ea85cd6f4d8d406402c23/scripts/setup_database.py
- Command line
-
hf download hf://spaces/OctusTech/cartalogo-api@fef4203a8e525970ba9ea85cd6f4d8d406402c23/scripts/setup_database.py
-
curl -L -o setup_database.py https://huggingface.co/spaces/OctusTech/cartalogo-api/resolve/fef4203a8e525970ba9ea85cd6f4d8d406402c23/scripts/setup_database.py
3.11 kB
| import os | |
| from supabase import create_client, Client | |
| import pinecone | |
| from pinecone import Pinecone | |
| # Configuração do Supabase | |
| SUPABASE_URL = os.getenv('SUPABASE_URL') | |
| SUPABASE_KEY = os.getenv('SUPABASE_KEY') | |
| supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY) | |
| # Configuração do Pinecone | |
| PINECONE_API_KEY = os.getenv('PINECONE_API_KEY') | |
| pc = Pinecone(api_key=PINECONE_API_KEY) | |
| def create_database_tables(): | |
| """Cria as tabelas necessárias no Supabase""" | |
| # Tabela principal de peças | |
| create_pieces_table = """ | |
| CREATE TABLE IF NOT EXISTS pieces ( | |
| sku VARCHAR(50) PRIMARY KEY, | |
| name VARCHAR(255) NOT NULL, | |
| collection VARCHAR(100), | |
| material VARCHAR(255), | |
| predominant_metal VARCHAR(100), | |
| stone VARCHAR(100), | |
| size VARCHAR(50), | |
| description TEXT, | |
| created_at TIMESTAMP DEFAULT NOW(), | |
| updated_at TIMESTAMP DEFAULT NOW() | |
| ); | |
| """ | |
| # Tabela de imagens | |
| create_images_table = """ | |
| CREATE TABLE IF NOT EXISTS piece_images ( | |
| id SERIAL PRIMARY KEY, | |
| sku VARCHAR(50) REFERENCES pieces(sku) ON DELETE CASCADE, | |
| image_type VARCHAR(20) CHECK (image_type IN ('product', 'lifestyle')), | |
| original_url VARCHAR(500), | |
| thumbnail_url VARCHAR(500), | |
| file_name VARCHAR(255), | |
| embeddings_vector_id VARCHAR(100), | |
| technical_analysis JSONB, | |
| created_at TIMESTAMP DEFAULT NOW() | |
| ); | |
| """ | |
| # Índices para otimização | |
| create_indexes = """ | |
| CREATE INDEX IF NOT EXISTS idx_piece_images_sku ON piece_images(sku); | |
| CREATE INDEX IF NOT EXISTS idx_piece_images_type ON piece_images(image_type); | |
| """ | |
| try: | |
| supabase.postgrest.rpc('exec_sql', {'sql': create_pieces_table}).execute() | |
| supabase.postgrest.rpc('exec_sql', {'sql': create_images_table}).execute() | |
| supabase.postgrest.rpc('exec_sql', {'sql': create_indexes}).execute() | |
| print("✅ Tabelas criadas com sucesso!") | |
| except Exception as e: | |
| print(f"❌ Erro ao criar tabelas: {e}") | |
| def setup_pinecone_index(): | |
| """Configura o índice do Pinecone""" | |
| index_name = "jewelry-embeddings" | |
| try: | |
| # Verifica se o índice já existe | |
| if index_name not in pc.list_indexes().names(): | |
| pc.create_index( | |
| name=index_name, | |
| dimension=512, # Dimensão do CLIP | |
| metric='cosine', | |
| spec=pinecone.ServerlessSpec( | |
| cloud='aws', | |
| region='us-east-1' | |
| ) | |
| ) | |
| print(f"✅ Índice {index_name} criado no Pinecone!") | |
| else: | |
| print(f"✅ Índice {index_name} já existe!") | |
| return pc.Index(index_name) | |
| except Exception as e: | |
| print(f"❌ Erro ao configurar Pinecone: {e}") | |
| return None | |
| if __name__ == "__main__": | |
| print("🚀 Configurando banco de dados...") | |
| create_database_tables() | |
| print("🚀 Configurando Pinecone...") | |
| index = setup_pinecone_index() | |
| print("✅ Setup concluído!") | |