studios / app.py
Persano's picture
Update app.py
567b716 verified
raw
history blame
5.35 kB
import gradio as gr
from fpdf import FPDF
import os
import tempfile
import matplotlib.pyplot as plt
import numpy as np
# Função para calcular o ROI
def calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel):
# Desconto sobre o aluguel (custos + administração + impostos)
aluguel_mensal_com_desconto = aluguel_mensal * (1 - desconto_aluguel / 100)
# Calcular valor total do financiamento
valor_financiado = valor_imovel - investimento_proprio
# Calcular parcelas mensais do financiamento
juros_mensal = (taxa_juros / 100) / 12
numero_parcelas = tempo_anos * 12
parcela_mensal = valor_financiado * (juros_mensal * (1 + juros_mensal) ** numero_parcelas) / ((1 + juros_mensal) ** numero_parcelas - 1)
# Calcular ROI para o primeiro ano
total_pago_1_ano = parcela_mensal * 12 + investimento_proprio
lucro_1_ano = aluguel_mensal_com_desconto * 12 - total_pago_1_ano
roi_1_ano = (lucro_1_ano / total_pago_1_ano) * 100
# Calcular ROI geral
total_pago = parcela_mensal * numero_parcelas + investimento_proprio
lucro = aluguel_mensal_com_desconto * numero_parcelas
roi = (lucro - total_pago) / total_pago * 100
return roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro
# Função para gerar o PDF
def gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro):
# Criar um PDF com o cálculo do ROI
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# Título
pdf.cell(200, 10, txt="Cálculo de ROI de Aluguel", ln=True, align='C')
pdf.ln(10)
# Adicionar os dados no PDF
pdf.cell(200, 10, txt=f"Valor do Imóvel: R$ {valor_imovel:,.2f}", ln=True)
pdf.cell(200, 10, txt=f"Investimento Próprio: R$ {investimento_proprio:,.2f}", ln=True)
pdf.cell(200, 10, txt=f"Aluguel Mensal: R$ {aluguel_mensal:,.2f}", ln=True)
pdf.cell(200, 10, txt=f"Tempo (anos): {tempo_anos} anos", ln=True)
pdf.cell(200, 10, txt=f"Taxa de Juros: {taxa_juros}%", ln=True)
pdf.cell(200, 10, txt=f"ROI do 1º Ano: {roi_1_ano:.2f}%", ln=True)
pdf.cell(200, 10, txt=f"ROI Geral: {roi:.2f}%", ln=True)
pdf.cell(200, 10, txt=f"Parcela Mensal do Financiamento: R$ {parcela_mensal:,.2f}", ln=True)
pdf.cell(200, 10, txt=f"Lucro Total do 1º Ano: R$ {lucro_1_ano:,.2f}", ln=True)
pdf.cell(200, 10, txt=f"Lucro Total: R$ {lucro:,.2f}", ln=True)
# Salvar o PDF em um arquivo temporário
temp_dir = tempfile.gettempdir()
pdf_file_path = os.path.join(temp_dir, "resultado_roi.pdf")
pdf.output(pdf_file_path)
return pdf_file_path
# Função para gerar o gráfico
def gerar_grafico(roi_1_ano, roi):
# Gerar gráfico
anos = [1, 20]
roi_values = [roi_1_ano, roi]
plt.figure(figsize=(6, 4))
plt.bar(anos, roi_values, color='blue', alpha=0.7)
plt.title('ROI ao Longo do Tempo')
plt.xlabel('Ano')
plt.ylabel('ROI (%)')
plt.xticks(anos, ['1º Ano', 'Geral'])
# Salvar gráfico em um arquivo temporário
temp_dir = tempfile.gettempdir()
grafico_path = os.path.join(temp_dir, "grafico_roi.png")
plt.savefig(grafico_path)
return grafico_path
# Função para o botão "Gerar ROI e PDF"
def calcular_gerar_roi_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel):
# Calcular ROI
roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro = calcular_roi(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, desconto_aluguel)
# Gerar o PDF com os dados
pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi_1_ano, roi, parcela_mensal, lucro_1_ano, lucro)
# Gerar gráfico
grafico_path = gerar_grafico(roi_1_ano, roi)
return grafico_path, pdf_path
# Interface Gradio com gr.Blocks
with gr.Blocks() as iface:
# Inputs para os dados
valor_imovel = gr.Number(label="Valor do Imóvel (R$)", value=500000)
investimento_proprio = gr.Number(label="Investimento Próprio (R$)", value=100000)
aluguel_mensal = gr.Number(label="Aluguel Mensal (R$)", value=2500)
tempo_anos = gr.Number(label="Tempo (anos)", value=20)
taxa_juros = gr.Number(label="Taxa de Juros (%)", value=8)
desconto_aluguel = gr.Number(label="Desconto sobre o Aluguel (%)", value=10)
# Resultado do cálculo ROI
roi_resultado = gr.Textbox(label="Resultado do Cálculo do ROI", lines=3)
# Botões
gerar_button = gr.Button("Gerar ROI e PDF")
# Exibição do gráfico
grafico_output = gr.Image(label="Gráfico de ROI")
# Exibição do link de download
pdf_download_button = gr.File(label="Baixar PDF", visible=False)
# Funções de callback para os botões
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])
# Estilo e descrição
iface.title = "Cálculo de ROI de Aluguel"
iface.description = "Insira os valores abaixo para calcular o ROI do seu imóvel e gerar o PDF com os resultados."
# Rodar a interface Gradio
iface.launch(share=True)