Persano commited on
Commit
8a2fdb3
·
verified ·
1 Parent(s): 209230e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -42
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 = int(anos)
12
  taxa_selic = float(taxa_selic) / 100
13
  desconto = float(desconto) / 100
14
- except ValueError:
15
- return "Erro: Verifique os dados de entrada."
16
-
17
- aluguel_liquido = aluguel_mensal * (1 - desconto)
18
- retorno_total = aluguel_liquido * 12 * anos
19
- roi = ((retorno_total - investimento_proprio) / investimento_proprio) * 100
20
-
21
- resultado = (
22
- f"Valor do Imóvel: R$ {valor_imovel:,.2f}\n"
23
- f"Investimento Próprio: R$ {investimento_proprio:,.2f}\n"
24
- f"Aluguel Mensal (líquido): R$ {aluguel_liquido:,.2f}\n"
25
- f"Tempo: {anos} anos\n"
26
- f"Taxa Selic: {taxa_selic * 100:.2f}%\n"
27
- f"ROI estimado: {roi:.2f}%"
28
- )
29
- return resultado
 
 
 
 
 
 
 
30
 
31
  def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto):
32
- texto = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto).replace("\n", "<br>")
33
- html = f"<h2>Simulação de ROI</h2><p>{texto}</p>"
34
 
35
- data = datetime.now().strftime("%d-%m-%Y")
36
- filename = f"/tmp/roi-simulacao-{data}.pdf"
 
 
 
37
 
38
- HTML(string=html).write_pdf(filename)
39
- return filename
 
 
 
40
 
41
  with gr.Blocks() as demo:
42
- gr.Markdown("# 📊 Simulador de ROI de Aluguel com Geração de PDF")
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
- aluguel_mensal = gr.Textbox(label="Aluguel mensal (R$)", placeholder="Ex: 2500")
48
- anos = gr.Textbox(label="Duração (anos)", placeholder="Ex: 5")
49
  with gr.Row():
50
- taxa_selic = gr.Textbox(label="Taxa SELIC (%)", placeholder="Ex: 10.75")
51
  desconto = gr.Textbox(label="Desconto sobre o aluguel (%)", placeholder="Ex: 10")
52
  with gr.Row():
53
- botao_calcular = gr.Button("📈 Calcular ROI")
54
- botao_pdf = gr.Button("📄 Gerar PDF")
55
- resultado = gr.Textbox(label="Resultado da Simulação", lines=6)
56
- arquivo_pdf = gr.File(label="Download do PDF")
 
 
 
57
 
58
- botao_calcular.click(
59
- calcular_roi,
60
  inputs=[valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto],
61
- outputs=resultado
62
  )
63
- botao_pdf.click(
64
- gerar_pdf,
 
65
  inputs=[valor_imovel, investimento_proprio, aluguel_mensal, anos, taxa_selic, desconto],
66
- outputs=arquivo_pdf
 
 
 
 
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
+