Persano commited on
Commit
4a19aa3
·
verified ·
1 Parent(s): b72b5d3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -82
app.py CHANGED
@@ -1,92 +1,84 @@
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
 
 
1
  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
+
11
+ # Calcular parcelas mensais do financiamento
12
+ juros_mensal = (taxa_juros / 100) / 12
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()
28
  pdf.set_font("Arial", size=12)
29
+
30
+ # Título
31
+ pdf.cell(200, 10, txt="Cálculo de ROI de Aluguel", ln=True, align='C')
32
+ pdf.ln(10)
33
+
34
+ # Adicionar os dados no PDF
35
+ pdf.cell(200, 10, txt=f"Valor do Imóvel: R$ {valor_imovel:,.2f}", ln=True)
36
+ pdf.cell(200, 10, txt=f"Investimento Próprio: R$ {investimento_proprio:,.2f}", ln=True)
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
45
+ temp_dir = tempfile.gettempdir()
46
+ pdf_file_path = os.path.join(temp_dir, "resultado_roi.pdf")
47
+ pdf.output(pdf_file_path)
48
+
49
+ return pdf_file_path
50
+
51
+ # Interface Gradio
52
+ def interface(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
+ # Gerar PDF
57
+ pdf_path = gerar_pdf(valor_imovel, investimento_proprio, aluguel_mensal, tempo_anos, taxa_juros, roi, parcela_mensal, lucro)
58
+
59
+ # Retornar o link de download
60
+ return f"Aqui está o seu cálculo de ROI. Clique no link abaixo para baixar o PDF:", pdf_path
61
+
62
+ # Definir a interface do Gradio
63
+ iface = gr.Interface(
64
+ fn=interface,
65
+ inputs=[
66
+ gr.Number(label="Valor do Imóvel (R$)", value=500000),
67
+ gr.Number(label="Investimento Próprio (R$)", value=100000),
68
+ gr.Number(label="Aluguel Mensal (R$)", value=2500),
69
+ gr.Number(label="Tempo (anos)", value=20),
70
+ gr.Number(label="Taxa de Juros (%)", value=8)
71
+ ],
72
+ outputs=[
73
+ gr.Textbox(label="Resultado do Cálculo do ROI", lines=3),
74
+ gr.File(label="Baixar PDF")
75
+ ],
76
+ live=True
77
+ )
78
+
79
+ # Rodar a interface Gradio
80
+ iface.launch(share=True)
81
 
 
82
 
83
 
84