Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,72 @@
|
|
1 |
-
from flask import Flask, request, render_template_string, send_file
|
2 |
-
import weasyprint
|
3 |
-
import io
|
4 |
import gradio as gr
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
-
#
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
pdf_io.seek(0)
|
16 |
-
return pdf_io
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
def index():
|
21 |
-
return render_template_string("""
|
22 |
-
<html>
|
23 |
-
<head>
|
24 |
-
<title>Gerador de Orçamento</title>
|
25 |
-
</head>
|
26 |
-
<body>
|
27 |
-
<h1>Gerador de Orçamento e PDF</h1>
|
28 |
-
<form action="/generate_pdf" method="POST">
|
29 |
-
<label for="html_content">Digite o conteúdo HTML do Orçamento:</label><br>
|
30 |
-
<textarea name="html_content" rows="10" cols="50"></textarea><br><br>
|
31 |
-
<input type="submit" value="Gerar PDF">
|
32 |
-
</form>
|
33 |
-
</body>
|
34 |
-
</html>
|
35 |
-
""")
|
36 |
|
37 |
-
|
38 |
-
@app.route('/generate_pdf', methods=['POST'])
|
39 |
-
def generate_pdf_route():
|
40 |
-
html_content = request.form['html_content']
|
41 |
-
pdf_io = generate_pdf(html_content)
|
42 |
-
|
43 |
-
# Enviar o arquivo PDF gerado
|
44 |
-
return send_file(pdf_io, as_attachment=True, download_name="orcamento.pdf", mimetype="application/pdf")
|
45 |
|
46 |
-
# Função
|
47 |
-
def
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
-
# Função para
|
52 |
-
def
|
53 |
-
#
|
54 |
-
|
55 |
-
inputs="text",
|
56 |
-
outputs="file",
|
57 |
-
live=True,
|
58 |
-
title="Gerador de Orçamento",
|
59 |
-
description="Insira o conteúdo HTML para gerar o orçamento em PDF")
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
# Rodar o Flask em uma thread separada e Gradio na porta 7860
|
64 |
-
if __name__ == '__main__':
|
65 |
-
# Rodar o Flask em uma thread separada
|
66 |
-
thread = Thread(target=lambda: app.run(host="0.0.0.0", port=5000))
|
67 |
-
thread.start()
|
68 |
|
69 |
-
#
|
70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import pdfkit
|
3 |
+
from datetime import date
|
4 |
+
import tempfile
|
5 |
|
6 |
+
# Função para calcular o ROI
|
7 |
+
def calcular_roi(valor_imovel, investimento_proprio, taxa_selic, tempo, aluguel, desconto):
|
8 |
+
# Convertendo as entradas para float
|
9 |
+
valor_imovel = float(valor_imovel)
|
10 |
+
investimento = float(investimento_proprio)
|
11 |
+
taxa_selic = float(taxa_selic) / 100
|
12 |
+
tempo = float(tempo) # anos
|
13 |
+
aluguel_mensal = float(aluguel)
|
14 |
+
desconto = float(desconto) / 100
|
15 |
|
16 |
+
# Calculando financiamento, juros, receita líquida e ROI
|
17 |
+
financiamento = valor_imovel - investimento
|
18 |
+
juros_total = financiamento * taxa_selic * tempo
|
19 |
+
receita_bruta = aluguel_mensal * 12 * tempo
|
20 |
+
receita_liquida = receita_bruta * (1 - desconto)
|
|
|
|
|
21 |
|
22 |
+
lucro = receita_liquida - juros_total
|
23 |
+
roi = (lucro / investimento) * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
return round(roi, 2), round(receita_liquida, 2), round(juros_total, 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Função para gerar o HTML do PDF
|
28 |
+
def gerar_pdf_html(valor_imovel, investimento_proprio, taxa_selic, tempo, aluguel, desconto, roi, receita, juros):
|
29 |
+
html = f"""
|
30 |
+
<h1>Relatório de Simulação de ROI</h1>
|
31 |
+
<p>Data: {date.today().strftime('%d/%m/%Y')}</p>
|
32 |
+
<p><strong>Valor do imóvel:</strong> R$ {valor_imovel}</p>
|
33 |
+
<p><strong>Investimento próprio:</strong> R$ {investimento_proprio}</p>
|
34 |
+
<p><strong>Taxa SELIC:</strong> {taxa_selic}%</p>
|
35 |
+
<p><strong>Tempo:</strong> {tempo} anos</p>
|
36 |
+
<p><strong>Aluguel mensal:</strong> R$ {aluguel}</p>
|
37 |
+
<p><strong>Desconto:</strong> {desconto}%</p>
|
38 |
+
<hr>
|
39 |
+
<p><strong>Receita líquida estimada:</strong> R$ {receita}</p>
|
40 |
+
<p><strong>Juros do financiamento:</strong> R$ {juros}</p>
|
41 |
+
<p><strong>ROI:</strong> {roi}%</p>
|
42 |
+
"""
|
43 |
+
return html
|
44 |
|
45 |
+
# Função principal para o Gradio
|
46 |
+
def simular_roi(valor_imovel, investimento_proprio, taxa_selic, tempo, aluguel, desconto):
|
47 |
+
# Calculando o ROI
|
48 |
+
roi, receita, juros = calcular_roi(valor_imovel, investimento_proprio, taxa_selic, tempo, aluguel, desconto)
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
+
# Gerando HTML para o PDF
|
51 |
+
html = gerar_pdf_html(valor_imovel, investimento_proprio, taxa_selic, tempo, aluguel, desconto, roi, receita, juros)
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# Gerando o PDF
|
54 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmpfile:
|
55 |
+
pdfkit.from_string(html, tmpfile.name)
|
56 |
+
return tmpfile.name, f"ROI estimado: {roi}%", html
|
57 |
+
|
58 |
+
# Interface do Gradio
|
59 |
+
iface = gr.Interface(
|
60 |
+
fn=simular_roi,
|
61 |
+
inputs=[
|
62 |
+
gr.Textbox(label="Valor do imóvel (R$)"),
|
63 |
+
gr.Textbox(label="Investimento próprio (entrada) (R$)"),
|
64 |
+
gr.Textbox(label="Taxa SELIC anual (%)"),
|
65 |
+
gr.Textbox(label="Tempo de investimento (anos)"),
|
66 |
+
gr.Textbox(label="Valor do aluguel mensal (R$)"),
|
67 |
+
gr.Textbox(label="Desconto sobre o aluguel (%)"),
|
68 |
+
],
|
69 |
+
outputs=[
|
70 |
+
gr.File(label="Download do PDF"),
|
71 |
+
gr.Textbox(label="Resultado do ROI", interactive=False),
|
72 |
+
gr.HTML(label="Resultado
|