Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +31 -2
src/streamlit_app.py
CHANGED
|
@@ -36,6 +36,9 @@ st.set_page_config(
|
|
| 36 |
st.title("Churn – Regressão Logística (PPCA/UnB)")
|
| 37 |
st.caption("Item (a) – Modelagem da Retenção de Clientes e interpretação de coeficientes/odds ratio.")
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
# -----------------------------
|
| 40 |
# Data loader (cache) – robusto para HF Spaces
|
| 41 |
# -----------------------------
|
|
@@ -51,6 +54,8 @@ def load_data():
|
|
| 51 |
except Exception:
|
| 52 |
pass
|
| 53 |
roots += [Path.cwd(), Path("."), Path("/home/user/app")]
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Caminhos explícitos rápidos
|
| 56 |
fast_candidates = [
|
|
@@ -83,8 +88,10 @@ def load_data():
|
|
| 83 |
|
| 84 |
# 2) Busca recursiva case-insensitive pelo nome
|
| 85 |
targets = []
|
|
|
|
| 86 |
for root in roots:
|
| 87 |
-
if root.exists():
|
|
|
|
| 88 |
for p in root.rglob("*"):
|
| 89 |
try:
|
| 90 |
if p.is_file() and p.name.lower() == "churn_modelling.csv":
|
|
@@ -134,6 +141,7 @@ if df.empty:
|
|
| 134 |
"com exatamente esse caminho e nome (case-sensitive)."
|
| 135 |
)
|
| 136 |
|
|
|
|
| 137 |
st.info("**Alternativa:** faça upload do CSV abaixo para testar agora (não persiste no repositório).")
|
| 138 |
up = st.file_uploader("Envie Churn_Modelling.csv", type=["csv"])
|
| 139 |
if up is not None:
|
|
@@ -148,8 +156,29 @@ if df.empty:
|
|
| 148 |
up.seek(0)
|
| 149 |
df = pd.read_csv(up, sep="\t")
|
| 150 |
data_info = "via upload do usuário"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
else:
|
| 152 |
-
|
|
|
|
|
|
|
| 153 |
|
| 154 |
st.success(f"Dataset carregado de: `{data_info}`")
|
| 155 |
|
|
|
|
| 36 |
st.title("Churn – Regressão Logística (PPCA/UnB)")
|
| 37 |
st.caption("Item (a) – Modelagem da Retenção de Clientes e interpretação de coeficientes/odds ratio.")
|
| 38 |
|
| 39 |
+
# Botão para limpar cache do loader e revarrer o repositório
|
| 40 |
+
st.sidebar.button("🔄 Reescanear CSV", on_click=lambda: st.cache_data.clear())
|
| 41 |
+
|
| 42 |
# -----------------------------
|
| 43 |
# Data loader (cache) – robusto para HF Spaces
|
| 44 |
# -----------------------------
|
|
|
|
| 54 |
except Exception:
|
| 55 |
pass
|
| 56 |
roots += [Path.cwd(), Path("."), Path("/home/user/app")]
|
| 57 |
+
# ADIÇÃO: raízes comuns em Spaces
|
| 58 |
+
roots += [Path("/app"), Path("/app/src")]
|
| 59 |
|
| 60 |
# Caminhos explícitos rápidos
|
| 61 |
fast_candidates = [
|
|
|
|
| 88 |
|
| 89 |
# 2) Busca recursiva case-insensitive pelo nome
|
| 90 |
targets = []
|
| 91 |
+
seen_dirs = set()
|
| 92 |
for root in roots:
|
| 93 |
+
if root.exists() and str(root) not in seen_dirs:
|
| 94 |
+
seen_dirs.add(str(root))
|
| 95 |
for p in root.rglob("*"):
|
| 96 |
try:
|
| 97 |
if p.is_file() and p.name.lower() == "churn_modelling.csv":
|
|
|
|
| 141 |
"com exatamente esse caminho e nome (case-sensitive)."
|
| 142 |
)
|
| 143 |
|
| 144 |
+
# Alternativa 1: upload local (pode falhar em Spaces públicos com 403)
|
| 145 |
st.info("**Alternativa:** faça upload do CSV abaixo para testar agora (não persiste no repositório).")
|
| 146 |
up = st.file_uploader("Envie Churn_Modelling.csv", type=["csv"])
|
| 147 |
if up is not None:
|
|
|
|
| 156 |
up.seek(0)
|
| 157 |
df = pd.read_csv(up, sep="\t")
|
| 158 |
data_info = "via upload do usuário"
|
| 159 |
+
# Alternativa 2: URL direta
|
| 160 |
+
st.info("**Alternativa 2 (URL):** informe um link direto (RAW) para o CSV (ex.: GitHub Raw ou Hugging Face Datasets) e clique em **Carregar via URL**.")
|
| 161 |
+
url_csv = st.text_input("URL direta do CSV (https://...)")
|
| 162 |
+
load_url = st.button("Carregar via URL")
|
| 163 |
+
if load_url and url_csv:
|
| 164 |
+
try:
|
| 165 |
+
df = pd.read_csv(url_csv)
|
| 166 |
+
data_info = f"via URL: {url_csv}"
|
| 167 |
+
except Exception:
|
| 168 |
+
try:
|
| 169 |
+
df = pd.read_csv(url_csv, sep=";")
|
| 170 |
+
data_info = f"via URL (sep=';'): {url_csv}"
|
| 171 |
+
except Exception:
|
| 172 |
+
try:
|
| 173 |
+
df = pd.read_csv(url_csv, sep="\t")
|
| 174 |
+
data_info = f"via URL (sep='\\t'): {url_csv}"
|
| 175 |
+
except Exception as e2:
|
| 176 |
+
st.error(f"Falha ao carregar via URL: {e2}")
|
| 177 |
+
st.stop()
|
| 178 |
else:
|
| 179 |
+
# Se nem upload nem URL foram usados e df continua vazio, parar aqui
|
| 180 |
+
if df.empty:
|
| 181 |
+
st.stop()
|
| 182 |
|
| 183 |
st.success(f"Dataset carregado de: `{data_info}`")
|
| 184 |
|