Spaces:
Sleeping
Sleeping
Download app.py from DCezaum/TaxBot_Alpha: direct link, hf CLI and curl.
- Browser
- Download file 5.22 kB
-
https://huggingface.co/spaces/DCezaum/TaxBot_Alpha/resolve/main/app.py
- Command line
-
hf download hf://spaces/DCezaum/TaxBot_Alpha/app.py
-
curl -L -o app.py https://huggingface.co/spaces/DCezaum/TaxBot_Alpha/resolve/main/app.py
5.22 kB
| # -*- coding: utf-8 -*- | |
| import os | |
| import streamlit as st | |
| from agente import LimiteDeUsoError, TaxBot | |
| st.set_page_config( | |
| page_title="TaxBot — O Agente do Imposto de Renda", | |
| page_icon="💬", | |
| layout="centered", | |
| ) | |
| EXEMPLOS = [ | |
| "Ganho R$ 6.500 por mês. Quanto vou pagar de IR em 2026?", | |
| "Como funciona a retenção de 10% sobre dividendos?", | |
| "Recebo R$ 80 mil por mês de dividendos de uma empresa. Quanto pago de IRPFM?", | |
| "O que muda para quem ganha até R$ 5 mil?", | |
| ] | |
| # ===================== HELPERS ===================== | |
| def escapar_cifrao(s: str) -> str: | |
| """O Markdown do Streamlit interpreta $...$ como LaTeX; escapa só o cifrão e preserva o resto da formatação.""" | |
| return (s or "").replace("$", "\\$") | |
| def com_aviso(pedacos, aviso): | |
| """Repassa o texto em streaming escapando o cifrão e apaga o aviso de espera ao chegar o primeiro pedaço.""" | |
| for i, pedaco in enumerate(pedacos): | |
| if i == 0: | |
| aviso.empty() | |
| yield escapar_cifrao(pedaco) | |
| aviso.empty() | |
| def chave_gemini() -> str | None: | |
| try: | |
| return st.secrets.get("GEMINI_API_KEY") or os.getenv("GEMINI_API_KEY") | |
| except FileNotFoundError: # sem secrets.toml local | |
| return os.getenv("GEMINI_API_KEY") | |
| def carregar_agente(api_key: str) -> TaxBot: | |
| return TaxBot(api_key=api_key) | |
| def mostrar_detalhes(fontes, ferramentas): | |
| if ferramentas: | |
| with st.expander("🧮 Cálculos executados"): | |
| for f in ferramentas: | |
| st.markdown(f"**`{f['nome']}`**") | |
| st.json({"entrada": f["argumentos"], "resultado": f.get("resultado", {})}, expanded=False) | |
| if fontes: | |
| with st.expander("📚 Trechos consultados"): | |
| for t in fontes: | |
| selo = " · ✅ oficial" if t.get("oficial") else "" | |
| st.markdown(f"**{t['fonte']}**, p. {t['pagina']}{selo}") | |
| st.caption(escapar_cifrao(t["texto"][:400]) + "…") | |
| # ===================== SETUP ===================== | |
| api_key = chave_gemini() | |
| if not api_key: | |
| st.error( | |
| "A chave do Gemini não está configurada. Defina `GEMINI_API_KEY` em `.streamlit/secrets.toml` " | |
| "(local) ou em *Settings → Secrets* no Streamlit Community Cloud." | |
| ) | |
| st.stop() | |
| try: | |
| agente = carregar_agente(api_key) | |
| except FileNotFoundError as e: | |
| st.error(str(e)) | |
| st.stop() | |
| if "history" not in st.session_state: | |
| st.session_state.history = [] | |
| with st.sidebar: | |
| st.markdown("### TaxBot Alpha") | |
| st.caption("Baseado na Lei 15.270/2025 (reforma do IR, vigente desde 01/2026).") | |
| if st.button("🗑️ Limpar conversa", use_container_width=True): | |
| st.session_state.history = [] | |
| st.rerun() | |
| # ===================== HEADER ===================== | |
| st.title("💬 TaxBot") | |
| st.markdown( | |
| "Pergunte o que quiser sobre a **reforma do Imposto de Renda**. Eu explico as novas regras, " | |
| "simulo cálculos e comparo o que muda no IRPF." | |
| ) | |
| # ===================== HISTÓRICO ===================== | |
| for m in st.session_state.history: | |
| with st.chat_message(m["role"]): | |
| st.markdown(escapar_cifrao(m["content"])) | |
| if m["role"] == "assistant": | |
| mostrar_detalhes(m.get("fontes"), m.get("ferramentas")) | |
| # ===================== ENTRADA ===================== | |
| pergunta = st.chat_input("Escreva sua pergunta...") | |
| if not st.session_state.history and not pergunta: | |
| st.markdown("**Experimente:**") | |
| cols = st.columns(2) | |
| for i, ex in enumerate(EXEMPLOS): | |
| if cols[i % 2].button(ex, key=f"ex{i}", use_container_width=True): | |
| pergunta = ex | |
| if pergunta: | |
| historico_anterior = [{"role": m["role"], "content": m["content"]} for m in st.session_state.history] | |
| st.session_state.history.append({"role": "user", "content": pergunta}) | |
| with st.chat_message("user"): | |
| st.markdown(escapar_cifrao(pergunta)) | |
| with st.chat_message("assistant"): | |
| fontes, ferramentas, texto = [], [], "" | |
| aviso = st.empty() | |
| aviso.caption("🔎 Consultando os documentos e calculando...") | |
| try: | |
| r, pedacos = agente.responder_stream(pergunta, historico_anterior) | |
| # O texto aparece conforme o Gemini escreve; o aviso some no primeiro pedaço. | |
| st.write_stream(com_aviso(pedacos, aviso)) | |
| texto, fontes, ferramentas = r.texto, r.fontes, r.ferramentas | |
| except LimiteDeUsoError as e: | |
| aviso.empty() | |
| texto = f"⏳ {e}" | |
| st.markdown(texto) | |
| except Exception as e: # noqa: BLE001 — mostra o erro ao usuário em vez de quebrar a página | |
| aviso.empty() | |
| texto = f"⚠️ Erro inesperado: `{e}`" | |
| st.markdown(texto) | |
| mostrar_detalhes(fontes, ferramentas) | |
| st.session_state.history.append( | |
| {"role": "assistant", "content": texto, "fontes": fontes, "ferramentas": ferramentas} | |
| ) | |
| if len(st.session_state.history) == 2: | |
| st.rerun() # tira os botões de exemplo da tela | |
| st.caption( | |
| "⚠️ Informação geral: **não** constitui aconselhamento tributário, contábil ou jurídico." | |
| ) | |