Abimael Torcate Claude commited on
Commit
32be338
·
1 Parent(s): 61c49a3

Fix secret parsing to match Hugging Face Space configuration

Browse files

- Update extrair_credenciais() to use 'db_checklist' secret name
- Parse secret format: Db_host, Porta, Username, Database, Password
- Add fallback for OpenAI API key typo: OPEANAI_APY_KEY -> OPENAI_API_KEY
- Maintain backward compatibility with individual environment variables
- Should now connect properly using the configured Hugging Face secrets

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (2) hide show
  1. pages/relatorio_ia.py +2 -1
  2. utils/database.py +37 -36
pages/relatorio_ia.py CHANGED
@@ -49,7 +49,8 @@ def setup_openai():
49
  pass
50
 
51
  # Priorizar variáveis de ambiente do sistema (Hugging Face Spaces)
52
- api_key = os.getenv('OPENAI_API_KEY')
 
53
 
54
  if not api_key:
55
  return False
 
49
  pass
50
 
51
  # Priorizar variáveis de ambiente do sistema (Hugging Face Spaces)
52
+ # Tentar ambas as grafias (pode haver erro de digitação no secret)
53
+ api_key = os.getenv('OPENAI_API_KEY') or os.getenv('OPEANAI_APY_KEY')
54
 
55
  if not api_key:
56
  return False
utils/database.py CHANGED
@@ -10,50 +10,51 @@ import os
10
  import re
11
 
12
  def extrair_credenciais():
13
- """Extrai as credenciais do secret acesso_db"""
14
- acesso_db = os.getenv('acesso_db', '')
 
15
 
16
- # Se não houver o secret, tenta variáveis individuais
17
- if not acesso_db:
18
- credenciais = {
19
- 'host': os.getenv('DB_HOST'),
20
- 'port': int(os.getenv('DB_PORT', '5432')),
21
- 'database': os.getenv('DB_NAME'),
22
- 'user': os.getenv('DB_USER'),
23
- 'password': os.getenv('DB_PASSWORD')
 
 
 
24
  }
25
 
26
- # Fallback para valores hardcoded quando variáveis não estão definidas
27
- if not credenciais['host']:
28
- credenciais.update({
29
- 'host': '77.37.43.160',
30
- 'port': 5432,
31
- 'database': 'checklist',
32
- 'user': 'abimael',
33
- 'password': 'ctweek'
34
- })
35
 
36
  return credenciais
37
 
38
- # Extrai as credenciais do texto
39
- credenciais = {}
40
-
41
- # Padrões para extrair cada valor
42
- padroes = {
43
- 'host': r'DB_HOST\s*=\s*([\d\.]+)',
44
- 'port': r'DB_PORT\s*=\s*(\d+)',
45
- 'database': r'DB_NAME\s*=\s*(\w+)',
46
- 'user': r'DB_USER\s*=\s*(\w+)',
47
- 'password': r'DB_PASSWORD\s*=\s*(\w+)'
48
  }
49
 
50
- for key, padrao in padroes.items():
51
- match = re.search(padrao, acesso_db)
52
- if match:
53
- if key == 'port':
54
- credenciais[key] = int(match.group(1))
55
- else:
56
- credenciais[key] = match.group(1)
 
 
57
 
58
  return credenciais
59
 
 
10
  import re
11
 
12
  def extrair_credenciais():
13
+ """Extrai as credenciais do secret db_checklist"""
14
+ # Primeiro tenta o secret db_checklist
15
+ db_checklist = os.getenv('db_checklist', '')
16
 
17
+ if db_checklist:
18
+ # Extrai as credenciais do formato usado no secret
19
+ credenciais = {}
20
+
21
+ # Padrões para extrair cada valor do formato específico
22
+ padroes = {
23
+ 'host': r'Db_host:\s*([\d\.]+)',
24
+ 'port': r'Porta:\s*(\d+)',
25
+ 'user': r'Username:\s*(\w+)',
26
+ 'database': r'Database:\s*(\w+)',
27
+ 'password': r'Password:\s*(\w+)'
28
  }
29
 
30
+ for key, padrao in padroes.items():
31
+ match = re.search(padrao, db_checklist)
32
+ if match:
33
+ if key == 'port':
34
+ credenciais[key] = int(match.group(1))
35
+ else:
36
+ credenciais[key] = match.group(1)
 
 
37
 
38
  return credenciais
39
 
40
+ # Fallback para variáveis individuais
41
+ credenciais = {
42
+ 'host': os.getenv('DB_HOST'),
43
+ 'port': int(os.getenv('DB_PORT', '5432')),
44
+ 'database': os.getenv('DB_NAME'),
45
+ 'user': os.getenv('DB_USER'),
46
+ 'password': os.getenv('DB_PASSWORD')
 
 
 
47
  }
48
 
49
+ # Fallback para valores hardcoded quando variáveis não estão definidas
50
+ if not credenciais['host']:
51
+ credenciais.update({
52
+ 'host': '77.37.43.160',
53
+ 'port': 5432,
54
+ 'database': 'checklist',
55
+ 'user': 'abimael',
56
+ 'password': 'ctweek'
57
+ })
58
 
59
  return credenciais
60