Spaces:
Sleeping
Sleeping
David Hrachovy commited on
Commit ·
78f9503
0
Parent(s):
Init
Browse files- .gitignore +47 -0
- Readme.md +44 -0
- app.py +72 -0
- requirements.in +10 -0
- requirements.txt +0 -0
.gitignore
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Python
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
*.so
|
| 6 |
+
.Python
|
| 7 |
+
build/
|
| 8 |
+
develop-eggs/
|
| 9 |
+
dist/
|
| 10 |
+
downloads/
|
| 11 |
+
eggs/
|
| 12 |
+
.eggs/
|
| 13 |
+
lib/
|
| 14 |
+
lib64/
|
| 15 |
+
parts/
|
| 16 |
+
sdist/
|
| 17 |
+
var/
|
| 18 |
+
wheels/
|
| 19 |
+
*.egg-info/
|
| 20 |
+
.installed.cfg
|
| 21 |
+
*.egg
|
| 22 |
+
|
| 23 |
+
# Virtual Environment
|
| 24 |
+
venv/
|
| 25 |
+
env/
|
| 26 |
+
ENV/
|
| 27 |
+
.venv/
|
| 28 |
+
|
| 29 |
+
# IDE
|
| 30 |
+
.idea/
|
| 31 |
+
.vscode/
|
| 32 |
+
*.swp
|
| 33 |
+
*.swo
|
| 34 |
+
|
| 35 |
+
# Gradio
|
| 36 |
+
flagged/
|
| 37 |
+
|
| 38 |
+
# Environment variables
|
| 39 |
+
.env
|
| 40 |
+
|
| 41 |
+
# Database
|
| 42 |
+
*.db
|
| 43 |
+
|
| 44 |
+
# Misc
|
| 45 |
+
.DS_Store
|
| 46 |
+
.gradio/
|
| 47 |
+
lib/
|
Readme.md
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Real Estate Projects
|
| 3 |
+
emoji: 🏠
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
app_file: app.py
|
| 8 |
+
pinned: false
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Real Estate Projects Scraper and Analyzer
|
| 12 |
+
|
| 13 |
+
A Gradio interface that scrapes and analyzes real estate projects using LangChain and SQLite.
|
| 14 |
+
|
| 15 |
+
## Features
|
| 16 |
+
- Stores data in SQLite using Peewee ORM
|
| 17 |
+
- Chat interface to query project data
|
| 18 |
+
|
| 19 |
+
## Setup
|
| 20 |
+
|
| 21 |
+
1. Create and activate a virtual environment:
|
| 22 |
+
```sh
|
| 23 |
+
uv venv --python 3.12
|
| 24 |
+
```
|
| 25 |
+
|
| 26 |
+
2. Compile the requirements file:
|
| 27 |
+
```sh
|
| 28 |
+
uv pip compile requirements.in --generate-hashes -o requirements.txt
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
3. Install the dependencies:
|
| 32 |
+
```sh
|
| 33 |
+
uv pip install -r requirements.txt
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
4. Run the application:
|
| 37 |
+
```sh
|
| 38 |
+
uv run app.py
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
5. Upgrade all packages to their latest versions:
|
| 42 |
+
```sh
|
| 43 |
+
uv pip install --upgrade -r requirements.txt
|
| 44 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain import hub
|
| 2 |
+
from langchain.agents import AgentExecutor, create_openai_tools_agent
|
| 3 |
+
from langchain_openai import ChatOpenAI
|
| 4 |
+
from langchain_community.utilities import SQLDatabase
|
| 5 |
+
from langchain_community.agent_toolkits.sql.toolkit import SQLDatabaseToolkit
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from dotenv import load_dotenv
|
| 8 |
+
from langchain.schema import HumanMessage, AIMessage
|
| 9 |
+
import os.path
|
| 10 |
+
|
| 11 |
+
# Load environment variables
|
| 12 |
+
load_dotenv()
|
| 13 |
+
|
| 14 |
+
# Check if database exists
|
| 15 |
+
if not os.path.exists('estate.db'):
|
| 16 |
+
raise FileNotFoundError(
|
| 17 |
+
"Database file 'estate.db' not found. Please run 'uv run init_db.py' first."
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
# Initialize model and database
|
| 21 |
+
model = ChatOpenAI(model="gpt-4", streaming=True)
|
| 22 |
+
db = SQLDatabase.from_uri("sqlite:///estate.db")
|
| 23 |
+
|
| 24 |
+
# Set up SQL toolkit and tools
|
| 25 |
+
toolkit = SQLDatabaseToolkit(db=db, llm=model)
|
| 26 |
+
tools = toolkit.get_tools()
|
| 27 |
+
|
| 28 |
+
# Get the OpenAI tools agent prompt
|
| 29 |
+
prompt = hub.pull("hwchase17/openai-tools-agent")
|
| 30 |
+
|
| 31 |
+
# Create the agent with OpenAI tools format
|
| 32 |
+
agent = create_openai_tools_agent(
|
| 33 |
+
llm=model,
|
| 34 |
+
tools=tools,
|
| 35 |
+
prompt=prompt
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Create agent executor
|
| 39 |
+
agent_executor = AgentExecutor(
|
| 40 |
+
agent=agent,
|
| 41 |
+
tools=tools,
|
| 42 |
+
verbose=True
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
def chat_with_sql(message, history):
|
| 46 |
+
try:
|
| 47 |
+
history_langchain_format = []
|
| 48 |
+
for msg in history:
|
| 49 |
+
if msg['role'] == "user":
|
| 50 |
+
history_langchain_format.append(HumanMessage(content=msg['content']))
|
| 51 |
+
elif msg['role'] == "assistant":
|
| 52 |
+
history_langchain_format.append(AIMessage(content=msg['content']))
|
| 53 |
+
history_langchain_format.append(HumanMessage(content=message))
|
| 54 |
+
response = agent_executor.invoke({"input": message, "history": history_langchain_format})
|
| 55 |
+
for i in range(len(response["output"])):
|
| 56 |
+
yield response["output"][:i+1]
|
| 57 |
+
except Exception as e:
|
| 58 |
+
yield f"Error: {str(e)}"
|
| 59 |
+
|
| 60 |
+
# Create the Gradio interface
|
| 61 |
+
demo = gr.ChatInterface(
|
| 62 |
+
fn=chat_with_sql,
|
| 63 |
+
title="SQL Chat Assistant",
|
| 64 |
+
description="Ask questions about your SQLite database!",
|
| 65 |
+
examples=[
|
| 66 |
+
"What is project with the lowest deposit?",
|
| 67 |
+
],
|
| 68 |
+
type="messages"
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
demo.launch(share=False, debug=True)
|
requirements.in
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
langchain
|
| 3 |
+
langchain-community
|
| 4 |
+
langchain-openai
|
| 5 |
+
python-dotenv
|
| 6 |
+
beautifulsoup4
|
| 7 |
+
requests
|
| 8 |
+
peewee
|
| 9 |
+
browser_use
|
| 10 |
+
openai
|
requirements.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|