Persano commited on
Commit
567b716
·
verified ·
1 Parent(s): a066d47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -28
app.py CHANGED
@@ -2,9 +2,14 @@ import gradio as gr
2
  from fpdf import FPDF
3
  import os
4
  import tempfile
 
 
5
 
6
  # Função para calcular o ROI
7
- def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros):
 
 
 
8
  # Calcular valor total do financiamento
9
  valor_financiado = valor_imovel - investimento_proprio
10
 
@@ -13,15 +18,20 @@ def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos,
13
  numero_parcelas = tempo_anos * 12
14
  parcela_mensal = valor_financiado * (juros_mensal * (1 + juros_mensal) ** numero_parcelas) / ((1 + juros_mensal) ** numero_parcelas - 1)
15
 
16
- # Calcular ROI
 
 
 
 
 
17
  total_pago = parcela_mensal * numero_parcelas + investimento_proprio
18
- lucro = aluguel_mensal * numero_parcelas
19
  roi = (lucro - total_pago) / total_pago * 100
20
 
21
- return roi, parcela_mensal, lucro
22
 
23
  # Função para gerar o PDF
24
- def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi, parcela_mensal, lucro):
25
  # Criar um PDF com o cálculo do ROI
26
  pdf = FPDF()
27
  pdf.add_page()
@@ -37,8 +47,10 @@ def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, ta
37
  pdf.cell(200, 10, txt=f"Aluguel Mensal: R$ {aluguel_mensal:,.2f}", ln=True)
38
  pdf.cell(200, 10, txt=f"Tempo (anos): {tempo_anos} anos", ln=True)
39
  pdf.cell(200, 10, txt=f"Taxa de Juros: {taxa_juros}%", ln=True)
40
- pdf.cell(200, 10, txt=f"ROI Calculado: {roi:.2f}%", ln=True)
 
41
  pdf.cell(200, 10, txt=f"Parcela Mensal do Financiamento: R$ {parcela_mensal:,.2f}", ln=True)
 
42
  pdf.cell(200, 10, txt=f"Lucro Total: R$ {lucro:,.2f}", ln=True)
43
 
44
  # Salvar o PDF em um arquivo temporário
@@ -48,23 +60,38 @@ def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, ta
48
 
49
  return pdf_file_path
50
 
51
- # Função para o botão "Gerar ROI"
52
- def calcular_gerar_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros):
53
- # Calcular ROI
54
- roi, parcela_mensal, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros)
 
55
 
56
- # Exibir resultado
57
- return f"ROI Calculado: {roi:.2f}%\nParcela Mensal: R$ {parcela_mensal:,.2f}\nLucro Total: R$ {lucro:,.2f}"
 
 
 
 
 
 
 
 
 
 
 
58
 
59
- # Função para o botão "Gerar PDF"
60
- def gerar_pdf_arquivo(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros):
61
  # Calcular ROI
62
- roi, parcela_mensal, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros)
63
 
64
  # Gerar o PDF com os dados
65
- pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi, parcela_mensal, lucro)
 
 
 
66
 
67
- return pdf_path
68
 
69
  # Interface Gradio com gr.Blocks
70
  with gr.Blocks() as iface:
@@ -74,25 +101,27 @@ with gr.Blocks() as iface:
74
  aluguel_mensal = gr.Number(label="Aluguel Mensal (R$)", value=2500)
75
  tempo_anos = gr.Number(label="Tempo (anos)", value=20)
76
  taxa_juros = gr.Number(label="Taxa de Juros (%)", value=8)
77
-
 
78
  # Resultado do cálculo ROI
79
  roi_resultado = gr.Textbox(label="Resultado do Cálculo do ROI", lines=3)
80
 
81
  # Botões
82
- gerar_roi_button = gr.Button("Gerar ROI")
83
- gerar_pdf_button = gr.Button("Gerar PDF", elem_id="pdf_button")
84
 
85
- # Funções de callback para os botões
86
- gerar_roi_button.click(fn=calcular_gerar_roi, inputs=[valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros], outputs=roi_resultado)
87
- pdf_file_output = gerar_pdf_button.click(fn=gerar_pdf_arquivo, inputs=[valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros], outputs=gr.File(label="Baixar PDF"))
88
 
89
- # Configuração do tema e título
90
- iface.title = "Cálculo de ROI de Aluguel"
91
- iface.description = "Insira os valores abaixo e calcule o retorno sobre investimento (ROI) de aluguel."
92
 
93
- # Mostrar link de download como botão após a geração do PDF
94
- pdf_file_output.update(visible=True)
95
 
 
 
 
 
96
  # Rodar a interface Gradio
97
  iface.launch(share=True)
98
 
 
2
  from fpdf import FPDF
3
  import os
4
  import tempfile
5
+ import matplotlib.pyplot as plt
6
+ import numpy as np
7
 
8
  # Função para calcular o ROI
9
+ def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel):
10
+ # Desconto sobre o aluguel (custos + administração + impostos)
11
+ aluguel_mensal_com_desconto = aluguel_mensal * (1 - desconto_aluguel / 100)
12
+
13
  # Calcular valor total do financiamento
14
  valor_financiado = valor_imovel - investimento_proprio
15
 
 
18
  numero_parcelas = tempo_anos * 12
19
  parcela_mensal = valor_financiado * (juros_mensal * (1 + juros_mensal) ** numero_parcelas) / ((1 + juros_mensal) ** numero_parcelas - 1)
20
 
21
+ # Calcular ROI para o primeiro ano
22
+ total_pago_1_ano = parcela_mensal * 12 + investimento_proprio
23
+ lucro_1_ano = aluguel_mensal_com_desconto * 12 - total_pago_1_ano
24
+ roi_1_ano = (lucro_1_ano / total_pago_1_ano) * 100
25
+
26
+ # Calcular ROI geral
27
  total_pago = parcela_mensal * numero_parcelas + investimento_proprio
28
+ lucro = aluguel_mensal_com_desconto * numero_parcelas
29
  roi = (lucro - total_pago) / total_pago * 100
30
 
31
+ return roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro
32
 
33
  # Função para gerar o PDF
34
+ def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro):
35
  # Criar um PDF com o cálculo do ROI
36
  pdf = FPDF()
37
  pdf.add_page()
 
47
  pdf.cell(200, 10, txt=f"Aluguel Mensal: R$ {aluguel_mensal:,.2f}", ln=True)
48
  pdf.cell(200, 10, txt=f"Tempo (anos): {tempo_anos} anos", ln=True)
49
  pdf.cell(200, 10, txt=f"Taxa de Juros: {taxa_juros}%", ln=True)
50
+ pdf.cell(200, 10, txt=f"ROI do 1º Ano: {roi_1_ano:.2f}%", ln=True)
51
+ pdf.cell(200, 10, txt=f"ROI Geral: {roi:.2f}%", ln=True)
52
  pdf.cell(200, 10, txt=f"Parcela Mensal do Financiamento: R$ {parcela_mensal:,.2f}", ln=True)
53
+ pdf.cell(200, 10, txt=f"Lucro Total do 1º Ano: R$ {lucro_1_ano:,.2f}", ln=True)
54
  pdf.cell(200, 10, txt=f"Lucro Total: R$ {lucro:,.2f}", ln=True)
55
 
56
  # Salvar o PDF em um arquivo temporário
 
60
 
61
  return pdf_file_path
62
 
63
+ # Função para gerar o gráfico
64
+ def gerar_grafico(roi_1_ano, roi):
65
+ # Gerar gráfico
66
+ anos = [1, 20]
67
+ roi_values = [roi_1_ano, roi]
68
 
69
+ plt.figure(figsize=(6, 4))
70
+ plt.bar(anos, roi_values, color='blue', alpha=0.7)
71
+ plt.title('ROI ao Longo do Tempo')
72
+ plt.xlabel('Ano')
73
+ plt.ylabel('ROI (%)')
74
+ plt.xticks(anos, ['1º Ano', 'Geral'])
75
+
76
+ # Salvar gráfico em um arquivo temporário
77
+ temp_dir = tempfile.gettempdir()
78
+ grafico_path = os.path.join(temp_dir, "grafico_roi.png")
79
+ plt.savefig(grafico_path)
80
+
81
+ return grafico_path
82
 
83
+ # Função para o botão "Gerar ROI e PDF"
84
+ def calcular_gerar_roi_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel):
85
  # Calcular ROI
86
+ roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel)
87
 
88
  # Gerar o PDF com os dados
89
+ pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro)
90
+
91
+ # Gerar gráfico
92
+ grafico_path = gerar_grafico(roi_1_ano, roi)
93
 
94
+ return grafico_path, pdf_path
95
 
96
  # Interface Gradio com gr.Blocks
97
  with gr.Blocks() as iface:
 
101
  aluguel_mensal = gr.Number(label="Aluguel Mensal (R$)", value=2500)
102
  tempo_anos = gr.Number(label="Tempo (anos)", value=20)
103
  taxa_juros = gr.Number(label="Taxa de Juros (%)", value=8)
104
+ desconto_aluguel = gr.Number(label="Desconto sobre o Aluguel (%)", value=10)
105
+
106
  # Resultado do cálculo ROI
107
  roi_resultado = gr.Textbox(label="Resultado do Cálculo do ROI", lines=3)
108
 
109
  # Botões
110
+ gerar_button = gr.Button("Gerar ROI e PDF")
 
111
 
112
+ # Exibição do gráfico
113
+ grafico_output = gr.Image(label="Gráfico de ROI")
 
114
 
115
+ # Exibição do link de download
116
+ pdf_download_button = gr.File(label="Baixar PDF", visible=False)
 
117
 
118
+ # Funções de callback para os botões
119
+ gerar_button.click(fn=calcular_gerar_roi_pdf, inputs=[valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel], outputs=[grafico_output, pdf_download_button])
120
 
121
+ # Estilo e descrição
122
+ iface.title = "Cálculo de ROI de Aluguel"
123
+ iface.description = "Insira os valores abaixo para calcular o ROI do seu imóvel e gerar o PDF com os resultados."
124
+
125
  # Rodar a interface Gradio
126
  iface.launch(share=True)
127