Update app.py
Browse files
app.py
CHANGED
@@ -1,72 +1,93 @@
|
|
1 |
import gradio as gr
|
2 |
-
from weasyprint import HTML
|
3 |
-
from datetime import datetime
|
4 |
import tempfile
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto):
|
7 |
try:
|
8 |
valor_imovel = float(valor_imovel)
|
9 |
investimento_proprio = float(investimento_proprio)
|
10 |
aluguel_mensal = float(aluguel_mensal)
|
11 |
-
anos =
|
12 |
taxa_selic = float(taxa_selic) / 100
|
13 |
desconto = float(desconto) / 100
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto):
|
32 |
-
|
33 |
-
html = f"<h2>Simulação de ROI</h2><p>{texto}</p>"
|
34 |
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
40 |
|
41 |
with gr.Blocks() as demo:
|
42 |
-
gr.Markdown("# 📊 Simulador de ROI de Aluguel com
|
43 |
-
with gr.Row():
|
44 |
-
valor_imovel = gr.Textbox(label="Valor do imóvel (R$)", placeholder="Ex: 500000")
|
45 |
-
investimento_proprio = gr.Textbox(label="Investimento próprio (R$)", placeholder="Ex: 100000")
|
46 |
with gr.Row():
|
47 |
-
|
48 |
-
|
49 |
with gr.Row():
|
50 |
-
|
51 |
desconto = gr.Textbox(label="Desconto sobre o aluguel (%)", placeholder="Ex: 10")
|
52 |
with gr.Row():
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
57 |
|
58 |
-
|
59 |
-
calcular_roi,
|
60 |
inputs=[valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto],
|
61 |
-
outputs=
|
62 |
)
|
63 |
-
|
64 |
-
|
|
|
65 |
inputs=[valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto],
|
66 |
-
outputs=
|
|
|
|
|
|
|
|
|
67 |
)
|
68 |
|
69 |
demo.launch(server_port=7860)
|
70 |
|
71 |
|
72 |
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import tempfile
|
3 |
+
import os
|
4 |
+
from fpdf import FPDF
|
5 |
+
|
6 |
+
def format_brl(value):
|
7 |
+
return f"R$ {value:,.2f}".replace(",", "X").replace(".", ",").replace("X", ".")
|
8 |
|
9 |
def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto):
|
10 |
try:
|
11 |
valor_imovel = float(valor_imovel)
|
12 |
investimento_proprio = float(investimento_proprio)
|
13 |
aluguel_mensal = float(aluguel_mensal)
|
14 |
+
anos = float(anos)
|
15 |
taxa_selic = float(taxa_selic) / 100
|
16 |
desconto = float(desconto) / 100
|
17 |
+
|
18 |
+
aluguel_liquido = aluguel_mensal * (1 - desconto)
|
19 |
+
retorno_total = aluguel_liquido * 12 * anos
|
20 |
+
roi = (retorno_total / investimento_proprio) * 100 if investimento_proprio > 0 else 0
|
21 |
+
|
22 |
+
valor_imovel_fmt = format_brl(valor_imovel)
|
23 |
+
investimento_proprio_fmt = format_brl(investimento_proprio)
|
24 |
+
aluguel_liquido_fmt = format_brl(aluguel_liquido)
|
25 |
+
retorno_fmt = format_brl(retorno_total)
|
26 |
+
|
27 |
+
resultado = (
|
28 |
+
f"🧮 **Cálculo de ROI Imobiliário**\n\n"
|
29 |
+
f"📌 Valor do Imóvel: {valor_imovel_fmt}\n"
|
30 |
+
f"📌 Investimento Próprio: {investimento_proprio_fmt}\n"
|
31 |
+
f"📌 Aluguel Mensal (líquido): {aluguel_liquido_fmt}\n"
|
32 |
+
f"📌 Retorno em {anos:.1f} anos: {retorno_fmt}\n"
|
33 |
+
f"📌 Taxa Selic: {taxa_selic * 100:.2f}%\n"
|
34 |
+
f"✅ ROI estimado: **{roi:.2f}%**"
|
35 |
+
)
|
36 |
+
|
37 |
+
return resultado, ""
|
38 |
+
except Exception as e:
|
39 |
+
return "Erro no cálculo: " + str(e), ""
|
40 |
|
41 |
def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto):
|
42 |
+
resultado, _ = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto)
|
|
|
43 |
|
44 |
+
pdf = FPDF()
|
45 |
+
pdf.add_page()
|
46 |
+
pdf.set_font("Arial", size=12)
|
47 |
+
for linha in resultado.split('\n'):
|
48 |
+
pdf.cell(200, 10, txt=linha.strip(), ln=True)
|
49 |
|
50 |
+
temp_dir = tempfile.mkdtemp()
|
51 |
+
pdf_path = os.path.join(temp_dir, "roi_calculo.pdf")
|
52 |
+
pdf.output(pdf_path)
|
53 |
+
|
54 |
+
return pdf_path
|
55 |
|
56 |
with gr.Blocks() as demo:
|
57 |
+
gr.Markdown("# 📊 Simulador de ROI de Aluguel com PDF")
|
|
|
|
|
|
|
58 |
with gr.Row():
|
59 |
+
valor_imovel = gr.Textbox(label="Valor do Imóvel (R$)", placeholder="Ex: 300000")
|
60 |
+
investimento_proprio = gr.Textbox(label="Investimento Próprio (R$)", placeholder="Ex: 100000")
|
61 |
with gr.Row():
|
62 |
+
aluguel_mensal = gr.Textbox(label="Aluguel Mensal Bruto (R$)", placeholder="Ex: 2000")
|
63 |
desconto = gr.Textbox(label="Desconto sobre o aluguel (%)", placeholder="Ex: 10")
|
64 |
with gr.Row():
|
65 |
+
anos = gr.Textbox(label="Tempo (em anos)", placeholder="Ex: 5")
|
66 |
+
taxa_selic = gr.Textbox(label="Taxa Selic (%)", placeholder="Ex: 10.75")
|
67 |
+
|
68 |
+
calcular_btn = gr.Button("🔍 Calcular ROI")
|
69 |
+
gerar_pdf_btn = gr.Button("📄 Gerar PDF do Resultado")
|
70 |
+
resultado_output = gr.Markdown()
|
71 |
+
pdf_output = gr.File(label="⬇️ Baixar PDF", interactive=True, visible=False)
|
72 |
|
73 |
+
calcular_btn.click(
|
74 |
+
fn=calcular_roi,
|
75 |
inputs=[valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto],
|
76 |
+
outputs=[resultado_output, pdf_output]
|
77 |
)
|
78 |
+
|
79 |
+
gerar_pdf_btn.click(
|
80 |
+
fn=gerar_pdf,
|
81 |
inputs=[valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto],
|
82 |
+
outputs=pdf_output
|
83 |
+
).then(
|
84 |
+
lambda: gr.update(visible=True),
|
85 |
+
inputs=[],
|
86 |
+
outputs=pdf_output
|
87 |
)
|
88 |
|
89 |
demo.launch(server_port=7860)
|
90 |
|
91 |
|
92 |
|
93 |
+
|