DEV Community

Cover image for Automating Real Estate Listing Description Generation with Lyzr Automata,Streamlit and OpenAI
harshit-lyzr
harshit-lyzr

Posted on

Automating Real Estate Listing Description Generation with Lyzr Automata,Streamlit and OpenAI

Are you a real estate professional looking to streamline your listing process? Look no further! In this blog, we'll dive into how you can harness the power of automation using Streamlit and OpenAI to generate compelling real estate listing descriptions effortlessly.

We'll walk through the process of building a Streamlit web app that interacts with Lyzr Automata to generate SEO-friendly descriptions based on input parameters such as project name, size range, amenities, and locality. With just a few clicks, you can have engaging descriptions ready for your listings, saving you time and effort.

We'll cover everything from setting up the Streamlit app to integrating the OpenAI model and crafting persuasive prompts for the AI. Additionally, we'll explore how to handle user input validation and error handling to ensure a smooth user experience.

By the end of this tutorial, you'll have a powerful tool at your disposal that not only automates the tedious task of writing real estate descriptions but also enhances the quality and consistency of your listings, ultimately helping you attract more potential buyers or tenants.

Setting Up the Environment
Imports:
Imports necessary libraries: os, streamlit, libraries from lyzr_automata

pip install lyzr_automata streamlit
Enter fullscreen mode Exit fullscreen mode
import streamlit as st
from lyzr_automata.ai_models.openai import OpenAIModel
from lyzr_automata import Agent,Task
from lyzr_automata.pipelines.linear_sync_pipeline import LinearSyncPipeline
from PIL import Image
Enter fullscreen mode Exit fullscreen mode

OpenAI API Key:

api = st.sidebar.text_input("Enter Your OPENAI API KEY HERE",type="password")
Enter fullscreen mode Exit fullscreen mode

api = st.sidebar.text_input("Enter Your OPENAI API KEY HERE",type="password") Creates a text input field in the sidebar where users can enter their OpenAI API key securely (password mode).

OpenAI Model Setup :

if api:
    open_ai_text_completion_model = OpenAIModel(
        api_key=api,
        parameters={
            "model": "gpt-4-turbo-preview",
            "temperature": 0.2,
            "max_tokens": 1500,
        },
    )
else:
    st.sidebar.error("Please Enter Your OPENAI API KEY")
Enter fullscreen mode Exit fullscreen mode

if api: This block executes only if the user has entered an API key.
open_ai_text_completion_model = OpenAIModel(): Creates an instance of the OpenAIModel class with the provided API key and sets the model parameters like "gpt-4-turbo-preview" for the text generation model, temperature for randomness, and maximum token limit.
else: If no API key is provided, an error message is displayed in the sidebar using st.sidebar.error().

User Input Fields:

name = st.text_input("Project Name", placeholder="Lodha World View")
size_range = st.slider("Size Range", 100, 10000, (800, 1800))
project_size = st.text_input("Project Size", placeholder="2 Building - 56 Units")
avg_price = st.number_input("Average Price", placeholder="6.81 K/sq.ft")
configuration = st.text_input("Configuration", placeholder="2,3 BHK")
amenties = st.text_input("Amenities", placeholder="Children's Play Meditation Area,Community Hall,Senior Citizen Siteout,Lift(s),Gymnasium")
locality = st.text_input("Locality", placeholder="Bandra West")
Enter fullscreen mode Exit fullscreen mode

name = st.text_input(): Creates text input fields in the app for users to enter details like project name, size range, project size, average price, configuration, amenities, and locality.

Description Generation Function:

def re_description(project_name, size_range, project_size, avg_price, configuration, locality):
    listing_agent = Agent(
        role="Real Estate expert",
        prompt_persona=f"You are an Expert Real Estate Expert and Copywriter too.Your Task Is to write SEO Friendly Real Estate Listing Description based on Input Values."
    )

    prompt = f"""
    Your Task is to generate SEO friendly Real Estate Listing Description using Below Inputs:
    Project Name: {project_name}
    Size Range: {size_range}
    Project Size: {project_size}
    Average Price: {avg_price}
    Configurations: {configuration}
    Amenities: {amenties}
    Locality: {locality}

    Step 1: Research About Locality and their nearest features which benefits Property like nearest school,hospital and etc.
    Step 2: Write An SEO friendly Real Estate Listing Description using Above information and Inputs. 

    Output:
    Project Description:
    A luxurious residential project with 2bhk homes! Thoughtfully planned to bestow a lavish lifestyle, Kedar Darshan is designed by urban architects and engineers keeping in line with the Vastu principles, striking a perfect balance of space, aesthetics, and amenities. Enjoy an unparalleled experience of urban living with serene surroundings and facilities galore only at Kedar Darshan!

    Amenities:
    Modern Living: A suite of world-class amenities catering to every age and interest.
    Children's Play Area: Dedicated space for children to play and enjoy.
    Yoga/Meditation Area: Area for adults to find solace and rejuvenation.
    Grand Entrance Lobby: Exudes grandeur, welcoming residents and guests into a realm of luxury.
    Community Hall: Perfect venue for celebrations and gatherings, fostering a sense of community.
    Senior Citizens' Sit-Out Area: Exclusive area ensuring peace and tranquility for seniors.
    Vastu Compliance: Ensures positive energy flows throughout the project.
    State-of-the-Art Lifts: Equipped for ease of access across all floors.

    Features:
    Strategic Location: Proximity to Mumbai's best educational institutions, healthcare facilities, shopping destinations, and entertainment hotspots.
    Educational Institutions: Renowned schools like Podar International School nearby.
    Healthcare Facilities: Hospitals like Global Hospital within reach.
    Connectivity: Well-connected to other parts of Mumbai, offering a blend of luxury and convenience.
    """

    listing_task = Task(
        name="Generate Listing Description",
        model=open_ai_text_completion_model,
        agent=listing_agent,
        instructions=prompt,
    )

    output = LinearSyncPipeline(
        name="Listing Description Pipline",
        completion_message="Product Description Generated!!",
        tasks=[
            listing_task
        ],
    ).run()

    answer = output[0]['task_output']

    return answer
Enter fullscreen mode Exit fullscreen mode

def re_description(project_name, size_range, project_size, avg_price, configuration, locality): Defines a function named re_description that takes various property details as arguments.
listing_agent = Agent(): Creates an Agent instance representing a real estate expert and copywriter.
prompt_persona defines the agent's role and task description.
prompt is a multi-line string defining the instructions for the text generation model. It includes placeholders for user-provided property details and outlines steps for research and description writing.
listing_task = Task(): Creates a Task instance specifying the task name (Generate Listing Description), model to be used (open_ai_text_completion_model), agent performing the task (listing_agent), and the prompt (instructions).
LinearSyncPipeline(): Creates a LinearSyncPipeline instance to manage the execution of the text generation task. It defines the pipeline name, completion message ("Product Description Generated!!"), and the tasks list containing the listing_task.
output = pipeline.run(): Runs the pipeline, which executes the text generation task using the OpenAI model and agent.
answer = output[0]['task_output']: Extracts the generated description text from the response.

Output:

if api and st.button("Generate", type="primary"):
    result = re_description(name,size_range,project_size,avg_price,configuration,locality)
    st.markdown(result)
Enter fullscreen mode Exit fullscreen mode

if api and st.button("Generate", type="primary"): This block executes only if the user has entered an API key and clicks the "Generate" button.
result = re_description(name, size_range, project_size, avg_price, configuration, locality): Calls the re_description function with user-provided details to generate the real estate listing description.
st.markdown(result): Displays the generated description using Streamlit's markdown function.

This code utilizes Streamlit to create a web app that allows users to generate real estate listing descriptions. Users enter property details, and if an OpenAI API key is provided, the app leverages the lyzr_automata library to interact with the OpenAI API and generate descriptions through a text-completion model. The LinearSyncPipeline manages the execution of this process. Overall, the code demonstrates how to combine user input, external APIs, and text generation models to create an interactive web application.

try it now: https://lyzr-property-listing.streamlit.app/
For more information explore the website: Lyzr

Top comments (0)