Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sqlite3
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import openai
|
| 4 |
+
import os
|
| 5 |
+
import streamlit as st
|
| 6 |
+
import datetime
|
| 7 |
+
|
| 8 |
+
# Set OpenAI API Key (Ensure it's set properly)
|
| 9 |
+
openai.api_key = os.getenv("sk-NOBe-504FBda5dOQPesE8xKYgzmvBhG_Z_21UZGXtvT3BlbkFJF2vDvLgwzMMrIYgqAC6ezqMnupr9ZAelUCMH4XBP8A") or "sk-NOBe-504FBda5dOQPesE8xKYgzmvBhG_Z_21UZGXtvT3BlbkFJF2vDvLgwzMMrIYgqAC6ezqMnupr9ZAelUCMH4XBP8A"
|
| 10 |
+
|
| 11 |
+
# Create sample house data
|
| 12 |
+
house_data = [
|
| 13 |
+
("Alice", "1234567890", 15000, True, "Delhi", "2BHK", "Furnished"),
|
| 14 |
+
("Bob", "9876543210", 45000, False, "Mumbai", "3BHK", "Semi Furnished"),
|
| 15 |
+
("Charlie", "5556667777", 30000, True, "Pune", "4BHK", "Non Furnished"),
|
| 16 |
+
("David", "4445556666", 25000, True, "Jaipur", "1BHK", "Furnished"),
|
| 17 |
+
("Eve", "3332221111", 40000, False, "Ahmedabad", "3BHK", "Semi Furnished")
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
# Create DataFrame and save to CSV
|
| 21 |
+
df = pd.DataFrame(house_data, columns=["owner_name", "contact", "price", "for_sale", "location", "house_type", "house_details"])
|
| 22 |
+
df.to_csv("houses.csv", index=False)
|
| 23 |
+
|
| 24 |
+
# Connect to SQLite Database
|
| 25 |
+
conn = sqlite3.connect("houses.db")
|
| 26 |
+
cursor = conn.cursor()
|
| 27 |
+
|
| 28 |
+
# Create Table if it doesn't exist
|
| 29 |
+
cursor.execute('''
|
| 30 |
+
CREATE TABLE IF NOT EXISTS houses (
|
| 31 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 32 |
+
owner_name TEXT,
|
| 33 |
+
contact TEXT,
|
| 34 |
+
price INTEGER,
|
| 35 |
+
for_sale BOOLEAN,
|
| 36 |
+
location TEXT,
|
| 37 |
+
house_type TEXT,
|
| 38 |
+
house_details TEXT
|
| 39 |
+
)
|
| 40 |
+
''')
|
| 41 |
+
|
| 42 |
+
# Load data from CSV and insert into database (replace table content if exists)
|
| 43 |
+
df = pd.read_csv("houses.csv")
|
| 44 |
+
df.to_sql("houses", conn, if_exists="replace", index=False)
|
| 45 |
+
conn.commit()
|
| 46 |
+
|
| 47 |
+
# Function to retrieve data from SQLite Database
|
| 48 |
+
def retrieve_data():
|
| 49 |
+
cursor.execute("SELECT * FROM houses")
|
| 50 |
+
rows = cursor.fetchall()
|
| 51 |
+
# Assuming the table now includes the 'id' column, we include it in the DataFrame.
|
| 52 |
+
return pd.DataFrame(rows, columns=["owner_name", "contact", "price", "for_sale", "location", "house_type", "house_details"])
|
| 53 |
+
|
| 54 |
+
# Chatbot function that uses the database records in its prompt
|
| 55 |
+
def chatbot(query):
|
| 56 |
+
df = retrieve_data()
|
| 57 |
+
relevant_data = df.to_string(index=False)
|
| 58 |
+
prompt = f"Given the following real estate records:\n{relevant_data}\n\nAnswer the user's query based on the provided data.\n\nUser Query: {query}\nResponse:"
|
| 59 |
+
|
| 60 |
+
response = openai.ChatCompletion.create(
|
| 61 |
+
model="gpt-3.5-turbo",
|
| 62 |
+
messages=[
|
| 63 |
+
{"role": "system", "content": "You are an AI assistant that uses the provided database records to answer user queries."},
|
| 64 |
+
{"role": "user", "content": prompt}
|
| 65 |
+
]
|
| 66 |
+
)
|
| 67 |
+
return response["choices"][0]["message"]["content"]
|
| 68 |
+
|
| 69 |
+
# Streamlit UI
|
| 70 |
+
st.title("Real Estate Chatbot")
|
| 71 |
+
|
| 72 |
+
st.write("### Available Houses")
|
| 73 |
+
st.dataframe(retrieve_data())
|
| 74 |
+
|
| 75 |
+
query = st.text_input("Ask about available houses:")
|
| 76 |
+
if st.button("Ask Chatbot"):
|
| 77 |
+
if query:
|
| 78 |
+
response_text = chatbot(query)
|
| 79 |
+
# Get the current date and time
|
| 80 |
+
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 81 |
+
st.write("### User Query:")
|
| 82 |
+
st.write(query)
|
| 83 |
+
st.write(f"### Chatbot Response (Generated on {now}):")
|
| 84 |
+
st.write(response_text)
|
| 85 |
+
else:
|
| 86 |
+
st.warning("Please enter a query.")
|
| 87 |
+
|
| 88 |
+
conn.close()
|