Spaces:
Sleeping
Sleeping
File size: 2,576 Bytes
443d608 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
import gradio as gr
import pandas as pd
import os
import json
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.http import MediaIoBaseDownload
import io
# Função para carregar e ler a planilha
def load_and_get_data():
try:
# Carrega as credenciais a partir da variável de ambiente do Hugging Face Secrets
cred_json = os.getenv("GOOGLE_APPLICATION_CREDENTIALS")
creds = service_account.Credentials.from_service_account_info(json.loads(cred_json))
service = build('drive', 'v3', credentials=creds)
file_id = '1lNadnIVnDs9Oex5ckNXj1-56nvlEea6p'
# Baixa o arquivo CSV
request = service.files().get_media(fileId=file_id)
fh = io.FileIO('imoveis_ficticios.csv', 'wb')
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
# Carrega o CSV no pandas
df = pd.read_csv('imoveis_ficticios.csv')
return df
except Exception as e:
return f"Erro ao acessar o arquivo CSV: {e}"
# Função para o agente conversar e filtrar os imóveis
def conversar_e_sugerir(preco_maximo, status_imovel):
df = load_and_get_data()
# Filtros
df_filtrado = df[df['valor'] <= preco_maximo]
if status_imovel:
df_filtrado = df_filtrado[df_filtrado['status'].isin(status_imovel)]
# Exibe as opções filtradas
if df_filtrado.empty:
return "Desculpe, não encontrei imóveis que atendem aos critérios."
sugestões = df_filtrado[['nome', 'bairro', 'valor', 'status']].head(5) # Limita a 5 opções
resposta = "Aqui estão algumas opções de imóveis para você:\n"
for _, imovel in sugestões.iterrows():
resposta += f"{imovel['nome']} - {imovel['bairro']} - R${imovel['valor']} - Status: {imovel['status']}\n"
return resposta
# Interface do Gradio
def interface():
return gr.Interface(
fn=conversar_e_sugerir,
inputs=[
gr.Slider(minimum=0, maximum=2000000, label="Preço Máximo Procurado", default=2000000),
gr.CheckboxGroup(label="Status do Imóvel", choices=["pronto", "em construção", "na planta"], default=["pronto", "em construção"])
],
outputs="text",
title="Agente Conversacional de Imóveis",
description="Converse com o agente para encontrar o imóvel ideal! Defina o preço máximo e o status do imóvel."
)
# Rodar o app
if __name__ == "__main__":
interface().launch()
|