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>
- pages/relatorio_ia.py +2 -1
- 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 |
-
|
|
|
|
| 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
|
| 14 |
-
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
credenciais = {
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
'
|
| 23 |
-
'
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
'
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
'password': 'ctweek'
|
| 34 |
-
})
|
| 35 |
|
| 36 |
return credenciais
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
credenciais = {
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
'
|
| 44 |
-
'
|
| 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 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 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 |
|