Update app.py
Browse files
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 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
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 |
|