Persano commited on
Commit
6fcf1fa
·
verified ·
1 Parent(s): 2aef9de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -61
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
- from threading import Thread
 
 
6
 
7
- # Inicializar o aplicativo Flask
8
- app = Flask(__name__)
 
 
 
 
 
 
 
9
 
10
- # Função para gerar PDF usando o WeasyPrint
11
- def generate_pdf(html_content):
12
- pdf_io = io.BytesIO()
13
- # Gerar o PDF a partir do HTML com WeasyPrint
14
- weasyprint.HTML(string=html_content).write_pdf(pdf_io)
15
- pdf_io.seek(0)
16
- return pdf_io
17
 
18
- # Página inicial com formulário e integração com Gradio
19
- @app.route('/')
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
- # Rota para gerar o PDF com o conteúdo fornecido
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 Gradio para gerar PDF
47
- def gradio_generate_pdf(html_content):
48
- pdf_io = generate_pdf(html_content)
49
- return pdf_io
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
51
- # Função para rodar o Gradio (no Hugging Face)
52
- def launch_gradio_interface():
53
- # Definir a interface Gradio
54
- interface = gr.Interface(fn=gradio_generate_pdf,
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
- interface.launch(server_port=7860, server_name="0.0.0.0", share=True)
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
- # Iniciar a interface Gradio na porta 7860
70
- launch_gradio_interface()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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